题目:
代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
int data;
struct SList* next;
}SL;
void SListInit(SL** ps)
{
*ps = (SL*)malloc(sizeof(SL));
(*ps)->next = NULL;
}
void SListCreate(SL** ps, int n)
{
SL* cur = *ps;
int x = 0;
while (n)
{
scanf("%d", &x);
getchar();
SL* newnode = (SL*)malloc(sizeof(SL*));
newnode->data = x;
cur->next = newnode;
cur = newnode;
n--;
}
cur->next = NULL;
}
void SListFind(SL** ps, int i)
{
SL* cur = *ps;
int j = 1;
while (cur)
{
if (j == i)
break;
j++;
cur = cur->next;
}
if (j == i)
printf("ok");
else
printf("error");
}
int main()
{
SL* plist = NULL;
SListInit(&plist);
int n = 0;
scanf("%d", &n);
getchar();
SListCreate(&plist,n);
int i = 0;
scanf("%d", &i);
getchar();
SListFind(&plist, i);
return 0;
}