/*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;
}