C语言 线性表之单链表的相关操作
一、什么是单链表?
线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以存在于内存中未被占用的任意位置,
比起顺序存储结构,在链式存储结构中,除了要存储数据元素信息外,还要存储它的后继元素的存储地址,即指针。
我们把存储数据元素信息的域成为数据域,把存储后继结点地址的域称为指针域。两部分信息组成的数据元素成为结点(Node)。
二、单链表结构示意图。
三、单链表的基本操作
(1)结构体定义:
1 typedef struct node{ 2 int data; 3 struct node *next; 4 }Lnode;//可直接用Lnode *p定义一个结点
(2)初始化链表:
1 void InitLnode(Lnode *&l){//初始化链表 2 l=(Lnode *)malloc(sizeof(Lnode)); 3 l->next=null; 4 }
(3)输出链表:
1 void DispLnode(Lnode *l){//输出链表 2 Lnode *p; 3 p=(Lnode *)malloc(sizeof(Lnode)); 4 p=l->next; 5 while(p!=null){ 6 printf("%d ",p->data); 7 p=p->next; 8 } 9 printf("\n"); 10 }
(4)取链表中某个位置的值:
1 bool GetElem(Lnode *l,int i,ElemType *e){//取链表中的值 2 Lnode *p; 3 int j=1; 4 p=(Lnode *)malloc(sizeof(Lnode)); 5 p=l->next; 6 while(p!=null && j<i){ 7 p=p->next; 8 j++; 9 } 10 if(i<1 || l->next==null)return false;//参数错误或链表为空时返回false 11 *e=p->data;//取元素值 12 return true; 13 }
(5)某个位置插入值:
1 bool LnodeInsert(Lnode *l,int i,ElemType e){//插入数据元素 2 Lnode *p,*q; 3 p=(Lnode *)malloc(sizeof(Lnode)); 4 q=l; 5 int j=1; 6 while(q!=null && j<i){ 7 q=q->next; 8 j++; 9 } 10 if(q==null || j>i) return false; 11 p->data=e; 12 p->next=q->next; 13 q->next=p; 14 return true; 15 }
(6)删除数据元素
1 bool LnodeDelete(Lnode *l,int i,ElemType *e){//删除数据元素 2 Lnode *q,*p; 3 q=l; 4 int j=1; 5 while(q->next!=null && j<i){ 6 q=q->next; 7 j++; 8 } 9 if(!(p->next) || j>i) return false; 10 p=q->next; 11 q->next=p->next; 12 *e=p->data; 13 free(p); 14 return true; 15 }
(7)尾插法创建单链表
1 void CreateLnode(Lnode *head){ 2 Lnode *q,*p; 3 q=(Lnode *)malloc(sizeof(Lnode)); 4 q=head; 5 int i; 6 for(i=0;i<5;i++){ 7 p=(Lnode *)malloc(sizeof(Lnode)); 8 p->data=i+1; 9 q->next=p; 10 q=p; 11 } 12 p->next=null;//单链表尾指针设为空 13 }
(8)头插法创建单链表
1 void CreateHeadLnode(lnode *head){//头插法建立链表 2 lnode *p,*q; 3 q=head; 4 q->next=null; 5 for(int i=0;i<10;i++){ 6 p=(lnode *)malloc(sizeof(lnode)); 7 p->data=i+1; 8 p->next=q->next; 9 q->next=p; 10 } 11 }
代码如下:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define null 0 5 #define MAXSIZE 100 6 typedef int ElemType; 7 typedef struct node{ 8 int data; 9 struct node *next; 10 }Lnode; 11 void InitLnode(Lnode *&l){//初始化链表 12 l=(Lnode *)malloc(sizeof(Lnode)); 13 l->next=null; 14 } 15 void DispLnode(Lnode *l){//输出链表 16 Lnode *p; 17 p=(Lnode *)malloc(sizeof(Lnode)); 18 p=l->next; 19 while(p!=null){ 20 printf("%d ",p->data); 21 p=p->next; 22 } 23 printf("\n"); 24 } 25 bool GetElem(Lnode *l,int i,ElemType *e){//取链表中的值 26 Lnode *p; 27 int j=1; 28 p=(Lnode *)malloc(sizeof(Lnode)); 29 p=l->next; 30 while(p!=null && j<i){ 31 p=p->next; 32 j++; 33 } 34 if(i<1 || l->next==null)return false;//参数错误或链表为空时返回false 35 *e=p->data;//取元素值 36 return true; 37 } 38 bool LnodeInsert(Lnode *l,int i,int e){//插入数据元素 39 Lnode *p,*q; 40 p=(Lnode *)malloc(sizeof(Lnode)); 41 q=l; 42 int j=1; 43 while(q!=null && j<i){ 44 q=q->next; 45 j++; 46 } 47 if(q==null || j>i) return false; 48 p->data=e; 49 p->next=q->next; 50 q->next=p; 51 return true; 52 } 53 bool LnodeDelete(Lnode *l,int i,ElemType *e){//删除数据元素 54 Lnode *q,*p; 55 q=l; 56 int j=1; 57 while(q->next!=null && j<i){ 58 q=q->next; 59 j++; 60 } 61 if(!(q->next) || j>i) return false; 62 p=q->next; 63 q->next=p->next; 64 *e=p->data; 65 free(p); 66 return true; 67 } 68 bool ChangeLnode(Lnode *l,int i,ElemType e){ 69 Lnode *p; 70 p=l->next; 71 int j=1; 72 while(p!=null && j<i){ 73 p=p->next; 74 j++; 75 } 76 if(p==null) return false; 77 p->data=e; 78 return true; 79 } 80 void CreateTailLnode(Lnode *head){//尾插法建立单链表 81 Lnode *q,*p; 82 q=(Lnode *)malloc(sizeof(Lnode)); 83 q=head; 84 int i; 85 for(i=0;i<10;i++){ 86 p=(Lnode *)malloc(sizeof(Lnode)); 87 p->data=i+1; 88 q->next=p; 89 q=p; 90 } 91 p->next=null; 92 }//CreateTailLnode(head) 93 void CreateHeadLnode(Lnode *head){//头插法建立链表 94 Lnode *p,*q; 95 q=head; 96 q->next=null; 97 for(int i=0;i<10;i++){ 98 p=(Lnode *)malloc(sizeof(Lnode)); 99 p->data=i+1; 100 p->next=q->next; 101 q->next=p; 102 } 103 }//CreateHeadLnode(head) 104 int main(){ 105 Lnode *l; 106 ElemType e; 107 printf("链式表的基本操作如下:\n"); 108 InitLnode(l); 109 LnodeInsert(l,1,1); 110 LnodeInsert(l,2,2); 111 LnodeInsert(l,3,3); 112 LnodeInsert(l,4,4); 113 LnodeInsert(l,5,5); 114 DispLnode(l); 115 GetElem(l,1,&e); 116 printf("链表的第一个元素是:%d\n",e); 117 GetElem(l,2,&e); 118 printf("链表的第二个元素是:%d\n",e); 119 LnodeDelete(l,2,&e); 120 printf("删除的元素为%d\n",e); 121 DispLnode(l); 122 LnodeDelete(l,1,&e); 123 printf("删除的元素为%d\n",e); 124 DispLnode(l); 125 GetElem(l,3,&e); 126 printf("链表的第三个元素为:%d\n",e); 127 LnodeInsert(l,1,1); 128 DispLnode(l); 129 LnodeInsert(l,3,4); 130 DispLnode(l); 131 printf("替换第二个元素数据:"); 132 ChangeLnode(l,2,6); 133 DispLnode(l); 134 }
运行结果为:
大一下刚接触数据结构课程,了解到算法的重要性后再来系统地学习,欢迎指正交流!!