Description
Create a single-linked list of lead nodes of length n, looking for the first node in the table, and if found, output "ok! ", otherwise output" error! ”。 The processing data type is integer type.
Input
The length of the first behavior list n; The data element in the second behavior chain; the third behavior to find the node I.
Output
Find on the output "ok! "The output is not found" error! ”。
Sample Input101 2 3 4 5 6 7 8 9 105Sample inputok!
Code:
#include <stdio.h>
#include <malloc.h>
typedef struct NODE
{
int data;
Node * NEXT;
}node;
int total;
void Create (Node*&l,int a[],int N)
{
node*r,*s;
L= (node*) malloc (sizeof (struct Node));
S=l;
int i;
for (i=0;i<n;i++)
{
S= (node*) malloc (sizeof (struct Node));
s->data=a[i];
r->next=s;
R=s;
}
r->next=null;
}
void input (NODE*&L)
{
int i,a[100];
scanf ("%d", &total);
for (i=0;i<total;i++)
{
scanf ("%d", &a[i]);
}
Create (l,a,total);
}
BOOL Insert (Node*l,int i)
{
int j=0;
Node *p;
P=l;
if (i>total| | i<=0)
{
return false;
}
while (P!=null)
{
j + +;
p=p->next;
if (j==i)//Find existing I
return true;
}
}
int main ()
{
Node *l;
Input (L);
int i;
scanf ("%d", &i);
if (Insert (l,i))
{
printf ("ok!");
}
Else
printf ("error!");
return 0;
}
Implementation of a lookup algorithm on a single-linked list (0955) Swust-oj