C-language implementation of chain storage structure for linear tables

Source: Internet
Author: User

#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20/* Storage space Initial allocation */

typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */
typedef int elemtype;/* Elemtype Type is based on the actual situation and is assumed to be int */


Status visit (elemtype c)
{
printf ("%d", c);
return OK;
}

typedef struct NODE
{
Elemtype data;
struct Node *next;
}node;
typedef struct NODE *linklist; /* Define LINKLIST */

/* Initialize sequential linear table */
Status initlist (linklist *l)
{
*l= (linklist) malloc (sizeof (Node)); /* Generate head node and make L point to this head node */
if (! ( *L)/* Storage Allocation failed */
return ERROR;
(*l)->next=null; /* Pointer field is empty */

return OK;
}

/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns True if L is an empty table, otherwise false */
Status listempty (linklist L)
{
if (L->next)
return FALSE;
Else
return TRUE;
}

/* Initial conditions: Sequential linear table L already exists. Operation Result: Reset L to Empty table */
Status clearlist (linklist *l)
{
Linklist p,q;
p= (*l)->next; /* p points to the first node */
while (p)/* is not at the end of the table */
{
q=p->next;
Free (p);
p=q;
}
(*l)->next=null; /* head node pointer field is empty */
return OK;
}

/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns the number of data elements in L */
int Listlength (linklist L)
{
int i=0;
Linklist p=l->next; /* p points to the first node */
while (p)
{
i++;
p=p->next;
}
return i;
}

/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) */
/* Operation Result: Use E to return the value of the I data element in L */
Status Getelem (linklist l,int i,elemtype *e)
{
Int J;
linklist p;/* Declaration a node P */
p = l->next;/* Let P point to the first node of the list L */
j = 1;/* J for Counter */
while (P && j<i)/* p is not empty or counter j is not equal to I, the loop continues */
{
p = p->next; /* Let P point to the next node. */
++j;
}
if (!p | | j>i)
return ERROR; /* element I does not exist */
*e = p->data; /* Take the first element of the data */
return OK;
}

/* Initial conditions: Sequential linear table L already exists */
/* Operation Result: Returns the bit order of the 1th data element in L that satisfies the relationship with E. */
/* If such a data element does not exist, the return value is 0 */
int Locateelem (linklist l,elemtype e)
{
int i=0;
Linklist p=l->next;
while (p)
{
i++;
if (p->data==e)/* Find such data element */
return i;
p=p->next;
}

return 0;
}


/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l), */
/* operation result: Inserting a new data element before the I position in L e,l length plus 1 */
Status Listinsert ( linklist *l,int i,elemtype e)
{
Int J;
Linklist p,s;
P = *l;
J = 1;
while (P && J < i)/* Find the first node */
{
P = p->next;
++j;
}
if (!p | | J > i)
return ERROR;/* The I element does not exist */
S = (linklist) malloc (sizeof (Node));/* Generate new node (C language standard function) */
S->data = e;
S->next = p->next;/* Assigns the successor of P to the successor of S/
P->next = s;/* assigns S to the subsequent */
return OK of P;
}

/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) */
/* Operation Result: Delete the I data element of L and return its value with E, the length of L minus 1 */
Status listdelete (linklist *l,int i,elemtype *e)
{
Int J;
Linklist p,q;
p = *l;
j = 1;
while (P->next && J < i)/* traverse looking for element I/*
{
p = p->next;
++j;
}
if (! ( P->next) | | J > I)
return ERROR; /* element I does not exist */
Q = p->next;
P->next = q->next;/* assigns the successor of Q to the successor of P */
*e = q->data; /* Send the data in the Q node to E */
Free (q); /* Let the system reclaim this node and free up memory */
return OK;
}

/* Initial conditions: Sequential linear table L already exists */
/* Operation result: output per data element of L, in turn */
Status Listtraverse (linklist L)
{
Linklist p=l->next;
while (p)
{
Visit (P->data);
p=p->next;
}
printf ("\ n");
return OK;
}

