動作を実現するために、リストの基礎

コンテンツ実装:
1、最初のドロップノード
2、ノード尾栓
3、最初の点簡略要素
4を、エンドポイントは、要素簡略
挿入ノード要素の指定された位置の後に、5
ノード要素を削除して位置指定した後、6
ノード要素を見つけるために、7
8、リストを空にする
達成するために:
ノード構造やリスト構造を作成します

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;
}

両者を達成する:
初期化リスト、第一プラグ、端部プラグの印刷を可能にします。

//初始化
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;
 }
}

3を達成するために:
最初の穿刺を、実装尾を穿刺します。

// 单链表的尾删
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;
 }
}

4実現:
あなたが挿入する場所を指定した後、ノード削除要素は、達成するために:

// 单链表在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;
}

5を達成:
動作を実現空にする、検索:

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;
 }
}
公開された40元の記事 ウォンの賞賛2 ビュー589

おすすめ

転載: blog.csdn.net/scacacac/article/details/105206863