List basis to achieve operation

Implementing content:
1, the first drop node
2, node tail plug
3, the first point abridged element
4, the end point abridged element
5, after the specified position of the insertion node element
6, after deleting the node element specified position
7, to find a node element
8, empty the list
to achieve a:
create a node structure and list structure

typedef int Type;
//节点结构体
typedef struct Node {
 Type data;
 struct Node* next;
}Node;
//链表结构体:存放节点元素
typedef struct Slist {
 Node* _head;
}Slist;
//创建节点元素函数
Node* BuySListNode(Type x) {
 Node* sl = (Node*)malloc(sizeof(Node)) ;
 sl->data = x;
 sl->next = NULL;
 return sl;
}

Achieve two:
initialization list, the first plug, the end plug, enables the printing of:

//初始化
void SListInit(Slist* plist) {
 plist->_head = NULL;
}
// 单链表打印
void SListPrint(Slist* plist){
 Node* s = plist->_head;
 while (s) {
  printf("%d  ", s->data);
  s = s->next;
 }
 printf("\n");
}
// 单链表尾插
void SListPushBack(Slist* pplist, Type x) {
 //创建节点元素
 Node* node = BuySListNode(x);
 //如果是空表
 if (pplist->_head == NULL) {
  pplist->_head = node;
  node->next = NULL;
 }
 //表里有元素
 else {
  Node* cur = pplist->_head;
  while (cur->next) {
   cur = cur->next;
  }
  cur->next = node;
  node->next = NULL;
 } 
}
// 单链表的头插
void SListPushFront(Slist* pplist, Type x) {
 Node* node = BuySListNode(x);
 //空表插入
 if (pplist->_head == NULL) {
  pplist->_head = node;
 }
 //有元素状况
 else {
  node->next = pplist->_head;
  pplist->_head = node;
 }
}

To achieve three:
the first puncturing, puncturing the tail implemented;

// 单链表的尾删
void SListPopBack(Slist* pplist) {
 Node* pre = NULL;
 Node* tail = pplist->_head;
 //1、空或者只有一个节点
 //2、两个及以上各节点
 if (tail == NULL || tail->next == NULL) {
  free(tail);
  pplist->_head = NULL;
 }
 else {
  while (tail->next) {
   pre = tail;
   tail = tail->next;
  }
  free(tail);
  tail = NULL;
  pre->next = NULL;
 }
}
// 单链表头删
void SListPopFront(Slist* pplist) {
 //没有元素则不操作,否则依次删
 if (pplist->_head) {
  Node* cur = pplist->_head;
  pplist->_head = pplist->_head->next;
  free(cur);
  cur = NULL;
 }
}

Achieve four:
After you specify the location to insert, delete node elements to achieve:

// 单链表在pos位置之后插入x
void SListInsertAfter(Node* pos, Type x){
 assert(pos);
 Node* next = pos->next;
 Node* newNode = BuySListNode(x);
 pos->next = newNode;
 newNode->next = next;
}
// 单链表删除pos位置之后的值
void SListEraseAfter(Node* pos) {
 //必须有两个及以上的节点元素
 assert(pos && pos->next);
 Node* next = pos->next;
  pos->next = next->next;
  free(next);
  next = NULL;
}

Achieve five:
Find, empty achieve operation:

Node* SListFind(Slist* plist, Type x) {
 Node* cur = plist->_head;
 while (cur) {
  if (cur->data == x) {
   return cur;
  }
  cur = cur->next;
 }
 return NULL;
}
// 单链表的销毁
void SListDestory(Slist* plist) {
 while(plist->_head) {
  Node* cur = plist->_head;
  plist->_head = plist->_head->next;
  free(cur);
  cur = NULL;
 }
}
Published 40 original articles · won praise 2 · Views 589

Guess you like

Origin blog.csdn.net/scacacac/article/details/105206863