/* Generate n-element values randomly, create single-chain linear table with header nodes L (head interpolation) */
void Createlisthead (linklist *l, int n)
{
Linklist p;
int i;
Srand (Time (0)); /* Initialize random number seed */
*l = (linklist) malloc (sizeof (Node));
(*l)->next = NULL; /* First set up a single-link list of lead nodes */
for (i=0; i<n; i++)
{
p = (linklist) malloc (sizeof (Node)); /* Create a new node */
P->data = rand ()%100+1; /* Randomly generate a number within 100 */
P->next = (*l)->next;
(*l)->next = p;/* inserted into the header */
}
}

/* Generate n-element values randomly, create single-chain linear table with header nodes L (tail interpolation) */
void Createlisttail (linklist *l, int n)
{
Linklist p,r;
int i;
Srand (Time (0)); /* Initialize random number seed */
*l = (linklist) malloc (sizeof (Node)); /* L for the entire linear table */
R=*l; /* R is the node that points to the tail */
for (i=0; i<n; i++)
{
p = (node *) malloc (sizeof (node)); /* Create a new node */
P->data = rand ()%100+1; /* Randomly generate a number within 100 */
r->next=p; /* Point the pointer at the end of the footer to the new node */.
r = P; /* Define the current new node as the footer terminal node */
}
R->next = NULL; /* indicates end of current list */
}

int main ()
{
Linklist L;
Elemtype e;
Status i;
int j,k;
I=initlist (&L);
printf ("Initialize L: Listlength (L) =%d\n", Listlength (l));
for (j=1;j<=5;j++)
I=listinsert (&L,1,J);
printf ("The table head in L in turn inserted after the l.data=:");
Listtraverse (L);

printf ("Listlength (l) =%d \ n", Listlength (l));
I=listempty (L);
printf ("L is empty: i=%d (1: Yes 0: NO) \ n", i);

I=clearlist (&L);
printf ("Empty L: Listlength (L) =%d\n", Listlength (l));
I=listempty (L);
printf ("L is empty: i=%d (1: Yes 0: NO) \ n", i);

for (j=1;j<=10;j++)
Listinsert (&L,J,J);
printf ("After the footer of L is inserted in 1~10: L.data=");
Listtraverse (L);

printf ("Listlength (l) =%d \ n", Listlength (l));

Listinsert (&l,1,0);
printf ("The table header in L is inserted after 0: L.data=");
Listtraverse (L);
printf ("Listlength (l) =%d \ n", Listlength (l));

Getelem (l,5,&e);
printf ("The value of the 5th element is:%d\n", e);
for (j=3;j<=4;j++)
{
K=locateelem (L,J);
if (k)
printf ("The value of%d element is%d\n", k,j);
Else
printf ("no element with a value of%d \ n", j);
}

K=listlength (L); /* k for table length */
for (j=k+1;j>=k;j--)
{
I=listdelete (&l,j,&e); /* Delete the first J data */
if (i==error)
printf ("Delete%d data failed \ n", j);
Else
printf ("Delete element%d value is:%d\n", j,e);
}
printf ("Output the elements of L sequentially:");
Listtraverse (L);

j=5;
Listdelete (&l,j,&e); /* Delete 5th Data */
printf ("Delete element%d value is:%d\n", j,e);

printf ("Output the elements of L sequentially:");
Listtraverse (L);

I=clearlist (&L);
printf ("\ n empty L: Listlength (L) =%d\n", Listlength (l));
Createlisthead (&l,20);
printf ("Whole creation of elements of L (head interpolation method):");
Listtraverse (L);

I=clearlist (&L);
printf ("\ n Delete l after: Listlength (l) =%d\n", Listlength (l));
Createlisttail (&l,20);
printf ("Whole creation of elements of L (tail interpolation method):");
Listtraverse (L);


return 0;
}

Lookup: Sequential structure constant order chain linear order of linear tables

Insert and Delete: Sequential structure of linear table linear order-chain constant order

C-language implementation of chain storage structure for linear tables

Related Article

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.