线性表的定义
定义n个数据元素的有限序列,记作(a1, a2, …, an)ai 是表中数据元素,n 是表长度 线性表的特点 1. 除第一个元素外,其他每一个元素有一个且仅有一个直接前驱。2. 除最后一个元素外其他每一个元素有一个且仅有一个直接后继。
顺序表的定义和特点 定义 将线性表中的元素相继存放在一个连续的存储空间中。 可利用一维数组描述存储结构 特点 线性表的顺序存储方式 遍历 顺序访问, 可以随机存取
前插法建立单链表 :
从一个空表开始,重复读入数据: 生成新结点 将读入数据存放到新结点的数据域中 将该新结点插入到链表的前端直到读入结束符为止。
后插法建立单链表:
每次将新结点加在链表的表尾; 设置一个尾指针 r,总是指向表中最后一个结点,新结点插在它的后面;尾指针 r 初始时置为指向表头结点地址。
顺序表与链表的比较:
基于空间的比较 存储分配的方式 顺序表的存储空间是静态分配的 链表的存储空间是动态分配的 存储密度 = 结点数据本身所占的存储量/结点结构所占的存储总量 顺序表的存储密度 = 1 链表的存储密度 < 1
基于时间的比较 存取方式 顺序表可以随机存取,也可以顺序存取 链表是顺序存取的 插入/删除时移动元素个数 顺序表平均需要移动近一半元素 链表不需要移动元素,只需要修改指针 若插入/删除仅发生在表的两端,宜采用带尾指针的循环链表
下面给大家奉上对链表进行各种操作的代码
#include "LinkList.h" #include <stdlib.h> #include <stdio.h> Node * Create_List() { Node *list = (Node*)malloc(sizeof(Node)/sizeof(char)); if (list == NULL) return NULL; list->next = NULL; // 空表 return list; } int Insert_Head(Node *h, LinkData data) { if (h == NULL) return FALSE; Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = h->next; h->next = node; return TRUE; } int Insert_Last(Node *h, LinkData data) { if (h == NULL) return FALSE; Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = NULL; Node* tmp = h; while (tmp->next) { tmp = tmp->next; } tmp->next = node; return TRUE; } int Insert_Pos(Node *h, int pos, LinkData data) { if (h == NULL || pos < 1) return FALSE; // 找要插入位置的前一个结点 Node *tmp = h; int i; for (i = 0; i < pos-1; i++) { if (tmp == NULL) break; tmp = tmp->next; } if (tmp == NULL) // 越界 { printf("插入位置越界\n"); return FALSE; } Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } node->data = data; node->next = tmp->next; tmp->next = node; return TRUE; } int Delete_Pos(Node* h, int pos) { if (h == NULL || pos < 1) return FALSE; // 找要插入位置的前一个结点 Node *tmp = h; int i; for (i = 0; i < pos-1; i++) { if (tmp->next == NULL) break; tmp = tmp->next; } if (tmp->next == NULL) // 越界 { printf("删除位置越界\n"); return FALSE; } Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Reverse_List(Node *h) { // h->next 空表 // h->next->next 只有一个结点 if (h == NULL || h->next == NULL || h->next->next == NULL) return FALSE; Node *pre = h->next; Node *cur = h->next->next; Node *tmp; while (cur) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } h->next->next = NULL; h->next = pre; return TRUE; } int Delete_Data(Node* h, LinkData data) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next) { if (tmp->next->data == data) break; tmp = tmp->next; } if (tmp->next == NULL) return FALSE; Node *p = tmp->next; tmp->next = p->next; free(p); return TRUE; } int Find_Element(Node* h, LinkData data, int *x) { if (h == NULL) return FALSE; Node *tmp = h->next; int k = 1; while (tmp) { if (tmp->data == data) { *x = k; return TRUE; } k++; tmp = tmp->next; } return FALSE; } int Get_Element(Node* h, int pos, int *x) { if (h == NULL || pos < 1) return FALSE; int i; Node *tmp = h; for (i = 0; i < pos; i++) { if (tmp == NULL) break; tmp = tmp->next; } if (tmp == NULL) return FALSE; else *x = tmp->data; return TRUE; } int Get_Len(Node * h) { if (h == NULL) return 0; Node *tmp = h; int count = 0; while (tmp->next) { count++; tmp = tmp->next; } return count; } int Clean_List(Node * h) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next) { Delete_Pos(h, 1); } return 0; } void Display(Node *h) { if (h == NULL) return; int count = 0; Node *tmp = h->next; while (tmp) { if (count++ % 4 == 0) printf ("\n"); printf ("