笔记6:链表(三种:头指针、头结点、头结点循环)
2017-08-03 20:35
381 查看
1、头指针链表
2、头结点链表
3、头结点循环链表:与头结点链表基本相似
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 typedef int LinkData; // 链表的数据类型 typedef struct _node { LinkData data; // 链表的数据 struct _node *next; // 指向链表下一个结点的指针 }Node; // 链表的头插 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; // 让新节点变为链表的第一个结点 *h = 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; // 指向第一个结点 if (tmp == NULL) // 空表 { *h = node; } else { while (tmp->next) { tmp = tmp->next; } tmp->next = node; } return TRUE; } // 在第 pos 个节点处插入数据,链表结点从1开始,没有第0个结点 int Insert_Pos (Node** h, int pos, LinkData data) { if (h == NULL || pos < 1) return FALSE; // 创建新节点 Node* node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { return FALSE; } // 给结点成员变量赋值 node->data = data; // 空表的状态下,只能插入在第一个结点处 if (*h == NULL) { if (pos != 1) { printf ("当前为空表,无法在第 %d 结点处插入数据\n", pos); free(node); return FALSE; } node->next = NULL; *h = node; } else // 非空表,需要找到插入位置的前一个结点 { if (pos == 1) { node->next = *h; *h = node; } else { int i; Node *tmp = *h; // tmp 开始的时候指向第一个结点 for (i = 0; i < pos-2; i++) { if (tmp == NULL) // 如果 pos 太大,会造成越界 break; tmp = tmp->next; } if (tmp == NULL) { printf ("插入位置越界\n"); free(node); return FALSE; } node->next = tmp->next; tmp->next = node; } } return TRUE; } int Delete_Pos(Node** h, int pos) { if (h == NULL || *h == NULL || pos < 1) return FALSE; Node *tmp = *h; if (pos == 1) { *h = tmp->next; free(tmp); } else { int i; for (i = 0; i < pos-2; i++) { if (tmp->next == NULL) // 如果 pos 太大,会造成越界 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 == NULL 空表 (*h)->next == NULL 只有一个元素 if (h == NULL || *h == NULL || (*h)->next == NULL) return FALSE; Node *pre = *h; Node *cur = (*h)->next; Node *tmp; while (cur) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } (*h)->next = NULL; *h = pre; return TRUE; } void Display(Node *h) { if (h == NULL) return; int count = 0; while (h) { if (count++ % 4 == 0) printf ("\n"); printf ("%8d", h->data); h = h->next; } printf ("\n"); } int main() { Node * head = NULL; // 指向链表第一个结点的指针(头指针) // 插入元素 int i; for (i = 0; i < 10; i++) { //Insert_Head(&head, i); Insert_Last(&head, i); } #if 0 Insert_Pos(&head, 1, 1000); Insert_Pos(&head, 10, 2000); Insert_Pos(&head, 13, 3000); Insert_Pos(&head, 15, 3000); Insert_Pos(&head, 0, 1000); #endif //Delete_Pos(&head, 11); Display(head); Reverse_List(&head); Display(head); return 0; }
2、头结点链表
#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #define FALSE 0 #define TRUE 1 typedef int LinkData; typedef struct _node { LinkData data; struct _node * next; }Node; // 创建链表 Node * Create_List(); // 尾插 int Insert_Last(Node *h, LinkData data); // 头插 int Insert_Head(Node *h, LinkData data); // 在第 pos 个结点处插入数据 int Insert_Pos(Node *h, int pos, LinkData data); //删除 第 pos 个结点 int Delete_Pos(Node* h, int pos); // 逆序 int Reverse_List(Node *head); // 删除指定数据 int Delete_Data(Node*, LinkData data); // 查找元素:如果有, 返回元素的位置 int Find_Element(Node* h, LinkData data, int *x); // 获取顺序表中的元素:通过位置获取 int Get_Element(Node* s, int pos, int *x); int Get_Len(Node * head); // 清空所有结点 int Clean_List(Node * head); // 销毁链表 int Destroy(Node *); void Display(Node *h); #endif
#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; } // 在第 pos 个结点处插入数据 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) 4000 { 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 ("%8d", tmp->data); tmp = tmp->next; } printf ("\n"); } int Destroy(Node *h) { if (h == NULL) return FALSE; Clean_List(h); free(h); return TRUE; }
3、头结点循环链表:与头结点链表基本相似
#include "LinkList.h" #include <stdlib.h> #include <stdio.h> // 创建链表 Node * Create_List() { Node *head = (Node*)malloc(sizeof(Node)/sizeof(char)); if (head == NULL) return NULL; head->next = head; // 空表 return head; } // 头插 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 = h; Node* tmp = h; while (tmp->next != h) { tmp = tmp->next; } tmp->next = node; return TRUE; } // 在第 pos 个结点处插入数据 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 == h) break; tmp = tmp->next; } if (tmp == h) // 越界 { 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; } //删除 第 pos 个结点 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 == h) break; tmp = tmp->next; } if (tmp->next == h) // 越界 { 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 == h || h->next->next == h) return FALSE; Node *pre = h->next; Node *cur = h->next->next; Node *tmp; while (cur == h) { tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } h->next->next = h; h->next = pre; return TRUE; } // 删除指定数据 int Delete_Data(Node* h, LinkData data) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next != h) { if (tmp->next->data == data) break; tmp = tmp->next; } if (tmp->next == h) 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 != h) { 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 == h) break; tmp = tmp->next; } if (tmp == h) 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 != h) { count++; tmp = tmp->next; } return count; } // 清空所有结点 int Clean_List(Node * h) { if (h == NULL) return FALSE; Node *tmp = h; while (tmp->next != h) { Delete_Pos(h, 1); } return 0; } // 打印输出 void Display(Node *h) { if (h == NULL) return; int count = 0; Node *tmp = h->next; while (tmp != h) { if (count++ % 4 == 0) printf ("\n"); printf ("%8d", tmp->data); tmp = tmp->next; } printf ("\n"); } // 销毁链表 int Destroy(Node *h) { if (h == NULL) return FALSE; Clean_List(h); free(h); return TRUE; }
相关文章推荐
- 【C++数据结构学习笔记---线性表】带头结点的双向循环链表
- 指针p,q,r依次指向某循环链表中三个相邻的结点,交换结点*q,*r在表中次序的程序段是
- 假设以带头结点的循环链表表示队列, 并且只设一个指针指向队尾元素结点(注意不设头指针) 试编写相应的队列初始化,入队列和出队列的算法
- 数据结构学习笔记(二) 链表之带表头结点的单循环链表基本操作
- 假设以不带头结点的循环链表表示队列,并且只设一个指针指向队尾结点,但不设头指针。试设计相应的入队和出队的算法
- 假设在长度大于1的循环链表中,即无头结点也无头指针,s为指向链表中的某个结点的指针,试编写算法删除结点s的前驱结点
- 单循环链表(头指针指在头结点)『3』
- 雅虎面试题─将两个双向循环链表中data值相同的结点删除
- 线性表学习归纳总结五:不带头结点的循环链表
- 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
- 两个仅设表尾指针的循环链表的合并(教科书图2.13)
- 数据结构 学习笔记之:线性表——顺序表、静态链表、动态链表(单链表、双链表、单循环链表、双循环链表,链式栈、链式队列)——基础概念大扫盲!
- 关于链表中头指针和头结点的理解
- 结构之美:单链表的头结点与头指针
- 数据结构题集(严蔚敏)2.37 改造带头结点的双循环链表
- 有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
- 数据结构学习笔记(二) 链表之带表头结点的双循环链表基本操作
- 13\给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点,
- 链表、头指针、头结点
- java 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。