解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node
{
char a[32];
struct Node *next;
int length;
}Node;
//插入
void Insert(Node *L)
{
int x,y,i=0,j=0;
char b[32];
scanf("%d %s",&x,b);
Node *q=(Node*)malloc(sizeof(Node));
while(i<strlen(b))
q->a[j++]=b[i++];//不要把下标都写成i++了
q->length=j;//为什么用strlen(q->a)会出错
if(L->next==NULL)
{
L->next=q;
q->next=NULL;
}
else
{
Node *p=L;
y=x-1;
while(y--)
p=p->next;
q->next=p->next;
p->next=q;
}
}
//遍历
void Show(Node *L)
{
int i=0;
Node *p=L->next;
while(p!=NULL)
{
i=0;
while(i<p->length)
printf("%c",p->a[i++]);
printf(" ");
p=p->next;
}
printf("\n");
}
//删除
void Delete(Node *L)
{
Node *p=L->next,*q=L;
char c[32];
scanf("%s",&c);
while(p!=NULL)
{
if(strcmp(p->a,c)==0)
{
q->next=p->next;
break;
}
p=p->next;
q=q->next;
}
}
//查找
void Search(Node *L)
{
char b[32];
int s=1;
scanf("%s",b);
Node *p=L->next;
while(p!=NULL)
{
if(strcmp(p->a,b)==0)
{
printf("%d\n",s);
break;
}
s++;
p=p->next;
}
}
int main()
{
Node *L=(Node *)malloc(sizeof(Node));
L->next=NULL;
char a[32];
while(~scanf("%s",a))
{
if(strcmp(a,"insert")==0)
{
Insert(L);
continue;
}
if(strcmp(a,"show")==0)
{
Show(L);
continue;
}
if(strcmp(a,"delete")==0)
{
Delete(L);
continue;
}
if(strcmp(a,"search")==0)
Search(L);
}
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:652 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:667 |
C语言训练-角谷猜想 (C语言代码)浏览:1759 |
矩阵乘法 (C++代码)浏览:1645 |
回文数(一) (C语言代码)浏览:804 |
C语言程序设计教程(第三版)课后习题1.5 (C++代码)浏览:1108 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:855 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:498 |
用筛法求之N内的素数。 (C语言代码)浏览:705 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:707 |