Definition, initialization, and other operations of the sequential list of linear tables

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERR 0
#define MAXSIZE 100
Defining the sequential storage structure

typedef struct list{
int elem[maxsize];
int last;
}seqlist;

Initializing a linear table
Seqlist *initlist ()
{
Seqlist *l;
L = (Seqlist *) malloc (sizeof (seqlist));
L->last = 0;
return L;
}

Find operations, find by content
int Locate (seqlist *l,int e)
{
int i=0;
for (i;i<l->last-1;i++)
{
if (l->elem[i]==e)
{
return i+1;
}
}
return-1;
}

The insert operation. Insert a new element before the first position in the table

int inslist (seqlist *l,int i,int e)
{
int k;
if (i<1| | I>L->LAST+1)
{
printf ("The insertion position is illegal! ");
return ERR;
}
if (l->last>=maxsize)
{
printf ("The table is full and cannot be inserted!") ");
return ERR;
}

for (k=l->last-1;k>=i;k--)
{
l->elem[k+1]=l->elem[k];
}
l->elem[i-1]=e;
l->last++;
return OK;
}

Deletes the first element of the table and returns its value with the pointer E
int dellist (seqlist *l,int i,int *e)
{
int k;
if (i<1| | I>l->last)
{
printf ("Delete location is not legal!") ");
return ERR;
}
*e=l->elem[i-1];
for (k=i-1;k<l->last;k++)
{
l->elem[k]=l->elem[k+1];
}
l->last--;
return OK;
}
Output of linear tables
void Print_seqlist (Seqlist *l)
{
Int J;
for (j=1;j<=l->last;j++)
{
printf ("%d\t", l->elem[j-1]);
if (j%5==0)
{
printf ("\ n");
}
}
}
int main ()
{

Seqlist *l;
L=initlist ();
int a,j,locate,e;
for (j=1;j<10;j++)
{
printf ("Please enter the number to insert: \ n");
scanf ("%d", &a);
Inslist (L,A,J);
}
printf ("\ n");
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("The inserted list is: \ n");
Print_seqlist (L);
printf ("\ n");
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("Find if element 5 exists, display subscript: \ n");
Locate = locate (l,5);
printf ("%d\n", locate);
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("Delete the 7th element, return the deleted element: \ n");
Dellist (l,7,&e);
printf ("The element being deleted is:%d\n", e);
return 0;
}

Definition, initialization, and other operations of the sequential list of linear tables

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.