国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言之單向鏈表詳解及實例代碼

2020-01-26 14:25:58
字體:
來源:轉載
供稿:網友

1,單向鏈簡潔。

單向鏈表(單鏈表)是鏈表的一種,其特點是鏈表的鏈接方向是單向的,對鏈表的訪問要通過順序讀取從頭部開始;鏈表是使用指針進行構造的列表;又稱為結點列表,因為鏈表是由一個個結點組裝起來的;其中每個結點都有指針成員變量指列表中的下一個結點;列表是由結點構成,由head指針指向第一個成為表頭的結點而終止于最后一個指向nuLL的指針;

2,例子要求:

根據示例代碼中的例子,完成單向鏈表(single linked list)中的以字符串為數據的鏈表的插入、刪除以及查找,并支持單向鏈表的反轉;

3,代碼實現。

#include <stdio.h>#include <math.h>#include <cstring>#include <memory.h>#include <malloc.h>//節點的定義typedef struct Node { void *data; //數據域 //鏈域 struct Node *next;} NodeStruct, *pNode;pNode head = NULL; typedef char (*pCompareFunc)(void *a, void *b);typedef void* (*pChar)(void *p);// 字符串判斷int str_compare(void *a, void *b) { char *pa = (char*)a; char *pb = (char*)b; return strcmp(pa , pb);}// 分配一個節點pNode allocate_node(void *data, pChar char_func) { pNode node = allocate(); node->data = char_func(data); return node;}// 創建節點pNode allocate() { void *p = malloc(sizeof(NodeStruct)); pNode node = (pNode)p; node->next = NULL; node->data = NULL; return node;}// 添加一個節點void insertNode(pNode node){ if (head == null){  head=node;  }  else {  node->next = head;  head = node;  }}void* char_char(void *p) { char* pa = (char*)malloc(sizeof(char)); memcpy(pa, p, sizeof(char)); return pa;}// 初始化節點pNode allocate_node(void *data, pChar char_func) { pNode node = allocate(); node->data = char_func(data); return node;}    // 釋放資源void free_list(pNode node) { pNode next = node; while (next != NULL) {  if (next->data != NULL)   free(next->data);  pNode temp = next;  next = next->next;  free(temp); }}// 1.1 添加一個節點void insert(pNode node) { if (head == NULL)  head = node; else {  node->next = head;  head = node; }}//1.2查找int str_search(void* data,pCompareFunc compare){ pNode next = head; pNode prev = NULL; while (next != NULL ) {  if (compare(data, next->data) == 0) {   // 如果查找到了,就退出返回1   return 1;   break;  }  prev = next;  next = next->next; } // 如果一直查找不到,就返回0 return 0;}// 1.3刪除節點void remove(void* data,pCompareFunc compare) { pNode next = head; pNode prev = NULL; while (next != NULL) {  if (compare(data, next->data) == 0) {   if (prev == NULL) {    head = next->next;    next->next = NULL;    free_list(next);   } else {    prev->next = next->next;    next->next = NULL;    free_list(next);   }   break;  }  prev = next;  next = next->next; }}//1.4反轉void invert_order(){  node *This,*prev;  p=head.next;  This=NULL;  while(p) {    prev=This;    This=p;    p=p->next;    This->next=prev;  }  head.next=This;}   void main(){ // 1單向鏈表 char a1[] = 'aaa1'; char a2[] = 'aaa2'; char a3[] = 'aaa3'; // 1.1添加 insertNode(allocate_node(a1, init_char)); insertNode(allocate_node(a2, init_char)); insertNode(allocate_node(a3, init_char)); // 1.2查找 int flag = 0; flag = str_search(&a2,str_compare); // 1.3刪除 remove(&a2,str_compare); // 1.4反轉  invert_order();} 

 以上就是對 C語言單向聯表的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 临武县| 湘潭市| 汉源县| 宁强县| 永顺县| 峨眉山市| 互助| 寿宁县| 邳州市| 太仆寺旗| 博兴县| 葫芦岛市| 东台市| 石楼县| 财经| 井研县| 和平县| 昆山市| 惠水县| 湟源县| 多伦县| 盱眙县| 五常市| 汝阳县| 松潘县| 安岳县| 上思县| 东乡| 武冈市| 金乡县| 双城市| 道孚县| 布尔津县| 淮安市| 远安县| 滦南县| 凭祥市| 台安县| 化州市| 吉水县| 宁强县|