Basic operation of linear table---linked list
#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 NULL 0
typedef int status;//function Result status code
typedef int ELEMTYPE;//Data Element type
Single-linked table storage structure------Linear table--------
typedef struct LNODE
{
Elemtype data;//Data domain
struct Lnode *next;//pointer field
}lnode,*linklist;
------Reverse-order 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 length of the table is 0), and the data field in the head node records the length of the table
L->next = null;//Create a single-link table with the lead node L
printf ("Please input data:\n");
for (i = n;i>0;--i)
{
p = (linklist) malloc (sizeof (Lnode));//Generate New nodes
scanf ("%d", &p->data);
P->next = l->next;
L->next = p;//new node inserted into the table header
l->data++;//increasing the length of the table
}
}//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;//I 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
------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
P->next = q;
}
return OK;
}
int main ()
{
Linklist L;int n,i,i1,i2,e,e1,e2,j1,j2;
printf ("Please input the number in the list:");
scanf ("%d", &n);
creatlist_l (L,n);
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;
}
"Experimental class---The basic operation of single-linked list"