解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _fushu{
int value;
struct _fushu *next;
}fushu;
void add(fushu **phead);
void print(fushu **phead);
void del_one(fushu **phead,int x,int *count);
void get_one(fushu **phead,int x);
void insert_one(fushu **phead,int x,int y,int *count);
int main()
{
fushu *head = NULL;//核心表头
int n;
scanf("%d",&n);
int count = n;
while(n--)
add(&head);//术式反转
int b;
scanf("%d",&b);
char cmd[7];
while(b--)
{
scanf("%s",cmd);
if(!strcmp(cmd,"show"))
{
print(&head);
}
if(!strcmp(cmd,"delete"))
{
int where;
scanf("%d",&where);
if(where>count || where<1)
printf("delete fail\n");
else
del_one(&head,where,&count);
}
if(!strcmp(cmd,"get"))
{
int where;
scanf("%d",&where);
if(where>count || where<1)
printf("get fail\n");
else
get_one(&head,where);
}
if(!strcmp(cmd,"insert"))
{
int where,how;
scanf("%d %d",&where,&how);
if((where>count || where<1) && !(count==0&&where==1))
printf("insert fail\n");
else
insert_one(&head,where,how,&count);
}
}
return 0;
}
void add(fushu **phead)
{
int what;
scanf("%d",&what);
fushu *a = (fushu*)malloc(1*sizeof(fushu));
a->value = what;
a->next = NULL;
if(!*phead)
*phead = a;
else
{
a->next = *phead;
*phead = a;
}
}
void print(fushu **phead)
{
if(!*phead) printf("Link list is empty");
else
{
fushu *p = *phead;
for(;p;p=p->next)
printf("%d ",p->value);
}
printf("\n");
}
void del_one(fushu **phead,int x,int *count)
{
fushu *p = *phead;
if(x==1)
{
*phead = p->next;
free(p);
}
else
{
for(int i=1;i<x-1;i++)
p=p->next;
fushu *pp = p->next;
p->next = pp->next;
free(pp);
}
printf("delete OK\n");
(*count)--;
}
void get_one(fushu **phead,int x)
{
fushu *p = *phead;
for(int i=1;i<x;i++)
p=p->next;
printf("%d\n",p->value);
}
void insert_one(fushu **phead,int x,int y,int *count)
{
fushu *a = (fushu*)malloc(1*sizeof(fushu));
a->value = y;
a->next = NULL;
fushu *p = *phead;
if(!p) *phead = a;
else
{
if(x==1)
{
a->next = *phead;
*phead = a;
}
else
{
for(int i=1;i<x-1;i++)
p=p->next;
a->next = p->next;
p->next = a;
}
}
printf("insert OK\n");
(*count)++;
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:530 |
C语言训练-数字母 (C语言代码)浏览:610 |
A+B for Input-Output Practice (IV) (C++代码)浏览:713 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:798 |
C语言训练-阶乘和数* (C语言代码)-------- 呆板写法浏览:1396 |
求组合数 (C语言代码)浏览:1206 |
WU-复数求和 (C++代码)浏览:2119 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:818 |
P1000 (C语言代码)浏览:911 |
1054题解浏览:516 |