Algorithm design topic a sequential table lookup

Source: Internet
Author: User

/* Set the keyword in the order table is incremented and orderly, the Sentinel is set at the edge of the index, the design algorithm to achieve a simple sequential lookup */

#include "stdio.h"

#include "malloc.h"

#define LIST_SIZE 20

typedef struct{

Charr[list_size];

Intlength; Length is the number of elements in the table

}recordlist;

Recordlist *sqlset () {//Build Order table

Recordlist*l;

Inti=0;char C;

L= (recordlist*) malloc (sizeof (recordlist));

scanf ("%c", &c);//Enter a character

L->length= 0;

while (c!= ' # ') {

l->r[i]=c;

l->length++;

i++;

if (l->length== list_size-1) break;

scanf ("%c", &c);

}

return (L);

}

int Sequensearch (recordlist *l,char k,intn) {//implementation at the end of the index, simple order lookup element

Inti=0,position=-1;//position the location to find

l->r[n]=k;//the last unit as a lookout.

while (n!=i) {

if (l->r[i]== k) {

Position= i+1;

Break

}

i++;

}

Returnposition;

}

int main () {

Recordlist*a;

Intm;

Charc;

printf ("Please enter data (character type)");

A= Sqlset ();

printf ("Please enter the element to find");

C=getchar ();//Eat a return character.

scanf ("%c", &c);

m= Sequensearch (a,c,a->length);

if (m==-1)

printf ("The lookup is unsuccessful. ");

Else

printf ("m=%d, find success. ", m);

Return0;

}

To run a screenshot:

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.