链表
一种链式结构,特点:可以不按连续空间存储数据,插入和删除数据速度更快;但查找数据需要一个个遍历。
1 单向链表
-
结构体定义
typedef struct node{ int num; struct node *next; } Link;
-
初始化
/* 初始化链表 */ void InitList(Link* l) { l->next = NULL; }
-
插入
/* 在指定位置插入结点 */ void InsertNode(Link* l,int num,int pos) { Link *head = l; //定义头指针 Link *insert = (Link *)malloc(sizeof(Link)); if(!insert){ printf("Error:No enough memory to allocate!\n"); return; }else{ if(!head->next){ //列表为空 插入第一个结点 insert->num = num; insert->next = NULL; head->next = insert; head = head->next; } else{ int i; for (i = 0; i < pos;++i){ if(!head->next){ printf("Error:Please input the right pos!\n"); return; }else{ head = head->next; } } insert->num = num; insert->next = head->next; head->next = insert; } } }
-
删除
/* 删除一个结点 */ void delList(Link* l,int pos) { Link *head = l; Link *temp = NULL; if(!head->next){ printf("Error:The List is empty ...\n"); return; }else{ int i; for (i = 0; i < pos;++i){ head = head->next; } temp = head->next; head->next = temp->next; free(temp); } }
-
修改
/* 修改结点数据 */ void editorElm(Link* l,int num,int pos) { int i; Link *head = l; for (i = 0; i <= pos;++i) { if(head->next == NULL){ printf("Error:Please input the right pos!\n"); return; }else{ head = head->next; } } head->num = num; }
-
查找
/* 查找元素 */ int findElm(Link* l,int target) { int pos; Link *head = l; for (pos = 0;head->next;++pos){ if(head->num == target){ return pos; }else{ head = head->next; } } return -1; }
-
打印
void Print(const Link* l) { while(l->next) { l = l->next; printf("%d ", l->num); } printf("\n"); }
2 其他
注意事项
为了避免出现分段错误,必须特别小心指针的使用。
-
第一种情况:链表初始化函数有返回值。
-
第二种情况:链表初始化函数没有返回值。
int* ptr = NULL;
/* NULL 表示的地址是0x00000000,这个是系统内存的地址,不可以使用,即,当定义ptr指向NULL后,不可以再写ptr = 3,否则会造成分段错误*/
3 双向链表
-
代码参考
#include<stdio.h> #include<stdlib.h> //定义结点 typedef struct node{ int data; struct node *pre, *next; } List; //定义bool类型 typedef enum Bool{ False = 0,True } BOOL; //初始化 void listInit(List* l){ l->pre = NULL; l->next = NULL; } //插入元素 void inElm(List* l,int data,int pos){ List *head = l; List *in = (List *)malloc(sizeof(List)); if(!head->next){//列表为空 in->data = data; in->pre = head; in->next = head->next; head->next = in; }else{ in->data = data; int i; for (i = 0; i < pos;++i){ if(!head){ printf("Error: Please input the right pos\n"); return ; } else head = head->next; } in->next = head->next; in->pre = head; head->next = in; } } //删除元素 void delElm(List *l,int pos){ List *head = l; int i; for (i = 0; i < pos;++i){ if(!head){ printf("Error:Please input the right pos!\n"); return; } else head = head->next; } head->pre->next = head->next; head->next->pre = head->pre; free(head); } //查找元素 int findElm(const List *l,int compData){ int i = 0; while(l->next){ l = l->next; if(l->data == compData) return i; i++; } return -1; } //更改元素 void editElm(List *l, int newData,int pos) { int i = 0; while(l->next){ l = l->next; if(i == pos){ l->data = newData; break; } i++; } printf("Error: Please input the right pos!\n"); } //打印整个链表 void Print(const List *l){ while(l->next){ l = l->next; printf("%d ", l->data); } printf("\n"); } int main(){ List *l = (List *)malloc(sizeof(List)); listInit(l); // int i; List *head = l; List *in = NULL; for (i = 0; i < 5;++i){ in = (List *)malloc(sizeof(List)); in->data = 2 * i + 1; in->next = head->next; in->pre = head; head->next = in; head = head->next; } Print(l); inElm(l, 2, 0); inElm(l, 90, 6); Print(l); delElm(l, 3); Print(l); int pos = findElm(l, 2); if(pos == -1){ printf("Not Found!\n"); } else{ printf("The pos is:%d\n", pos); } editElm(l, 80, 3); Print(l); system("pause"); return 0; }