【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
2017-11-02 00:05
232 查看
http://blog.csdn.net/hanjing_1995/article/details/51539563
[cpp] view
plain copy
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream><
4000
span style="margin:0px;padding:0px;border:none;background-color:inherit;">
using namespace std;
//单链表的实现
#include<assert.h>
typedef int DataType;
typedef struct SListNode
{
DataType _data;
struct SListNode* _next;
}SListNode;
SListNode* _CreateNode(DataType x)
{
SListNode* head = (SListNode*)malloc(sizeof(SListNode));
head->_data = x;
head->_next = NULL;
return head;
}
void PushBack(SListNode*& head, DataType x)
{
if (head == NULL)
{
head = _CreateNode(x);
head->_next = NULL;
}
else
{
SListNode* cur = head;
while (cur->_next != NULL)
{
cur = cur->_next;
}
cur->_next = _CreateNode(x);
}
}
void PopBack(SListNode*& head)
{
if (head == NULL)
{
return;
}
else if (head->_next == NULL)
{
free(head);
head = NULL;
}
else
{
SListNode* cur = head;
SListNode* next = head;
while (cur)
{
next = cur->_next;
if (next != NULL && next->_next == NULL)
{
free(next);
cur->_next = NULL;
return;
}
cur = cur->_next;
}
}
}
void PushFront(SListNode*& head,DataType x)
{
if (head == NULL)
{
head = _CreateNode(x);
}
else
{
SListNode* pcur = _CreateNode(x);
pcur->_next = head;
head = pcur;
}
}
void PopFront(SListNode*& head)
{
if (head == NULL)
{
return;
}
else if (head->_next == NULL)
{
free(head);
head = NULL;
}
else
{
SListNode* del = head;
SListNode* next = head->_next;
free(del);
del = NULL;
head = next;
}
}
void Insert(SListNode* head,int pos,DataType x)
{
assert(pos >= 0);
SListNode* cur = head;
while (--pos && cur)
{
cur = cur->_next;
}
if (pos > 0)
{
printf("pos位置大于链表长度!\n");
return;
}
SListNode* newcur = _CreateNode(x);
if (cur->_next)
{
SListNode* next = cur->_next;
cur->_next = newcur;
newcur->_next = next;
}
else if (cur->_next == NULL)
{
cur->_next = newcur;
}
}
size_t Length(SListNode*& head)
{
size_t count = 0;
SListNode* cur = head;
while (cur)
{
count++;
cur = cur->_next;
}
return count;
}
void PrintSList(SListNode*& head)
{
if (head == NULL)
{
return;
}
SListNode* cur = head;
while (cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
}
void Test()
{
SListNode* sList =NULL;
PushBack(sList, 1);
PushBack(sList, 2);
PushBack(sList, 3);
PushBack(sList, 4);
PushBack(sList, 5);
PrintSList(sList);
PopBack(sList);
PrintSList(sList);
PushFront(sList, 0);
PrintSList(sList);
PopFront(sList);
PrintSList(sList);
Insert(sList, 3, 10);
PrintSList(sList);
int ret = Length(sList);
printf("单链表长度为:%d\n", ret);
}
int main()
{
Test();
system("pause");
return 0;
}
[cpp] view
plain copy
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream><
4000
span style="margin:0px;padding:0px;border:none;background-color:inherit;">
using namespace std;
//单链表的实现
#include<assert.h>
typedef int DataType;
typedef struct SListNode
{
DataType _data;
struct SListNode* _next;
}SListNode;
SListNode* _CreateNode(DataType x)
{
SListNode* head = (SListNode*)malloc(sizeof(SListNode));
head->_data = x;
head->_next = NULL;
return head;
}
void PushBack(SListNode*& head, DataType x)
{
if (head == NULL)
{
head = _CreateNode(x);
head->_next = NULL;
}
else
{
SListNode* cur = head;
while (cur->_next != NULL)
{
cur = cur->_next;
}
cur->_next = _CreateNode(x);
}
}
void PopBack(SListNode*& head)
{
if (head == NULL)
{
return;
}
else if (head->_next == NULL)
{
free(head);
head = NULL;
}
else
{
SListNode* cur = head;
SListNode* next = head;
while (cur)
{
next = cur->_next;
if (next != NULL && next->_next == NULL)
{
free(next);
cur->_next = NULL;
return;
}
cur = cur->_next;
}
}
}
void PushFront(SListNode*& head,DataType x)
{
if (head == NULL)
{
head = _CreateNode(x);
}
else
{
SListNode* pcur = _CreateNode(x);
pcur->_next = head;
head = pcur;
}
}
void PopFront(SListNode*& head)
{
if (head == NULL)
{
return;
}
else if (head->_next == NULL)
{
free(head);
head = NULL;
}
else
{
SListNode* del = head;
SListNode* next = head->_next;
free(del);
del = NULL;
head = next;
}
}
void Insert(SListNode* head,int pos,DataType x)
{
assert(pos >= 0);
SListNode* cur = head;
while (--pos && cur)
{
cur = cur->_next;
}
if (pos > 0)
{
printf("pos位置大于链表长度!\n");
return;
}
SListNode* newcur = _CreateNode(x);
if (cur->_next)
{
SListNode* next = cur->_next;
cur->_next = newcur;
newcur->_next = next;
}
else if (cur->_next == NULL)
{
cur->_next = newcur;
}
}
size_t Length(SListNode*& head)
{
size_t count = 0;
SListNode* cur = head;
while (cur)
{
count++;
cur = cur->_next;
}
return count;
}
void PrintSList(SListNode*& head)
{
if (head == NULL)
{
return;
}
SListNode* cur = head;
while (cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("\n");
}
void Test()
{
SListNode* sList =NULL;
PushBack(sList, 1);
PushBack(sList, 2);
PushBack(sList, 3);
PushBack(sList, 4);
PushBack(sList, 5);
PrintSList(sList);
PopBack(sList);
PrintSList(sList);
PushFront(sList, 0);
PrintSList(sList);
PopFront(sList);
PrintSList(sList);
Insert(sList, 3, 10);
PrintSList(sList);
int ret = Length(sList);
printf("单链表长度为:%d\n", ret);
}
int main()
{
Test();
system("pause");
return 0;
}
相关文章推荐
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 单链表的PushBack,PopBack,Insert...函数
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- list(链表)常用成员(顺序容器)----插入push_back,push_front,insert删除pop_back,pop_front,erase,clear遍历begin,end判空em
- list(链表)常用成员(顺序容器)----插入push_back,push_front,insert删除pop_back,pop_front,erase,clear遍历begin,end判空emp
- list(链表)常用成员(顺序容器)----插入push_back,push_front,insert删除pop_back,pop_front,erase,clear遍历begin,end判空empt
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- list(链表)常用成员(顺序容器)----插入push_back,push_front,insert删除pop_back,pop_front,erase,clear遍历begin,end判空emp
- 单链表的基本操作大全之C语言实现(一)
- C语言实现链表的基本操作
- 单向链表基本操作(C语言实现)
- 单链表相关操作之C语言实现:插入,删除,倒转,复制,查找。。。
- 单链表的基本操作大全之C语言实现(二)
- 单链表的基本操作c语言实现
- c语言实现双向链表的基本操作
- C语言实现,无头结点不带环的单向链表的基本操作
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表