链表操作:创建,插入,删除,查找等功能
//定义链表结构体;
typedef struct linknode
{
int id;
char name[20];
struct linknode *next;
}node;
//创建链表;
#include <stdio.h>
#include "node.h"
/*create n node */
node *creat(int n)
{
node *head,*fp,*pb;
head = (node *)malloc(sizeof(node));//creat the head node
fp=head;
int i;
for(i=1; i<=n; i++)
{
pb=(node *)malloc(sizeof(node));//
printf("please input the NAME of id: %d:/n",i);
getchar();//注意,先获取一个换行
fgets(pb->name,10,stdin);//最好不要用GETS()函数;
// scanf("%s",pb->name);//carefullly,don't forget "&";
pb->id = i;
fp->next = pb;
pb->next = NULL;
fp = pb;
}
//pb->next = NULL;
fp = head;
head = head->next;
free(fp);//释放头结点;
return head;
}
//删除节点操作;
#include <stdio.h>
#include "node.h"
void delete(node *head,int id)
{
node *pb,*fp;
if(head==NULL)
{
printf("/nempty list!/n");
}
pb=head;
while((pb->id!=id) && (pb->next!=NULL))//找到ID,并且PB没有到链表结尾;
{
fp=pb;//fp一直尾随PB;
pb=pb->next;//PB向后移动;
}
if(pb->id==id)//find the id;
{
if(pb==head)
head=pb->next;
else fp->next=pb->next;//去掉PB,连接PB前后;
free(pb);
printf("The node id %d is deleted/n",id);
}
else
printf("The node not been foud!/n");
}
//查找
#include <stdio.h>
#include "node.h"
void find(node *head, int id)
{
node *pb;
pb = head;//让PB指向头结点;
while((pb->next != NULL)&&(pb->id != id))//pb最好指向要查找的节点
pb = pb->next;
if(pb->id == id)
printf("The ID and name is %d: %s/n",pb->id,pb->name);
else if((pb->id != id)&&(pb->next == NULL))
printf("Can't find the name/n");
}
//插入,也可以创建链表;
#include <stdio.h>
#include "node.h"
void insert(node * head,int i,char name[20])
{
node *pf,*pb,*pi;
pi=(node *)malloc(sizeof(node));//首先设置好要插入的节点;
scanf("%s",pi->name);
pi->id =i;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;//让PI作为第一个节点
}
else
{
while((pi->id > pb->id)&&(pb->next!=NULL))//两种判断方式,1:找到比PI大的数,2:不要到链表的最后;
{
pf=pb;//PF跟随PB
pb=pb->next; //PB向下走
}
if(pi->id <= pb->id)//找到了,在第一个节点,和在中间插入;
{
if(head==pb)//只有一个节点
head=pi;
else
pf->next=pi;//插入的PI要在PB前面
pi->next=pb; //PB在PI前面
}
else//没有找到,即最后一个
{
pb->next=pi;
pi->next=NULL;
}
}
}
#include <stdio.h>
#include "node.h"
void print_list(node *s)
{
while(s != NULL)
{
printf("the id and name is %d: %s/n",s->id,s->name);
s = s->next;
}
}
#include <stdio.h>
#include "node.h"
//链表操作;
int main()
{
node *t;
int id;
char x='w';
printf("Ple input 'c' for CREAT action/n");
printf("Ple input 'f' for FIND action/n");
printf("Ple input 'd' for DELETE action/n");
printf("Ple input 'i' for INSERT action/n");
while(x != 0)
{
printf("Please choose one of them to hava a action: /n");
scanf("%c",&x);
switch(x)
{
case 'c': t=(node *)creat(3);
print_list(t);
break;
case 'f': printf("Please input what id you will find!/n");
scanf("%d",&id);
find(t,id);
break;
case 'd': printf("Please input what id you want to del!/n");
scanf("%d",&id);
delete(t,id);
print_list(t);
break;
case 'i': printf("Please input what id you want to insert!/n");
scanf("%d",&id);
insert(t,id,t->name);
print_list(t);
break;
case 'p': print_list(t);
break;
default:
printf("NO ACTION!/n");
}
}
system("pause");
return 0;
}
MAKEFILE;
OBJ = print_list.o main.o find.o creat.o delete.o insert.o
CC = gcc
student:$(OBJ)
$(CC) -o $@ $^
main.o :main.c
$(CC) -c $<
find.o :find.c
$(CC) -c $<
print_list.o:print_list.c
$(CC) -c $<
creat.o:creat.c
$(CC) -c $<
delete.o:delete.c
$(CC) -c $<
insert.o:insert.c
$(CC) -c $<
clean:
rm *.o student -fr