链表的增删改查

#ifndef LINK_H_ #define LINK_H_ /* *node struction * */ struct stu { int number; char name[13]; struct stu *next; };

typedef struct stu stu_t; typedef stu_t *stu_pointer;

/* *function:init one node datas return values:void para * */ void init_node(stu_pointer node);/*success*/

/* *create a new link * */ stu_pointer create_link();/*success*/

/* *destroy one link which you have created * */ void destroy_link(stu_pointer head);/*success*/

/* *traversal one link which haved been created * */ void print_link(stu_pointer head);/*success*/

/* *find one node which value have given * */ stu_pointer find_node(stu_pointer head,int

data);/*success*/

/* * delect one node which value hava given * */ stu_pointer delect_node(stu_pointer head,int data);

/* * insert one new node into the link * */ stu_pointer insert_node(stu_pointer head,int

position,int data,char *name);

/* * update the node you wannt update * */ void updata_node(stu_pointer head,int olddata,int newdata,char *newname);/*success*/

/* *print the value of the node * */ void print_value_node(stu_pointer node);/*success*/ #endif

#include <stdio.h> #include "link.h" #include <stdlib.h> #include <string.h>

int main

(void) { #if 0 stu_t stu[3]={{34,"chenwei",NULL},\ {23,"xiaowei",NULL},\

{56,"tangsha",NULL}}; stu_pointer head,p; p=head=&stu[0]; stu[0].next=&stu

[1]; stu[1].next=&stu[2];

while(p!=NULL){ printf("number:%

d\tname:%s\n",p->number,p->name); p=p->next; } #endif #if 0

printf("\n--------------\n"); stu_pointer head,node; int data; int newdata;

printf("please create link\n"); head=create_link();

printf("travel the

link\n"); print_link(head); char newname[24]; /* printf("please input

position\n"); scanf("%d",&data); printf("\nplease input new datas\n");

scanf("%d%s",&newdata,newname); node=insert_node(head,data,newdata,newname);

print_link(node);*/ printf("\nplease input data which you want to delect\n");

scanf("%d",&data); node=delect_node(head,data); print_link(node); /* printf

("\nplease

input

the

value

which

you

want

to

search\n");*/ /* scanf("%d",&data);

node=find_node(head,data); print_value_node(node); printf("\ntest

updata_node\n"); updata_node(head,data,5); print_link(head);*/ /*

head=destroy_link(head); if(head==NULL) printf("seccuse\n");*/

#endif #if

1 stu_pointer head,node; int data,data1; char newname[14]; printf

("\n----------------\n"); printf("create one link\n"); head=create_link();

printf("output the datas of the link\n"); print_link(head); printf("please

input data you want to search\n"); scanf("%d",&data); node=find_node

(head,data); print_value_node(node); printf("please input data you want to

delect\n"); scanf("%d",&data); head=delect_node(head,data); printf("the data

of link after you delect %d node\n",data); print_link(head); printf("please

input new node\n "); scanf("%d%s",&data,newname); printf("please input

position\n"); scanf("%d",&data1); head=insert_node(head,data1,data,newname);

print_link(head); printf("updata old datas\n"); scanf("%d%d%

s",&data1,&data,newname); updata_node(head,data1,data,newname); print_link(head);

printf("destroy link\n"); destroy_link(head);

#endif return 0; }

void

init_node(stu_pointer node)/*init the data of one node*/ { int num;

/*char

*nm;*/ char nm[14]; scanf("%d%s",&num,nm); node->number=num; strcpy(node-

>name,nm); node->next=NULL; }

stu_pointer create_link()/*create one link*/ {

stu_pointer head=NULL,p=NULL,new; while(1){/*create link for new data and insert the

link*/ new=malloc(sizeof(stu_t)); if(NULL==new){

printf("malloc failed\n"); } init_node(new);

if(new->number==0){ free(new); new=NULL;

break; } else{ if(head==NULL){/*the link

is empty link whe the value of head is NULL*/ p=head=new;/*p adn

head position at the first node*/

} else{/*p

position at the second node and after nodes*/

p->next=new;

p=p->next; }

} }

return head;/*return the head of the link*/ }

void print_link(stu_pointer head)/*print

the datas of the link*/

{ stu_pointer p; p=head; while(p!=NULL){/*the

program end when the value of p is NULL*/ printf("num:%d\tname:%s\n",p-

>number,p->name); p=p->next; } }

/*destroy the link when you do need

it*/ void destroy_link(stu_pointer head) { stu_pointer p;

p=head;

while(p!=NULL){ head=p->next;/*set the head position at next one*/

free(p);/*free one node*/ p=head; }

}

/*find the node which

value is equal with data of argument *and return the postion of the node found * */ stu_pointer find_node(stu_pointer head,int data) { stu_pointer p; p=head;

while(p!=NULL){ if(p->number==data){ /* printf("find

success\n");*/ return p; } else{

p=p->next; } } return p;

}

/*print the value of the node*/ void print_value_node(stu_pointer node) {

printf("num:%d\tname:%s\n",node-

>number,node->name); }

/* *update the value of the node into a new data * */ void

updata_node(stu_pointer newdata,char *newname) {

head,int

olddata,int

stu_pointer p; p=find_node(head,olddata); p->number=newdata; strcpy(p-

>name,newname);

}

/* *insert a new node into the link * */ stu_pointer insert_node

(stu_pointer *newname) {

head,int

position,int

data,char

stu_pointer new,p,back;

new=malloc(sizeof(stu_t));/*malloc new space*/ new->number=data; strcpy(new-

>name,newname); p=back=head; if(head->number==position){/*judge the head if not

the find node*/ new->next=back; head->next=new; }

else{/*judge the node if not other node * * which is

middle node or end node*/ p=find_node(head,position); back=p-

>next; new->next=back; p->next=new; } return head; }

/*delect one old node if you need delect it

*you can use the function to delect one node

* */ stu_pointer delect_node(stu_pointer head,int data) { stu_pointer p,back;

back=head; if(head->number==data){ head=back->next;

free(back); back=NULL; return head; } else{

p=head->next;

while(p!=NULL){ if(p-

>number==data){ back->next=p->next;

free(p); p=NULL;

return head; }

back=back->next; p=p->next; }

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

} return head;

}


相关文档

链表的操作(增删改查)及逆置链表
c++课程设计《链表的实现-增删改查》
双链表增删改查
NOIP初赛之链表的创建和增删改查
C语言指针链表(增、删、改、查)
c言语链表的增删查改(最简单)
C语言链表实现增删改查
双向循环链表的增删改查
单链表的增删改查及合并
对表的增删改查
电脑版