头结点链表C语言

xiaoxiao2021-02-28  60

今天把头结点链表又复习了一波,虽然还不能脱稿盲打,基本是理解了,后面会坚持敲下去把他敲熟咯。

首先我们需要写一下头文件,后面的函数需要调用他分开写为了方便易懂,也就是我们写的.h文件

#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

头文件写完了就要开始写.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 ("
转载请注明原文地址: https://www.6miu.com/read-67139.html

最新回复(0)