头节点链表
来源:互联网 发布:宁夏网络教育培训学院 编辑:程序博客网 时间:2024/06/05 00:42
LinkList.h
#ifndef __LINKLIST_H__#define __LINKLIST_H__#define FALSE 0#define TRUE 1typedef 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
LinkList.c
#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 ("%8d", tmp->data);tmp = tmp->next;}printf ("\n");}int Destroy(Node *h){if (h == NULL)return FALSE;Clean_List(h);free(h);return TRUE;}
main.c
#include <stdio.h>#include "LinkList.h"int main(){// 创建链表Node* head = Create_List();if (head == NULL){printf("创建链表失败\n");return -1;}int i;for (i = 0; i < 10; i++){Insert_Head(head, i);}#if 0for (i = 0; i < 10; i++){Insert_Head(head, i);}Insert_Pos(head, 5, 1000);Insert_Pos(head, 1, 2000);Insert_Pos(head, 22, 2000);Insert_Pos(head, 24, 2000);Insert_Pos(head, 26, 2000);Delete_Pos (head, 21);Reverse_List(head);#endifDelete_Data(head, 0);int x;Find_Element(head, 7, &x);Get_Element(head, 8, &x);printf ("%d\n", x);printf ("%d\n", Get_Len(head));Display(head);Clean_List(head);printf ("清空后:\n");Display(head);return 0;}
阅读全文
0 0
- 头节点链表
- 头节点循环链表
- 链表中的头指针和头节点
- 删除一个没有头节点链表中的某个节点
- 单向无头节点链表操作
- 单向有头节点链表
- 双向有头节点链表
- 有头节点单向循环链表
- 有头节点双向循环链表
- 头节点链表C程序
- 数据结构链表 --头节点链表复习
- 数据结构之链表(头节点链表)
- 约瑟夫环的链表实现-有头节点
- 数据结构 《3》----双向链表(带有头节点)
- 约瑟夫问题(没有头节点的循环链表)
- 删除没有头结点的链表指点节点操作
- 简单数据结构之链表(无头节点)
- 简单数据结构之链表(有头节点)
- python3线程启动与停止
- 我所理解的二维数组
- 前端初学学习进程IV
- SpringBoot学习(五)操作数据库Spring-Data-JPA
- Xcode报错: no visible @interface for XXX declares…
- 头节点链表
- 爬取163新闻的函数
- DHCP协议浅析
- 实现两个窗口通信方法-postMessage
- 初窥AOP实例
- git工具的使用
- 【笔试题】拼多多2018校招内推编程
- 心路
- FTP浅析