#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE-1
#define OVERFLOW-2
#define NULL 0
typedef int STATUS;
typedef int ELEMTYPE;
Single-linked table storage structure------Linear table--------
typedef struct LNODE
{
Elemtype data;//Data domain
struct Lnode *next; Pointer field
}lnode,*linklist;
------(head interpolation) reverse-Enter the value of n elements and create a single-linked list L with a lead node
void creatlist_l (linklist &l,int N)
{
int i;
Lnode *p;
L = (linklist) malloc (sizeof (Lnode));
L->data = 0; The data field content in the header node is 0 (that is, the table has a length of 0), and the data fields in the head node record the length of the table
L->next = NULL; Create a single linked list L with a lead node.
printf ("Please enter the data in the single linked list in reverse order: \ n");
for (i = n;i>0;--i)
{
p = (linklist) malloc (sizeof (Lnode)); Create a new node
scanf ("%d", &p->data);
P->next = l->next;
L->next = p; New node inserted into the table header
l->data++; Increase the length of the table
}
}//creatlist_l
Tail interpolation method
Status creatlist_l (linklist &l)
{
Status listinsert_l (linklist &l,int i,elemtype e);
Elemtype temp;
L= (linklist) malloc (sizeof (Lnode));
if (! L) return ERROR;
l->data=0;
l->next=null;
printf ("Please Input data (9999) ending\n");
scanf ("%d", &temp);
while (temp!=9999)
{
listinsert_l (l,l->data+1,temp);
scanf ("%d", &temp);
}
return OK;
}//creatlist_l
Find------single-linked table L with header nodes
Status getelem_l (linklist l,int i,elemtype &e)
{
Lnode *p;
Int J;
p = l->next;
j = 1;
while (P && j<i)
{
p = p->next;
++j;
}
if (!p | | j>i)
return ERROR; The first element does not exist
E = p->data;
return OK;
}//getelem_l
------insertion of single-linked table L with header nodes
Status listinsert_l (linklist &l,int i,elemtype e)
{
Lnode *p,*s;
Int J;
p = L;
j = 0;
while (P && j<i-1)
{
p = p->next;
j + +;
}
if (!p | | j>i-1)
return ERROR;
s = (linklist) malloc (sizeof (Lnode)); Generate new nodes.
S->data = e;
S->next = p->next;
P->next = s;
l->data++; Increase the length of the table
return OK;
}//listinsert_l
------Deletion with header nodes
Status listdelete_l (linklist &l,int i,elemtype &e)
{
Lnode *p,*q;
int j = 0;
p = L;
while (P->next && j<i-1)
{//search for the first node and point P to its predecessor
p = p->next;
++j;
}
if (! ( P->next) | | J>I-1)
return ERROR;
Q = p->next;
P->next = q->next;
E = q->data;
Free (q); Release Node Q
l->data--; Table length minus One
return OK;
}//listdelete_l
------Traversal with Table header nodes
Status traverse_l (linklist L)
{
Lnode *p = l->next;
printf ("This list contains%d elements\n", l->data);
while (p)//cannot be written as while (P->next)
{
printf ("%5d->", p->data);
p = p->next;
}
printf ("null\n");
return OK;
}//traverse_l
------table length for single-linked list with header nodes
int length_l (linklist L)
{
int num = 0;
Lnode *p = l->next;
while (p)
{
num++;
p = p->next;
}
return num;
}//length_l (linklist L)
------Remove elements Ascending the list of leading nodes of the node L median value greater than mink is less than maxk all elements
Status Delete_between (linklist &l,int mink,int maxk)
{
Linklist p,q;
p = L;
while (P->next && p->next->data<=mink)
p = p->next; P is the last element less than mink.
if (P->next)
{
Q = p->next;
while (Q->DATA<MAXK)
{
Q = q->next; Q is the first element not less than MAXK
l->data--;
}
P->next = q;
}
return OK;
}
int main ()
{
Linklist L;int n,k,i,i1,i2,e,e1,e2,j1,j2;
printf ("Create a list of the following two ways, please select \ n");
printf ("1 Select Head Interpolation Method 2 Select the tail interpolation method \ n");
printf ("Please enter your selection ...");
scanf ("%d", &k);
if (k==1)
{
printf ("Please enter the length of the initial single-linked list:");
scanf ("%d", &n);
creatlist_l (L,n);
}
if (k==2)
{
creatlist_l (L);
}
Traverse_l (L);
printf ("The length is:%d\n", length_l (L));
printf ("Get elem I:");
scanf ("%d", &i);
getelem_l (l,i,e);
printf ("The no.%d elem is:%d\n", i,e);
printf ("Insert position:");
scanf ("%d", &i1);
printf ("Insert number:");
scanf ("%d", &e1);
listinsert_l (L,I1,E1);
Traverse_l (L);
printf ("Delete position:");
scanf ("%d", &i2);
Listdelete_l (L,I2,E2);
Traverse_l (L);
printf ("\ndelete between (A, B):");
scanf ("%d,%d", &j1,&j2);
printf ("The results:%d\n", Delete_between (L,J1,J2));
Traverse_l (L);
printf ("\ n");
return 0;
}
The basic operation of the linear table (it is possible to choose whether to use the head interpolation method or the tail interpolation method when creating the initial linked list)