linked list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char name[20];//
int number;
struct student* next;
};


int c;


struct student *creat(){
struct student *head=NULL;
struct student *end,*renew;
char buf[256];
c=0;
end=renew=(struct student*)malloc(sizeof(struct student));
printf("name: number:\n");
 fgets(buf,sizeof(buf),stdin);//stdin,标准输入流
 fflush(stdin);//刷新缓存
 sscanf(buf,"%s %d",&renew->name,&renew->number);
while(renew->number!=0){
c++;
if(c==1){
renew->next=NULL;
end=renew;
head=renew;
}
else{
renew->next=NULL;
end->next=renew;
end=renew;
}
renew=(struct student*)malloc(sizeof(struct student));
printf("name: number:(exit 0 )\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%s %d",&renew->name,&renew->number);
}
free(renew);
return head;
}
void print(struct student *head){
if(head==NULL){
printf("NULL!");
exit(1);
}
struct student *temp;
int index=1;
temp=head;
while(temp!=NULL){
printf("index:%d\t",index++);
printf(" name : %s\t",temp->name);
printf(" number : %d\n",temp->number);
temp=temp->next;
}
}


struct student *insert(struct student *head,int index){
struct student *renew,*pre,*temp;
int i;
char buf[256];

renew=(struct student*)malloc(sizeof(struct student));
printf("name: number:\n");
fgets(buf,sizeof(buf),stdin);//stdin,标准输入流
fflush(stdin);//刷新缓存
sscanf(buf,"%s %d",&renew->name,&renew->number);
temp=head;
if(index==1){//将新插入节点作为首节点
renew->next=head;
head=renew;
}
else{//find data(index)
for(i=1;i<index&&i<c;i++){//if donot have "&&i<c" what will happen
pre=temp;
temp=temp->next;// temp point to next data
}
if(index<=c){
pre->next=renew;
renew->next=temp;
}
else {//make new data insert into the end
temp->next=renew;
renew->next=NULL; 
}
}
c++;//donot miss,else you canot make change() work
return head;
}


struct student* Delete(struct student *head,int index){
struct student *temp,*pre;
int i;
temp=head;
if(index==1){
head=head->next;//make next data as point 
free(temp);
}
else{
for(i=1;i<index;i++){
pre=temp;
temp=temp->next;
}
pre->next=temp->next;//
free(temp);//free the memory of temp;

}
c--; //donot miss,else you canot make change() work
return head;
}
struct student *change(struct student *head,int i,int j ){
struct student *temp,*temp_i,*temp_j,*pre_i,*pre_j;
int k;
temp_i=head;
temp_j=head;
for(k=1;k<i;k++){//find data(index_i)
pre_i=temp_i;
temp_i=temp_i->next;
}
for(k=1;k<j;k++){//finde data(index_j)
pre_j=temp_j;
temp_j=temp_j->next;
}
if(i==1){
if(c==2){
temp_j->next=temp_i;
head=temp_j;
temp_i->next=NULL;
}
else if(j==c){
temp_j->next=temp_i->next;
head=temp_j;
pre_j->next=temp_i;
temp_i->next=NULL;
}
else if(j==i+1){
temp_i->next=temp_j->next;
temp_j->next=temp_i;
head=temp_j;
}
else{
temp=head;
for(k=1;k<j+1;k++)
temp=temp->next;
temp_j->next=temp_i->next;
head=temp_j;
pre_j->next=temp_i;

temp_i->next=temp;
}
}
else if(j==c){
if(i==j-1){
pre_i->next=temp_j;
temp_j->next=temp_i;
temp_i->next=NULL;
}
else{
temp=head;
for(k=1;k<i+1;k++)
temp=temp->next;
pre_i->next=temp_j;
pre_j->next=temp_i;
temp_i->next=NULL;

temp_j->next=temp;
}
}
else if(j==i+1){
temp=head;
for(k=1;k<j+1;k++)
temp=temp->next;
pre_i->next=temp_j;
temp_j->next=temp_i;
temp_i->next=temp;
}
else{
temp=head;
for(k=1;k<i+1;k++)
temp=temp->next;
pre_i->next=temp_j;
pre_j->next=temp_i;
temp_i->next=temp_j->next;
temp_j->next=temp;
}
return head;
}
struct student *search(struct student *head,int n){
int i;
struct student *temp;
temp=head;
for(i=1;i<n;i++)
temp=temp->next;
return temp;
}
main(){
struct student *head;
struct student *temp;
char buf[256];
int index,index_i=0,index_j=0;
head=creat();
print(head);




printf("insert: input before which to insert,input 0 to exit\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
while(index){
head=insert(head,index);
print(head);
printf("insert: input before which to insert,input 0 to exit\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
}


printf("delete:input which to delete,input 0 to exit\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
while(index){
head=Delete(head,index);
print(head);
printf("delete:input which to delete,input 0 to exit\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
}


printf("change: index_i:,index_j\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d,%d",&index_i,&index_j);
while(index_i!=0&&index_j!=0){
if(index_i>=index_j){
printf("shoulde i<j,input again\n");
exit(0);
}
head=change(head,index_i,index_j);
print(head);
printf("change: input index_i:,index_j:(index_i<index_j,input 0 to exit)\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d,%d",&index_i,&index_j);

}

printf("search: input index(input 0 to exit):\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
while(index){
printf("index:%d\t",index);
temp=search(head,index);
printf("name:%s\t",temp->name);
printf("number:%d\n",temp->number);
printf("search: input index(input 0 to exit):\n");
fgets(buf,sizeof(buf),stdin);
fflush(stdin);
sscanf(buf,"%d",&index);
}
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值