回复 5楼 TonyDeng
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head, *p, *q, *t,*t1,*t2,*temp;
int i, j, n, a,b,flag=1,j1;
char x[10] = "insert";
char y[10] = "delete";
char k[10] = "clear";
char z[15] = "getelem";
char z1[10] = "exit";
char h[1000];
head = NULL;
while (gets_s(h))
{
if(strcmp(h, x) == 0)
{
scanf_s("%d", &n);
if (head == NULL)
{
for (i = 1; i <= n; i++)
{
scanf_s("%d %d", &a, &b);
p = (struct node*)malloc(sizeof(struct node*));
p->data = b;
p->next = NULL;
if (head == NULL)
{
head = p;
}
else
{
q->next = p;
}
q = p;
}
}
else
{
for (j = 1; j <= n;j++)
{
scanf_s("%d %d", &a, &b);
t = head;
for (j1 = 1; j1 <= a; j1++)
{
if (j1 == a)
{
p = (struct node*)malloc(sizeof(struct node*));
p->data = b;
p->next = t->next;
t->next = p;
break;
}
t = t->next;
}
}
}
}
if (strcmp(h, y) == 0)
{
i = 1;
t = head;
scanf_s("%d", &n);
if (n == 1)
{
printf("%d\n", t->data);
flag = 0;
}
if (flag)
{
while (t != NULL)
{
t = t->next;
i++;
if (i + 1 == n)
{
b = t->next->data;
temp = t->next;
t = temp;
free(temp);
printf("%d\n", b);
}
}
}
}
if (strcmp(h, k) == 0)
{
head = NULL;
}
if (strcmp(h, z) == 0)
{
scanf_s("%d", &n);
t = head;
i = 1;
while (t != NULL)
{
t = t->next;
i++;
if (i == n)
{
printf("%d\n", t->data);
}
}
}
if (strcmp(h, z1) == 0)
{
break;
}
}
}
这是改完之后的