链表基础(创建、插入、删除)

来源:互联网 发布:淘宝合并购物车 编辑:程序博客网 时间:2024/05/29 02:09
链表是一种常见的重要的数据结构。它是动态地进行存储分配的一种结构,它可以根据需要开辟内存单元;链表有一个“头指针”变量,以head表示,它存放一个地址;该地址指向一个元素。链表中每一个元素称为“结点”,每个结点都应包括两个部分:一为用户需要用的实际数据,二为下一个结点的地址;因此,head指向第一个元素:第一个元素又指向第二个元素,类推……;直到最后一个元素,该元素不再指向其它元素,它称为“表尾”,它的地址部分放一个“NULL”(表示“空地址”),链表到此结束;
===========头文件===========#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 ==============功能函数=========#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;     //三句话 node->date=date,node->next=NULL,tmp_last->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;}                       //跳出循环的时候tmp可能是null,也可能不是,所以下面一句需要判断似否为null;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;}//删除第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 == NULL)break;tmp = tmp->next;}if(tmp == NULL){printf("删除位置越界\n");return FALSE;}Node *p;p = tmp->next;tmp->next = p->next;free(p);p =NULL;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;}//此刻cur =null;  pre是第一个节点;h->next->next=NULL;h->next = pre;return TRUE;}//删除指定的数据int Delete_Data(Node* h,LinkData data){if(h == NULL)return 0;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);p = NULL;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;return *x;}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;}// 销毁链表int Destroy(Node *h){if (h == NULL)return FALSE;Clean_List(h);free(h);return TRUE;}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");}=============主函数===========#include <stdio.h>#include "LinkList.h"int main(){    Node* head = Create_List();//head指向创建的空表;if (head == NULL){printf("创建链表失败\n");return -1;}int i;for (i = 0; i < 10; i++){Insert_Head(head, i);}#if 0//尾部插入for (i = 0; i < 10; i++){Insert_Last(head, i);}//pos节点插入数据;Insert_Pos(head, 5, 1000);    //删除pos节点;    Delete_Pos(head, 1);        //逆序    Reverse_List(head);//删除指定的数据;Delete_Data(head, 3);#endif#if 0// 查找元素:如果有, 返回元素的位置int x;Find_Element(head, 7, &x);printf ("       元素的位置为%d\n", x);#endif// 获取顺序表中的元素:通过位置获取int x;    Get_Element(head, 2,&x);printf("       此位置获取的元素是:%d\n",x);//链表长度printf("       链表长度是:%d\n",Get_Len(head));Clean_List(head);//清除节点;Display(head);//销毁节点;Display(head);return 0;}

原创粉丝点击