C语言实现链表

来源:互联网 发布:云计算和云存储的区别 编辑:程序博客网 时间:2024/06/05 11:21

定义

     链表是一种非连续、非顺序的存储结构,数据元素的顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。


结构组成
     链表的每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。

优缺点

     使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。    

链表有很多种不同的类型:单向链表,双向链表以及循环链表。


复杂度
     链表的复杂度分析:由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。


一、数组实现链表

#include#include#include#include#includeusing namespace std;#define SpaceSize 10struct Node {int value;int next;}node;struct Node CursorSpace[SpaceSize];void InitCursorSpace(void);int isEmpty(int L);int isLast(int p,int L);int Find(int x,int L);int FindPre(int x,int L);void Insert(int x,int L,int p);void Delete(int x,int L);int Header(const int L);int First(const int L);int Advance(const int p);int Retrive(const int p);voidInitCursorSpace(void){int i;for (i = 0; i <= SpaceSize; i++) {CursorSpace[i].value = i;CursorSpace[i].next = i % 10;}}static intCursorAlloc(){int p;p = CursorSpace[0].next;CursorSpace[0].next = CursorSpace[p].next;return p;}static voidCursorFree(int p){CursorSpace[p].next = CursorSpace[0].next;CursorSpace[0].next = p;}intisEmpty(int L){return CursorSpace[L].next == 0;}int isLast(int p, int L){return CursorSpace[p].next == 0;}int Find(int x, int L){int p;p = CursorSpace[L].next;while (p && CursorSpace[p].value != x)p = CursorSpace[p].next;return p;}void Delete(int x, int L){int p, temp;p = FindPre(x,L);if (!isLast(p, L)){temp = CursorSpace[p].next;CursorSpace[p].next = CursorSpace[temp].next;CursorFree(temp);}}void Insert(int x, int L, int p){int temp;temp = CursorAlloc();if (temp == NULL)printf("out of space");CursorSpace[temp].value = x;CursorSpace[temp].next = CursorSpace[p].next;CursorSpace[p].next = temp;}int FindPre(int x, int L){int temp;if (isEmpty(L))return -1;temp = L;L = CursorSpace[L].next;while (L && CursorSpace[L].value != x) {temp = L;L = CursorSpace[L].next;}return temp;}int Header(const int L){if (isEmpty(L))return -1;elsereturn L;}int First(const int L){if (isEmpty(L))return -1;elsereturn CursorSpace[L].value;}

二、指针实现链表

#include#include#include#include#includeusing namespace std;#define QUEUE_TYPE inttypedef struct STRUCT_NODE {QUEUE_TYPE value;QueueNode *next;}QueueNode;static QueueNode *front;static QueueNode *rear;void destroy_queue();void qInsert(QUEUE_TYPE value);void qDelete();QUEUE_TYPE first();int isEmpty();int isFull();voiddestroy_queue(){while (!isEmpty())qDelete();}voidqInsert(QUEUE_TYPE value){QueueNode *new_node;new_node = (QueueNode*)malloc(sizeof(QueueNode));assert(new_node!=NULL);new_node->value = value;new_node->next = NULL;if (rear == NULL)front = new_node;elserear->next = new_node;rear = new_node;}voidqDelete(void){QueueNode *next_node;assert(!isEmpty());next_node = front->next;free(front);front = next_node;if (front == NULL)rear = NULL;}QUEUE_TYPEfirst(){assert(!isEmpty());return front->value;}int isEmpty(){return front == NULL;}int isFull(){return 0;}