数据结构链表
来源:互联网 发布:php全选 编辑:程序博客网 时间:2024/05/22 07:08
/*linklist.h*/#ifndef _LINKLIST_H_#define _LINKLIST_H_#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0#define OK 0#define ERROR -1;typedef int ElemType;typedef struct SNode{ ElemType data; struct SNode *next; }Node,*Node_Ptr;typedef struct SList{ Node* head; int length;}LinkList;//便于理解定义链表类型/*函数声明*/ElemType Input();void Init(LinkList *list);void Insert(LinkList* list, int pos, ElemType e);void Delete(LinkList* list, ElemType e);void PrintElem(ElemType e);void Print(LinkList list);void Clear(LinkList* list);void Destory(LinkList* list);int IsEmpty(LinkList list);int GetLength(LinkList list);/*函数定义*//*初始化链表*/void Init(LinkList *list){ Node* pHead = (Node*)malloc(sizeof(ElemType)); if(!pHead) { printf("error:malloc is NULL\n"); exit(-1); } pHead->next = NULL; list->head = pHead; list->length = 0;}/*获取链表长度,返回int,也就是长度值*/int GetLength(LinkList list){ return list.length;}/*返回int型,TRUE表示链表为NULL,FALSE表示链表非空*/int IsEmpty(LinkList list){ if(list.length == 0) { return TRUE; } else { return FALSE; }}/*打印单个元素*/void PrintElem(ElemType e){ printf("%-4d",e);}/*打印链表*/void Print(LinkList list){ if( IsEmpty(list) ) { printf("List is Empty\n"); return; } Node* p = list.head; p = p->next; while(p != NULL) { PrintElem(p->data); p = p->next; } printf("\n"); }/*插入元素,参数1:传入链表地址,参数2:插入位置,参数3:插入元素值*/void Insert(LinkList* list, int pos, ElemType e ){ if(pos < 1) pos = 1; else if( pos > list->length) pos = list->length+1; Node* p = list->head; while(--pos) { p = p->next; } Node* pNew = (Node*)malloc(sizeof(ElemType)); pNew->data = e; pNew->next = NULL; if(p != NULL) { pNew->next = p->next; p->next = pNew; } else { p->next = pNew; } list->length++; return;}/*删除元素,参数1:传入链表地址,参数2:要删除的元素值*/void Delete(LinkList* list, ElemType e){ Node* p = list->head; Node* q = p->next; while(q != NULL && q->data !=e ) { p = p->next; q = q->next; } if(q == NULL) { printf("No Found this element \n"); return; } p->next = q->next; free(q); q = NULL; list->length--;}/*输入单个元素,返回值为元素类型用于接收元素*/ElemType Input(){ printf("input Element:"); ElemType e = 0; scanf("%d",&e); getchar(); return e;}/*清空链表元素*/void Clear(LinkList* list){ Node* p = list->head; Node* q = p->next; while(q != NULL) { p->next = q->next; free(q); q=p->next; list->length--; //Print(*list); }}/*销毁链表,链表不在可用,如果要继续使用,需要再次初始化*/void Destory(LinkList* list){ Clear(list); Node* p = list->head; free(p); p = NULL; printf("List is destoried\n");}#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
/*main.c用于测试*/#include <stdio.h>#include <stdlib.h>#include <stdlib.h>#include "LinkList.h"int main(int argc, char *argv[]){ int i = 0, pos = 0; ElemType e = 0; LinkList list; Init(&list); for(i=0;i<10;i++) { pos = rand()%10; e = i; printf("insert e=%d to pos=%d\t",e,pos); Insert(&list, pos, e); Print(list); } printf("list length %d\n",GetLength(list)); //删除值为5的元素 printf("after delete Element's value is 5\n"); Delete(&list, 5); Print(list); printf("list length %d\n",GetLength(list)); //再一次删除5的元素,无法找到。 printf("again delete Element's value is 5\n"); Delete(&list, 5); printf("list length %d\n",GetLength(list)); Destory(&list); return 0;}
阅读全文
0 0
- 数据结构-01-链表数据结构之单链表
- 数据结构之链表
- 数据结构-线性链表
- 数据结构 循环链表
- 线性链表---数据结构
- java数据结构链表
- 数据结构之链表
- 数据结构-实现链表
- 数据结构链表队列
- 【数据结构】静态链表
- 数据结构-链表队列
- 数据结构 线性链表
- 数据结构《顺序链表》
- 数据结构:块状链表
- 数据结构:块状链表
- 数据结构:静态链表
- 数据结构--双向链表
- 数据结构----链表队列
- AI在医疗行业的最新进展
- 计算机视觉简介:历史、现状和发展趋势
- 科技产品下一个重大突破将来自芯片堆叠技术
- 增加网站点击(引流)的不外传技巧
- 微软为什么要公开AI系统测试数据集和度量指标?
- 数据结构链表
- jQuery的节点操作
- ng-options如何应用到多维数组
- Android APK常驻内存解决方案
- hello World Spring Boot
- Remove Element:删除数组中指定元素
- linux内核驱动之helloWord本地驱动
- python 游标、MySQL连接池、设计表
- Detecting Text in Natural Image with Connectionist Text Proposal Network