Implementation of a lookup algorithm on a single-linked list (0955) Swust-oj

Source: Internet
Author: User

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*&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.