Basic operation of Linear table

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int *elem;
int length;
int listsize;
}sqlist;

#define LIST_MAX
#define LIST_ADD 2

int initlist (sqlist *l)
{
l->elem= (int*) malloc (list_max*sizeof (int. ));
if (! L->elem)
    {
printf ("Initialization failed! \ n ");
exit ( -1);
    }
l->length=0;
l->listsize=list_max;
return 1;
}

int destorylist (sqlist *l)
{
Free (L->elem);
l->elem=null;
    
return 1;
}
int clearlist (sqlist *l)
{
l->length=0;
return 1;
}
int GetList (sqlist l,int i,int *e)
{
if (i<0| | I>l.length)
    {
printf ("The length of the output lookup exceeds the length of the existing linked list!\n");
exit ( -1);
    }
*e=* (l.elem+i-1);
return 1;
}

int insertlist (sqlist *l,int i,int e)
{
int *newbase,*p,*q;
if (i<0| | I>L->LENGTH+1)
    {
printf ("The location you intend to insert is beyond the length of the list!") \ n ");
exit ( -1);
    }
if (l->length>=l->listsize)
    {
newbase= (int *) realloc (L->elem, (l->listsize+list_add) *sizeof (int));
if (!newbase)
exit ( -1);
l->listsize+=list_add;
l->elem=l->elem;
    }
p=l->elem+i-1;
For (q=l->elem+l->length-1;q>=p;q--)
* (q+1) =*q;
*p=e;
+ + (l->length);
        
return 1;
}
int Listempty (sqlist L)
{
if (l.length==0)
return 1;
Else
return 0;
}
int listlength (sqlist L)
{
return l.length;
}
int deletelist (sqlist *l,int i,int *e)
{
int *p;
if (i<0| | I>l->length)
    {
printf ("The position of the element to be removed does not exist in the list!\n");
exit ( -1);
    }
p=l->elem+i-1;
*e=*p;
For (;p <l->elem+l->length-1;p++)
*p=* (p+1);
--l->length;
return 1;
}    
int Locateelem (sqlist l,int e,int (*compare) (int v1,int v2))
{
int *p;
int i=1;
P=l.elem;
while (I<=l.length&&!compare (* (p++), E))
++i;
if (i<=l.length)
return i;
Else
return 0;
}    
int equal (int v1,int v2)
{
if (v1==v2)
return 1;
Else
return 0;
}

int Priorelem (sqlist l,int cur_e,int *pre_e)
{
int i;
I=locateelem (l,cur_e,equal);
if (i>1)
    {
*pre_e=* (l.elem+i-2);
return 1;
    }
Else
    {
printf ("No precursor!" \ n ");
exit ( -1);
    }
}
int Nextelem (sqlist l,int cur_e,int *next_e)
{
int i=1;
int *p=l.elem;
While (i<l.length&&*p!=cur_e)
    {
++i;
++p;
    }
if (i==l.length)
return 0;
Else
    {
*next_e=* (++p);
return 1;
    }
}
int Listtraverse (sqlist l,int (*visit) (int e))
{
int i=1;
For (i=1;i<=l.length;i++)
Visit (* (l.elem+i-1));
return 1;
}
int print (int e)
{
printf ("%d", e);
return 1;
}
int main ()
{
int i,e0;
sqlist L;
initlist (&l);

For (i=1;i<=11;i++)
insertlist (&l,i,i*2);
    
printf ("The linked list is empty:%d (1. Indicates null, 0 means non-null) \ n", Listempty (L));
    
For (i=1;i<=11;i++)
    {
GetList (L,I,&E0);
printf ("%d", E0);
    }

printf ("\ n");
    
deletelist (&L,1,&E0);
printf ("Delete first element%d\n", E0);

printf ("The length of the list is:%d\n", Listlength (L));
    
printf ("Find if there is a 4 in the attached table, it is in the position of%d\n", Locateelem (l,4,equal));
    
Priorelem (L,8,&E0);
printf ("Please output 8 of the previous element is:%d\n", E0);

Nextelem (L,8,&E0);
printf ("Please output 8 after the next element is:%d\n", E0);

Listtraverse (l,print);
printf ("\ n");

destorylist (&l);

return 0;
}

Basic operation of Linear table

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.