单链表(表头结点)
2017-08-04 20:38
211 查看
#include <stdio.h> #include <stdlib.h> #define OK 0 #define ERROR -1 #define MALLOC_ERROR -2 typedef int ElementType; //c语言中没有ElementType这个数据类型,讲数据结构时,用这个来泛指某一种数据类型 typedef struct node { ElementType data; //结点的数据 struct node *next; //结点指针 }Node; typedef Node *PNode; //重命名结点指针类型 //头插法创建链表 int Create_List_Head(PNode h, ElementType data) { if (h == NULL) { return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; node->next = h->next; h->next = node; return OK; } //尾插法创建链表 int Create_List_Tail(PNode h, ElementType data) { if (h == NULL) { return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; node->next = NULL; //找最后一个结点 PNode temp = h; while (temp->next) { temp = temp->next; } temp->next = node; return OK; } //在第pos个结点处插入一个数据 int Insert_Node(PNode h, int pos, ElementType data) { PNode temp = h; int k = 0; //找pos-1个结点 while (temp && k < pos-1) { temp = temp->next; k++; } if (temp == NULL) //越界 { printf ("无效的插入点\n"); return ERROR; } PNode node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return MALLOC_ERROR; } node->data = data; node->next = temp->next; temp->next = node; return OK; } //在链表中删除第pos个结点 int Delete_Node(PNode h, int pos) { PNode temp = h; int k = 0; //找到删除的结点前一个结点,就是temp while (temp && k < pos-1) { temp = temp->next; k++; } if (temp == NULL || temp->next == NULL) //越界 { printf ("删除位置越界\n"); return ERROR; } PNode p = temp->next; temp->next = p->next; free(p); p = NULL; return OK; } //链表逆序 int Inverse_List(PNode h) { //h == NULL 头结点开辟失败,为空 //h->next == NULL 空表 //h->next->next == NULL 只有一个结点 if (h->next == NULL || h->next->next == NULL) { return OK; } PNode pre = h->next; PNode cur = pre->next; PNode next = NULL; while(cur) { next = cur->next; cur->next = pre; pre = cur; cur = next; } /* h->next->next == NULL; //两个等于号!!!,将原来的头指针的指针变为尾指针置空 h->next = pre; //将头结点指向现在的第一个结点,重置头结点的next return OK; */ h->next->next = NULL; h->next = pre; //将头结点指向现在的第一个结点,重置头结点的next return OK; } //查找链表中的元素,找到后返回该结点的指针 PNode Search(PNode h, ElementType data) { if (h == NULL) { return NULL; } PNode temp = h->next; while(temp) { if (temp->data == data) { return temp; } temp = temp->next; } return NULL; } //计算链表的长度 int ListLen(PNode h) { int len = 0; PNode temp = h->next; while(temp) { temp = temp->next; len++; } return len; } //打印 void Display(PNode h) { if (h == NULL) { return; } PNode temp = h->next; //链表第一个结点指针 while (temp) { printf ("%4d",temp->data); temp = temp->next; } printf ("\n"); } int main() { PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char)); if (head_node == NULL) { return MALLOC_ERROR; } head_node->next = NULL; //空链表 int i = 0; for (i = 0; i < 10; i++) { /* //头插法创建链表 if (Create_List_Head(head_node,i) != OK) { return ERROR; } */ //尾插法创建链表 if (Create_List_Tail(head_node,i) != OK) { return ERROR; } } /* //在第pos个结点处插入一个数据 if (Insert_Node(head_node, 6, 7) != OK) { return ERROR; } //在链表中删除第pos个结点 if (Delete_Node(head_node,4) != OK) { return ERROR; } */ //将链表逆序 if (Inverse_List(head_node) != OK) { return ERROR; } Display(head_node); //查找元素 PNode p = Search(head_node, 5); if(p == NULL) { printf("无此结点\n"); } else { printf("%d\n",p->data); } //计算长度 int len = ListLen(head_node); printf("%d\n",len); return 0; }
相关文章推荐
- 数据结构学习笔记(二) 链表之带表头结点的单链表及其基本操作
- 带表头结点的双向循环链表
- 递增输出带表头结点的单链表元素
- 带表头结点的双向循环链表
- 带表头结点的单链表HeaderList
- 带表头结点的双向循环链表
- 带表头结点的链表进行栈操作
- 设ha和hb分别是两个带头节点的费递减有序单链表的表头指针,设计以算法,将将两个有序链表合成一个非递减的有序单链表,该程序以以前发表的博客中的链表中的区别在与该立案表带有头结点
- 不带表头结点的单向链表总结
- 带表头结点的双向循环链表
- 数据结构学习笔记(二) 链表之带表头结点的单循环链表基本操作
- 带表头结点的链表进行栈操作(练习)
- 数据结构学习笔记(二) 链表之带表头结点的双循环链表基本操作
- 从键盘读入n个整数(升序),请编写算法实现: 建立带表头结点的单链表; 显示单链表,(形如:H->10->20->30->40); 在有序单链表中插入新的数据元素x; 将单链表就地逆
- 初始化链表的四种方法(有无头结点以及表头(尾)插入法)
- 创建带表头结点的单向链表,并实现各种功能
- 不带表头结点的非循环单向链表
- 无表头结点的有序环形链表的合并
- C语言单链表的基本操作(带表头结点)
- 带表头结点的单向链表