C語言單鏈表(線性表的鏈式儲存)實現程式碼
阿新 • • 發佈:2019-01-08
linklist.h //標頭檔案:資料 及 函式的宣告
#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include <stdio.h> #include <stdlib.h> typedef int datatype; //連結串列中儲存的資料型別可以在標頭檔案中規定好,後續如果需要更改連結串列中儲存的資料型別,直接修改這裡的資料型別即可。此處以 int 型為例。 typedef struct node{ //單鏈表很簡單,每個節點兩個資料成員:需要儲存的資料 和 緊跟該節點後面的一個節點的地址 datatype data; struct node *next; }listnode,*linklist; extern linklist list_create(); //單鏈表的建立 extern linklist list_create2(); //單鏈表的建立(功能同上) extern int list_head_insert(linklist H,datatype value); //新建資料節點並且插入到頭結點的後面 extern int list_insert(linklist H,int pos,datatype value); //新建資料節點按照指定位置(pos)插入到連結串列中 extern int list_delete(linklist H,int pos); //刪除連結串列中指定位置的節點 extern linklist list_get(linklist H,int pos); //獲取連結串列中指定位置的節點 extern linklist list_locate(linklist H,datatype value); //獲取連結串列中資料等於value的第一個節點 extern void list_reverse(linklist H); //連結串列倒置(逆序) extern void list_show(linklist H); //連結串列節點資料顯示(遍歷) extern int list_order_insert(linklist H,datatype value); //單鏈表的有序插入(從小到大排列) extern void list_sort(linklist H); //單鏈表重新排序(從小到大排序) extern void list_free(linklist H); //釋放單鏈表 #endif
linklist.c //函式的具體實現,可根據自己的需要適當的新增 或 修改函式
#include "linklist.h" linklist list_create() //單鏈表的建立 { linklist H; if((H=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc failed!\n"); return H; } H->data = 0; H->next = NULL; return H; } linklist list_create2() //單鏈表的建立(功能同上一個函式) { linklist H,r,p; int value; if((H=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc failed!\n"); return H; } H->data = 0; H->next = NULL; r=H; while(1) { printf("input a number(-1 exit):"); scanf("%d",&value); if(value == -1) break; if((p=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc failed\n"); return H; } p->data = value; p->next = NULL; r->next = p; r=p; } return H; } int list_head_insert(linklist H,datatype value) //新建資料節點並且插入到頭結點的後面 { linklist p; if((p=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc failed\n"); return -1; } p->data = value; p->next = H->next; H->next = p; return 0; } int list_insert(linklist H,int pos,datatype value) //新建資料節點按照指定位置(pos)插入到連結串列中 { linklist p,q; if(pos==0) p=H; else p=list_get(H,pos-1); if(p==NULL) { printf("para is invalid\n"); return -1; } else { if((q=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc failed\n"); return -1; } q->data = value; q->next = p->next; p->next = q; return 0; } } int list_delete(linklist H,int pos) //刪除連結串列中指定位置的節點 { linklist p,q; if(pos == 0) p=H; else p=list_get(H,pos-1); if(p==NULL || p->next==NULL) { printf("para is invalid\n"); return -1; } else { q=p->next; p->next=q->next; free(q); q=NULL; return 0; } } linklist list_get(linklist H,int pos) //獲取連結串列中指定位置的節點 { linklist p=H; int i=-1; if(pos<0) { printf("position is invalid:<0\n"); return NULL; } while(p->next && i<pos) { p=p->next; i++; } //if(p->next) if(i==pos) { return p; } else { printf("position is invalid: > length\n"); return NULL; } } linklist list_locate(linklist H,datatype value) //獲取連結串列中資料等於value的第一個節點 { linklist p=H->next; while(p&&p->data !=value) { p=p->next; } return p; } void list_reverse(linklist H) //連結串列倒置(逆序) { linklist p,q; p=H->next; H->next = NULL; while(p) { q=p; p=p->next; q->next = H->next; H->next = q; } } int list_order_insert(linklist H,datatype value) //單鏈表的有序插入(從小到大排列) { linklist p,q; if((p=(linklist)malloc(sizeof(listnode)))==NULL) { printf("malloc faied\n"); return -1; } p->data = value; q=H; while(q->next&&q->next->data < value) { q=q->next; } p->next = q->next; q->next = p; return 0; } void list_show(linklist H) //連結串列節點資料顯示(遍歷) { while(H->next) { printf("%d ",H->next->data); H = H->next; } printf("\n"); } void list_sort(linklist H) //單鏈表重新排序(從小到大排序) { linklist p,q,r; p=H->next; H->next = NULL; while(p) { q=p; p=p->next; r=H; while(r->next && r->next->data < q->data) r=r->next; q->next = r->next; r->next = q; } } void list_free(linklist H) //釋放單鏈表 { linklist p,q; p = H->next; while(p) { q = p->next; free(p); p = q; } free(H); }