单链表的实现及其基本操作
结点的引入
链表是一种链式存储结构,链式存储结构的特点是用一组任意的存储单元存储数据元素。为了能正确表示数据元素之间的线性关系,需引入结点概念。一个结点表示链表中的一个数据元素,节点中除了储存数据元素的信息, 还必须存放指向下一个节点的的指针(单、双链表的最后一个节点除外,它们存储的是一个空指针NULL)
结点的结构如下图所示:
代码如下:
1 typedef struct node{ 2 int data; 3 struct node* pNext; 4 }Node, *PNode;
注:这里假设结点中储存的是整型 (int) 的数据
单链表由多个结点依次连接而成,我们不难想象出它结构:
我们注意到:在第一个结点的前面多了一个头结点,这是为了处理空表的方便而引入的,它的指针指向链表的第一个结点,而它的data域不存放任何信息。
单链表的基本操作
1.创建链表
1 PNode createList() 2 { 3 int len, value; 4 5 PNode pHead = (PNode)(malloc(sizeof(Node))); 6 PNode pTail = pHead; 7 pTail->pNext = NULL; 8 9 printf("请输入你要的节点个数:"); 10 scanf("%d", &len); 11 for(int i=1;i<=len;i++){ 12 printf("请输入第%d个节点的值:", i); 13 scanf("%d", &value); 14 15 PNode pNew = (PNode)malloc(sizeof(Node)); 16 pNew->data = value; 17 pTail->pNext = pNew; 18 pTail = pNew; 19 pTail->pNext = NULL; 20 } 21 22 return pHead; 23 }
2.遍历链表
void traverse(PNode pHead) { printf("遍历结果为:\n"); PNode pTra = pHead; while(pTra->pNext != NULL) { printf("%d ", pTra->pNext->data); pTra = pTra->pNext; } printf("\n"); }
3.判断链表是否为空
1 bool isEmpty(PNode pHead) 2 { 3 if(pHead->pNext==NULL) 4 return true; 5 else 6 return false; 7 }
4.链表长度
1 int length(PNode pHead) 2 { 3 int len = 0; 4 while(pHead->pNext!=NULL){ 5 pHead = pHead->pNext; 6 len++; 7 } 8 return len; 9 10 }
5.插入结点
1 bool insert(PNode pHead, int pos, int val) 2 { 3 if(pos<1 || pos>length(pHead)){ 4 return false; 5 }else{ 6 PNode pInsert = pHead; 7 for(int i=1;i<pos;i++){ 8 pInsert = pInsert->pNext; 9 } 10 11 PNode pNew = (PNode)malloc(sizeof(Node)); 12 pNew->data = val; 13 pNew->pNext = pInsert->pNext; 14 pInsert->pNext = pNew; 15 16 return true; 17 } 18 19 }
6.删除结点
1 bool del(PNode pHead, int pos) 2 { 3 if(pos<1 || pos>length(pHead)){ 4 return false; 5 }else{ 6 PNode pDel = pHead; 7 for(int i=1;i<pos;i++){ 8 pDel = pDel->pNext; 9 } 10 11 if(pos==length(pHead)){ 12 free(pDel->pNext); 13 pDel->pNext = NULL; 14 }else{ 15 PNode pNext = pDel->pNext->pNext; 16 free(pDel->pNext); 17 pDel->pNext = pNext; 18 } 19 20 return true; 21 22 } 23 24 25 }
7.查找节点
(1)按元素值查找
1 PNode locate(PNode pHead, int value) 2 { 3 PNode p = pHead->pNext; 4 while(p&&p->data!=value){ //NULL 是 0 5 p = p->pNext; 6 } 7 return p; 8 }
(2)按序号查找
1 PNode get(PNode pHead, int k) 2 { 3 PNode p = pHead; 4 for(int i=1;i<=k;i++){ 5 p = p->pNext; 6 } 7 return p; 8 9 }
完整代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef struct node{ 4 int data; 5 struct node* pNext; 6 }Node, *PNode; 7 8 PNode createList(); 9 void traverse(PNode pHead); 10 bool isEmpty(PNode pHead); 11 int length(PNode pHead); 12 bool insert(PNode pHead, int pos, int val); 13 bool del(PNode pHead, int pos); 14 PNode get(PNode pHead, int k); //按序号查找 15 PNode locate(PNode pHead, int value);//按值查找 16 17 int main(void) 18 { 19 //test 20 21 return 0; 22 } 23 24 PNode createList() 25 { 26 int len, value; 27 28 PNode pHead = (PNode)(malloc(sizeof(Node))); 29 PNode pTail = pHead; 30 pTail->pNext = NULL; 31 32 printf("请输入你要的节点个数:"); 33 scanf("%d", &len); 34 for(int i=1;i<=len;i++){ 35 printf("请输入第%d个节点的值:", i); 36 scanf("%d", &value); 37 38 PNode pNew = (PNode)malloc(sizeof(Node)); 39 pNew->data = value; 40 pTail->pNext = pNew; 41 pTail = pNew; 42 pTail->pNext = NULL; 43 } 44 45 return pHead; 46 } 47 48 49 void traverse(PNode pHead) 50 { 51 printf("遍历结果为:\n"); 52 PNode pTra = pHead; 53 while(pTra->pNext != NULL) 54 { 55 printf("%d ", pTra->pNext->data); 56 pTra = pTra->pNext; 57 } 58 printf("\n"); 59 } 60 61 bool isEmpty(PNode pHead) 62 { 63 if(pHead->pNext==NULL) 64 return true; 65 else 66 return false; 67 } 68 69 int length(PNode pHead) 70 { 71 int len = 0; 72 while(pHead->pNext!=NULL){ 73 pHead = pHead->pNext; 74 len++; 75 } 76 return len; 77 78 } 79 80 bool insert(PNode pHead, int pos, int val) 81 { 82 if(pos<1 || pos>length(pHead)){ 83 return false; 84 }else{ 85 PNode pInsert = pHead; 86 for(int i=1;i<pos;i++){ 87 pInsert = pInsert->pNext; 88 } 89 90 PNode pNew = (PNode)malloc(sizeof(Node)); 91 pNew->data = val; 92 pNew->pNext = pInsert->pNext; 93 pInsert->pNext = pNew; 94 95 return true; 96 } 97 98 } 99 100 bool del(PNode pHead, int pos) 101 { 102 if(pos<1 || pos>length(pHead)){ 103 return false; 104 }else{ 105 PNode pDel = pHead; 106 for(int i=1;i<pos;i++){ 107 pDel = pDel->pNext; 108 } 109 110 if(pos==length(pHead)){ 111 free(pDel->pNext); 112 pDel->pNext = NULL; 113 }else{ 114 PNode pNext = pDel->pNext->pNext; 115 free(pDel->pNext); 116 pDel->pNext = pNext; 117 } 118 119 return true; 120 121 } 122 123 124 } 125 126 PNode get(PNode pHead, int k) 127 { 128 PNode p = pHead; 129 for(int i=1;i<=k;i++){ 130 p = p->pNext; 131 } 132 return p; 133 134 } 135 PNode locate(PNode pHead, int value) 136 { 137 PNode p = pHead->pNext; 138 while(p&&p->data!=value){ //NULL 是 0 139 p = p->pNext; 140 } 141 return p; 142 }