c语言单链表存储的源程序,C语言单链表各操作源代码

/*my_list.h*/

/*author:li

data:13/08/13*/

#ifndef MY_LIST_H_

#define MY_LIST_H_

typedef struct stu

{

int num;

char name[20];

struct stu* next;

}stu_info;

typedef stu_info* node_p;

/*func:create list

return value:sucessfuf head of list,otherwise NULL is returned

para:node_p head*/

node_p init_list(node_p head);

node_p destory_list(node_p head);

node_p search_node(node_p head,int key);

node_p insert_node_to_list(node_p head,int key,node_p new);

void trav_list(node_p head);

node_p delete_node(node_p head,int key);

node_p change_node(node_p head,int a,int b);

#endif

/*tra_list.c*/

#include "my_list.h"

#include

void trav_list(node_p head)

{

node_p p=NULL;

p=head;

while(p!=NULL){

printf("num:%d,name:%s\n",p->num,p->name);

p=p->next;

}

return;

}

/*search_node.c*/

#include "my_list.h"

#include

node_p search_node(node_p head,int key)

{

node_p p=NULL;

p=head;

while(p!=NULL){

if(key==p->num){

printf("num:%d name:%s\n",p->num,p->name);

return p;

}

else

p=p->next;

}

if(p==NULL)

printf("not found\n");

}

/*insert_node.c*/

#include "my_list.h"

#include

node_p insert_node_to_list(node_p head,int key,node_p new)

{

node_p p=NULL;

p=head;

while(1){

if(p==NULL){

new->next=head;

head=new;

break;

}

if(p->num=key){

new->next=p->next;

p->next=new;

break;

}

p=p->next;

}

return head;

}

/*init_list.c*/

#include "my_list.h"

#include

#include

node_p init_list(node_p head)

{

node_p new=NULL;

node_p p=NULL;

while(1){

new=(node_p)malloc(sizeof(stu_info));

if(new==NULL)

printf("malloc failure!\n");

else{

printf("please input num and name:\n");

scanf("%d %s",&new->num,new->name);

new->next=NULL;

if(0==new->num){

free(new);

new=NULL;

break;

}

else{

if(head==NULL)

p=head=new;

else{

p->next=new;

p=p->next;

}

}

}

}

return head;

}

/*destory_list.c*/

#include "my_list.h"

#include

#include

node_p destory_list(node_p head)

{

node_p p=NULL;

p=head;

while(head!=NULL){

head=p->next;

free(p);

p=p->next;

}

return head;

}

/*delete_node.c*/

#include "my_list.h"

#include

#include

node_p delete_node(node_p head,int key)

{

node_p p=NULL,q=NULL;

p=head;

while(1){

if(p->next==NULL){

printf("not found the data you want delete\n");

break;

}

if(head->num==key){

head=p->next;

free(p);

break;

}

q=p;

p=p->next;

if(p->num==key){

q->next=p->next;

free(p);

break;

}

}

return head;

}

/*main.c*/

#include "my_list.h"

#include

#include

int main(void)

{

node_p head=NULL;

node_p p=NULL;

node_p new;

int key;

int key1;

head=init_list(head);

trav_list(head);

printf("please input the data you want search:\n");

scanf("%d",&key);

p=search_node(head,key);

printf("please input the data you want delete:\n");

scanf("%d",&key);

head=delete_node(head,key);

trav_list(head);

new=(node_p)malloc(sizeof(stu_info));

if(new==NULL){

printf("malloc failure\n");

return 1;

}

else{

printf("please input num and name you want insert\n");

scanf("%d %s",&new->num,new->name);

printf("please input the position you want insert:\n");

scanf("%d",&key1);

head=insert_node_to_list(head,key1,new);

}

trav_list(head);

head=destory_list(head);

trav_list(head);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值