c语言单向链表的基本功能的实现
2017-07-29 20:27
363 查看
1.单链表整体以结构提为框架,为了简单实现基本功能,里面的结点数据元素就定义为一个int data 型元素和next指针
具体如下:
接下来给出测试部分文件test.c,里面主要是主函数和测试函数
如果有什么问题,欢迎大家留言一起学习交流!
具体如下:
typedef struct Node { DataType data;//数据 struct Node *next;//指针 }Node;首先给Lists.h出头文件部分,主要是头文件和功能函数的声明
#define _CRT_SECURE_NO_WARNINGS 1接下来给出具体实现函数的文件部分Lists.c
#ifndef __LISTS_H__
#define __LISTS_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int DataType;
typedef struct Node { DataType data;//数据 struct Node *next;//指针 }Node;
void InitLists(Node **head);//初始化
void destory(Node **head);//free开辟的结点
void pushback(Node **head, int d);//从尾部插入
void diaplay(Node *head);//打印
void popback(Node **head);//从尾部删除
void pushfront(Node **head, int d);//从首部插入
void popfront(Node **head);//从首部删除
Node *Listfind(const Node *head, const int locate);//指定位置查找
void Listinsert(Node **head, int locate,int d);//指定位置插入
void Erase(Node **head, int locate);//指定位置删除
void Remove(Node **head, int d);//删除相同结点元素的第一个结点
void remove_all(Node **head, int d);//删除相同指定结点元素所在的所有结点
#endif
#define _CRT_SECURE_NO_WARNINGS 1 #include"Lists.h" void InitLists(struct Node **head)//二级指针接受指针变量的地址 { *head = NULL;//头指针指向空 } void pushback(Node **head, int d)//插尾函数 { Node *cur = *head;//*head表示指针head里面的内容 Node *p =(Node *) malloc(sizeof(Node));//开辟头结点 memset(p, 0, sizeof(Node));//初始化头结点 p->data = d;//给结点里面赋值 p->next = NULL;//头结点指向的下一个结点为NULL,因为还没有开辟 if (cur == NULL) { *head = p;//将头结点的地址赋值给头指针 } else { while (cur->next != NULL) { cur = cur->next; } cur->next = p; } } void diaplay(Node *head)//打印函数 { Node *cur = head;//将头指针指向的内容给cur while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } } void popback(Node **head)//从尾删除 { Node *str = NULL; Node *cur = *head; if (*head == NULL) return; while (cur->next != NULL) { str = cur; cur = cur->next; } if (str != NULL) { str->next = NULL; free(cur); } else { free(cur); *head = NULL; } } void pushfront(Node **head, int d)//从头插入 { assert(*head); Node *cur = *head;//*head表示指针head里面的内容 Node *p = (Node *)malloc(sizeof(Node));//开辟头结点 memset(p, 0, sizeof(Node));//初始化头结点 p->data = d;//给结点里面赋值 p->next = NULL;//头结点指向的下一个结点为NULL,因为还没有开辟 p->next = *head;//开辟的新结点里面 的next指向之前的旧结点 *head = p;//把新开辟的结点的地址给头指针 } void popfront(Node **head)//从头删除 { Node *str = NULL; Node *cur = *head; if (*head == NULL) return; *head= cur->next; free(cur); } Node *Listfind(const Node *head,const int locate)//查找指定位置的结点 { assert(head); if (head == NULL) return; int count = 1; Node *cur = head;//指向第一个结点cout赋值为1; while (cur!=NULL&&count<locate) { cur = cur->next; count++; } if (cur == NULL) return; else return cur; } void Listinsert(Node **head, int locate,int d)//指定位置插入结点和结点元素 { assert(*head); int count = 1; Node *cur = *head; Node *p = (Node *)malloc(sizeof(Node));//申请节点 memset(p, 0, sizeof(Node)); p->data = d; p->next = NULL; while (count < locate - 1) { cur = cur->next; count++; } if (locate == 1) { p->next = *head; *head = p; return; } p->next = cur->next;//把第locate的地址给p->next,把p的地址给locate-1的next cur->next = p; } void Erase(Node **head, int locate)//删除指定的结点 { assert(*head); if (*head == NULL) return; int count =1; Node *cur = *head; while (count < locate-1) { cur = cur->next; count++; } if (locate == 1) { *head = cur->next; free(cur); return; } Node *p = cur->next;//cur->next是locate的位置 cur->next = p->next;//cur->next里面存的是第四个的地址,p->next第四个 free(p); } void Remove(Node **head, int d)//删除指定重复结点元素的出现的第一个元素 { assert(*head); int count = 1; int n = 1; Node *cur = *head;//cur里面是第一个元素 Node *str= *head;//cur里面是第一个元素 while (cur!= NULL) { if (cur->data == d&&count==1)//要删除结点出现在第一个的位置上面的时候 { *head=cur->next; free(cur); return; } if (cur->data == d) { while (n <count-1) { str = str->next; n++; } str->next = cur->next; free(cur); return; } cur = cur->next; count++; } exit(EXIT_FAILURE); } void remove_all(Node **head, int d)//删除所有重复的结点 { assert(*head); int count = 1; int n = 1; Node *cur = *head;//cur里面是第一个元素 Node *str = *head;//cur里面是第一个元素 flag: while (cur != NULL) { if (cur->data == d&&count == 1)//要删除结点出现在第一个的位置上面的时候 { *head = cur->next; free(cur); cur = *head; str = *head; count = 1; n = 1; goto flag; } if (cur->data == d) { while (n <count - 1) { str = str->next; n++; } str->next = cur->next; free(cur); cur = *head;//删除一个以后要重新赋值 str = *head; count = 1; n = 1; goto flag;//goto回去重新开始计算 } cur = cur->next; count++; } } void destory(Node **head)//free链表函数释放 { assert(*head); Node *cur = *head; while (cur!= NULL) { *head = cur;//从前向后释放 cur=cur->next; free(*head); } }
接下来给出测试部分文件test.c,里面主要是主函数和测试函数
#define _CRT_SECURE_NO_WARNINGS 1 #include"Lists.h" void test() { struct Node *head;//头指针 InitLists(&head);//初始化需要将头指针赋值成为NULL,所以将指针的地址传过去 pushback(&head,1); pushback(&head, 2); //pushback(&head, 4); //pushback(&head, 2); //pushback(&head, 4); //pushback(&head, 2); //pushback(&head, 4); diaplay(head);//把头指针传给 printf("\n"); destory(&head);//free所有结点 /*Remove(&head, 2);*/ //remove_all(&head, 2);//删除所有重复的2结点 diaplay(head);//把头指针传给 //printf("\n"); //Listinsert(&head,1, 10); //Erase(&head, 1); //pushfront(&head, 1); //pushfront(&head, 2); //pushfront(&head, 3); //pushfront(&head, 4); //popfront(&head); //popfront(&head); //popfront(&head); //diaplay(head);//把头指针传给 //printf("\n"); //popback(&head); //diaplay(head);//把头指针传给 //printf("\n"); //printf("%d",Listfind(head, 1)->data); } int main() { test(); system("pause"); return 0; }注意:记得在测试最后面使用destory();函数来释放所动态开辟的结点
如果有什么问题,欢迎大家留言一起学习交流!
相关文章推荐
- C语言数据结构之单向链表(已经调试可以实现相应的功能了,可是还是有几个问题现在还是不大理解,希望大家能够一起探讨)
- 算法与数据结构-单向链表的基本操作C语言实现
- Java实现单向链表基本功能
- Java实现单向链表的基本功能详解
- 单向链表的C语言实现与基本操作
- C语言实现,无头结点不带环的单向链表的基本操作
- 单向链表基本操作(C语言实现)
- 实现单向链表的基本功能
- C语言实现链表之单向链表(九)在任意位置插入结点
- C语言单链表实现19个功能完全详解(完整例程)
- Python实现的单向循环链表功能示例
- C语言 链表的基本操作实现 源码
- C语言单向链表的实现
- 创建带表头结点的单向链表,并实现各种功能
- C语言数据结构_单链表_19个功能实现全解
- 链表基本操作的C语言简单实现
- 单链表的基本操作C语言实现
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 单向链表逆置的功能实现
- 数据结构——单向链表的基本操作C语言描述(克服对头结点和尾结点的操作)