c语言单链表的实现,自己编的C语言单链表的实现

看了下C primer plus的最后一章以及c和指针中讲解链表的部分,就动手写了个链表的功能实现程序,用纯C编写,在vc环境下,因此文件后缀为cpp。

38700b6976dee1cb778013eeab838c18.png

头文件sll_node.h

#include

#include

struct NODE

{

int value;

struct NODE *next;

};

typedef struct NODE Node;

//这里的root是在link_main文件中的定义的,同时应用于input.cpp文件,

//该文件中没有定义指针root,

//其结果是在调用过input函数之后,root值改变,不再为NULL;

extern Node *root;

bool sell_insert(Node **,int ); //插入节点函数

void show(Node *); //显示链表数据函数

void input_data(Node **); //输入链表数据函数

void free_list(Node **); //释放链表内存函数

unsigned int count(Node *); //链表节点计数函数

bool check(Node *,int); //检查输入的数据是否在聊表中已经存在的函数

//链表排序函数——冒泡法,不改变节点位置,只改变节点中数据的存储位置

void sort(Node *);

void delete_data(Node **,int); //删除节点函数

子函数文件node.c

#include "sll_node.h"

//遍历链表——检查输入的数据是否与链表中的数据重复

bool

check(Node *root,int value)

{

Node *current;

current = root;

while(current != NULL)

{

if(current->value != value)

current = current->next;

else

break;

}

if(current == NULL)

return true; //输入的数据没有与链表中的数据重复,返回true

else

return false;

}

//删除节点函数

void

delete_data(Node **phead,int delete_value)

{

Node *current;

Node *previous;

Node *next;

Node *pt;

current = *phead;

previous = NULL;

while(current->value != delete_value)

{

previous = current;

current = current->next;

next = current->next;

}

if(previous == NULL)

*phead = current->next;

else

previous->next = next;

pt = current;

free(pt);

}

//输入函数

void

input_data(Node **phead)

{

int value;

Node *current,*prev;

root = *phead;

puts("Enter the first value(q to end):");

while(scanf("%d",&value)==1)

{

while(getchar() != '\n')

continue;

current = (Node *)malloc(sizeof(Node));

if(root == NULL)

root = current;

else

prev->next = current;

current->next = NULL;

current->value = value;

puts("Enter the next value(q to end):");

prev = current;

}

}

//插入节点函数

bool

sell_insert(Node **phead,int new_value)

{

Node *current;

Node *previous;

Node *newn;

current = *phead;

previous = NULL;

while(current != NULL && current->value < new_value)

{

previous = current;

current = current->next;

}

newn = (Node *)malloc(sizeof(Node));

if(newn == NULL)

return false;

newn->value = new_value;

newn->next = current;

if(previous==NULL)

*phead = newn;

else

previous->next = newn;

return true;

}

//节点计数函数

unsigned int

count(Node *root)

{

unsigned int count = 0;

while(root != NULL)

{

++count;

root = root->next;

}

return count;

}

//显示函数

void

show(Node *root)

{

Node *current;

if(root == NULL)

printf("No data entered.");

current = root;

while(current != NULL)

{

printf(" %d ",current->value);

current = current->next;

}

}

//冒泡法排序链表函数——不改变节点位置,只改变节点中数据的存储位置void

sort(Node *root)

{

Node *current;

Node *s;

for(current = root;current->next != NULL;current = current->next)

{

for(s = root;s->next != NULL;s = s->next)

{

if(s->value > s->next->value)

{

int temp;

temp = s->value;

s->value = s->next->value;

s->next->value = temp;

}

}

}

}

//释放节点内存函数

void

free_list(Node **phead)

{

Node *psave;

while(*phead != NULL)

{

psave = (*phead)->next;

free(*phead);

*phead = psave;

}

}

主函数实现文件link_main.c

#include "sll_node.h"

Node *root = NULL;

int main()

{

//输入数据,分配内存,并输入原始序列和排序后的序列

input_data(&root);

if(root== NULL)

printf("No data entered.");

else

{

printf("Here is the list:\n");

show(root);

printf("\nThe sorted list is:\n");

sort(root);

show(root);

printf("\nTotal itms of the list is:\n %d",count(root));

}

//输入需要添加的数据

while(getchar() != '\n')

continue;

//这两句不可少,防止scanf因最后一次读取到的是字母而永远不会越过它

int new_value;

printf("\nNow enter the new value:\n");

scanf("%d",&new_value);

while(getchar() != '\n')

continue;

//检查待添加的数据是否在链表中存在,若存在,提示重新输入,直到不存在为止

while(!check(root,new_value) )

{

printf("Existed in the list ,please input another data:");

scanf("%d",&new_value);

while(getchar() != '\n')

continue;

}

//将待添加的数据加入新建的节点,并分配内存,插入链表,显示插入后的序列

if(sell_insert(&root,new_value))

{

printf("\nNow the list is:\n");

show(root);

printf("\nNow total itms of the list is:\n %d",count(root));

}

else

printf("insert error\n");

printf("\n");

//输入待删除的数据

int delete_value;

printf("\nNow enter the deleted value:\n");

scanf("%d",&delete_value);

while(getchar() != '\n')

continue;

//如果待删除的数据不在链表中,提示重新输入,知道存在为止

while(check(root,delete_value) )

{

printf("Not exist in the list ,please input a data in the list:");

scanf("%d",&delete_value);

while(getchar() != '\n')

continue;

}

//将待删除的数据所在的节点所占用的内存释放掉,即删除掉,重新链接链表,并显示删除数据后的序列

delete_data(&root,delete_value);

printf("\nNow the list is:\n");

show(root);

printf("\nNow total itms of the list is:\n %d",count(root));

printf("\n");

//释放链表所占用的内存

free_list(&root);

return 0;

}

运行结果:

e42756c34e1a017abdf2087d779d876a.png

关注我的新浪博客:http://blog.sina.com.cn/u/1921993171

关注我的新浪微博:http://weibo.com/u/1921993171?wvr=5&wvr=5&lf=reg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值