#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct student {
int score;
struct student *next;
}stu, *stuNode;
stuNode creat(void)
{
stuNode head,node,end;
int i,n=0;
head = (stuNode)malloc(sizeof(student));
head->next=NULL;
end = head;
printf("create a linklist,please input a number of list:");
scanf("%d",&n);
printf("begin to create linklist\n");
for (i = 0; i < n; i++) {
node = (stuNode)malloc(sizeof(student));
printf("please input %d value:",i+1);
scanf("%d", &node->score);
node->next=NULL;
end->next = node;
end = node;
}
end->next=NULL;
return head;
}
int getlength(stuNode st)
{
//the length is next's number
int k=0;
stuNode head;
head=st;
while(head->next!=NULL){
k++;
head=head->next;
}
return k;
}
// insert a value in linklist
void insert(stuNode st){
int data,n=0;
stuNode head,q;
head=st;
printf("please input insert loacation:\n");
scanf("%d",&n);
printf("please input insert value:\n");
scanf("%d",&data);
if(n>getlength(st)||n<1){printf("the location is error!!\n");}
for(int i=0;i<n-1;i++){head=head->next;}
q = (stuNode)malloc(sizeof(student));
q->next =head->next;
head->next=q;
q->score=data;
}
//delete a value
void deletelist(stuNode st)
{
int n=0;
stuNode head,q;
head=st;
printf("please input delete loacation:\n");
scanf("%d",&n);
if(n<1||n>getlength(st)){printf("the location is error!!\n");}
for(int i=0;i<n-1;i++){head=head->next;}
q=head->next;
head->next =q->next;
free(q);
}
//change a value
void change(stuNode st){
stuNode head,q;
int data,n=0;
head=st;
printf("please input change loacation:\n");
scanf("%d",&n);
printf("please input change value:\n");
scanf("%d",&data);
if(n<1||n>getlength(st)){printf("the location is error!!,please input again\n");
scanf("%d",&n);
}
for(int i=0;i<n-1;i++){head=head->next;}
q=head->next;
q->score=data;
}
//output linklist
void display(stuNode st) {
stuNode p=st->next;
printf("this linklist value is:");
while (p!=NULL)
{
if(p->next!=NULL)
printf("%d->", p->score);
else
printf("%d", p->score);
p = p->next;
}
printf("\n");
}
void listget(stuNode st){
int n=0;
printf("please input qurey loacation:\n");
scanf("%d",&n);
stuNode p=st;
for(int i=0;i<n-1;i++){p=p->next;}
printf("this linklist %dSt value is:%d\n",n, p->next->score);
}
int main() {
stuNode st;
int n;
while(1){
printf("**************************\n");
printf("1---create list\n");
printf("2---display list\n");
printf("3---insert value into list\n");
printf("4---change value of list\n");
printf("5---delete value of list\n");
printf("6---query value of list\n");
printf("**************************\n");
printf("please input the number 1-6:");
scanf("%d",&n);
printf("\n");
if(st==NULL){
printf("please input the number 1-6:");
scanf("%d",&n);
printf("\n");}
switch(n){
case 1:st=creat();break;
case 2:display(st);break;
case 3:insert(st);break;
case 4:change(st);break;
case 5:deletelist(st);break;
case 6:listget(st);break;
default:printf("input number is error\n");
}
}
return 0;
}
//出现的问题:需要第一次必须执行1,不然程序会崩溃。考虑用if语句:在n=1的情况下执行2-6的情况。
//反复创建链表,只会存储当前链表,上一次创建的链表信息不会保存,会被覆盖。
//可以增加数据信息:int char类型。
//一些细节 比如输入的数据是否超过范围或者不合理没有完全考虑进来;