1. Overview:
C language one-way linked list (single linked list) is one of the linked list, its characteristics are linked to the direction of the linked list is one-way, access to the linked list through sequential reading from the head start.
One of the simplest in a list is a one-way list, which contains two domains, an information field, and a pointer field. This link points to the next node in the list, and the last node points to a null value.
As shown in the following illustration:
A one-way list contains two values: The value of the current node and a link to the next node
A one-way list of nodes is divided into two parts. The first section saves or displays information about the node, and the second part stores the address of the next node. One-way linked lists can only be traversed in one direction.
The most basic structure of a linked list is to save the data at each node and the address of the next node, save a special end tag at the last node, save a pointer to the first node in a fixed location, and sometimes a pointer to the last node. When you look up a node, you need to start from the first node and access the next node, all the time, to the desired location. But you can also save the location of a node in advance and then access it directly. Of course, if only access to the data is not necessary, as well as the list to store pointers to the actual data. This is typically done to access the next or previous node in the linked list.
Relative to the doubly linked list, this ordinary, each node has only one pointer to the linked list is also called one-way linked list, or single linked list, usually used in every time only in order to traverse the list of times (such as the graph of the adjacency table, usually in a fixed order access).
2. Program implementation:
/* c2-2.h linear table of the single linked table storage structure
/struct Lnode
{
elemtype data;
struct Lnode *next;
typedef struct LNODE *linklist; /* Another way to define linklist * *
/* BO2-2.C basic operation of single linked list (storage structure defined by C2-2.h) (12)/Status initlist (linklist *l) {/* operation result: Construct an empty linear table L/*l= (linklist) Mallo C (sizeof (struct lnode));
/* Generate header node, and make L point to this head node/if (!*L)/* Storage allocation failure/exit (OVERFLOW); (*l)->next=null;
/* Pointer field is empty/return OK; Status destroylist (linklist *l) {*/* initial condition: Linear table L already exists.
Operation Result: Destroy linear table L/linklist q;
while (*l) {q= (*l)->next;
Free (*l);
*l=q;
return OK; The Status clearlist (linklist L)/* Does not change L/{*/* initial conditions: Linear table L already exists.
Operation Result: Reset L to Empty table * * * linklist p,q; p=l->next;
/* p points to the first node/while (p)/* not to the footer/{q=p->next;
Free (p);
p=q; } l->next=null;
/* head node pointer field is empty/return OK; Status Listempty (linklist l) {//* initial condition: Linear table L already exists.
Operation Result: If L is an empty table, return True, otherwise return false */if (l->next)/* NOT NULL/false;
else return TRUE; int Listlength (linklist l) {*/* initial condition: Linear table L already exists.
Operation Result: Returns the number of data elements in L/int i=0; Linklist p=l->next;
/* p points to the first node/while (p)/* not to the footer/{i++;
p=p->next;
return i; } StATUs Getelem (linklist l,int i,elemtype *e)/* algorithm 2.8/{/* L is the head pointer of a single linked table with a leading node. When the first element exists, its value is assigned to E and returned OK, otherwise the error */int j=1 is returned; /* J for Counter */linklist p=l->next;
/* p points to the first node/while (P&&J<I)/* CIS pointer to look backward until p points to the I element or P is null/{p=p->next;
j + +; } if (!p| |
J>I)/* I element does not exist/return ERROR; *e=p->data;
* * Take the first element of I/return OK; int Locateelem (linklist l,elemtype e,status (*compare) (Elemtype,elemtype)) {/* Initial condition: Linear table L already exists, compare () is a data element decision function (satisfies 1
* * * If such data element does not exist, then the return value is 0 */int i=0;
Linklist p=l->next;
while (p) {i++;
if (compare (p->data,e))/* Find such data element/return I;
p=p->next;
return 0; Status Priorelem (linklist l,elemtype cur_e,elemtype *pre_e) {//////////////////////////* Operation Result: If Cur_e is the data element of L, and is not the first one, then /* p points to the first node/while (P->next)/* p means the node has successor/{q=p->next;/* qFor the successor of P/if (q->data==cur_e) {*pre_e=p->data;
return OK; } p=q;
/* p move backward/} return infeasible;
/* p points to the node of the first node/while (P->next)/* p has a successor/{if (p->data==cur_e) {*next_e=p->next->data;
return OK;
} p=p->next;
return infeasible; Status Listinsert (linklist l,int i,elemtype e)/* algorithm 2.9.
Do not change L/{* * * insert element E/int j=0 before the first position in the single chain linear table L of the leading node.
Linklist p=l,s;
while (p&&j<i-1)/* Find the first i-1 node/{p=p->next;
j + +; } if (!p| |
J>I-1)/* I is less than 1 or greater than the table length */return ERROR; S= (linklist) malloc (sizeof (struct lnode)); /* Generate new node * * s->data=e;
* * Insert L/s->next=p->next;
p->next=s;
return OK; Status listdelete (linklist l,int i,elemtype *e)/* algorithm 2.10. Do not change l * * * * * in the lead node of the single chain lineIn the table L, delete the first element and return its value by E/int j=0;
Linklist p=l,q;
while (p->next&&j<i-1)/* Look for the first node and let P point to its forward/{p=p->next;
j + +; } if (!p->next| |
J>I-1) * * Delete location unreasonable * * return ERROR; q=p->next;
/* Delete and release the node * * p->next=q->next;
*e=q->data;
Free (q);
return OK; The parameter type of the Status Listtraverse (*VI) (elemtype)/* VI is elemtype, and the parameter type of the corresponding function in bo2-1.c is elemtype& different */{/* initial bar Component: Linear table L already exist////////////////////////////////
Once VI () fails, the operation fails/linklist p=l->next;
while (p) {VI (P->DATA);
p=p->next;
printf ("\ n");
return OK;
}
/* ALGO2-5.C Main program * * * #include "c1.h" typedef int elemtype; #include "c2-2.h" #include "bo2-2.c" void CreateList (linklist *l,int N)/* Algorithm 2.11/{//* Inverse order (inserted in the table header) input the value of n elements, the establishment of a single chain with the header structure of linear table
L */int i;
Linklist p;
*l= (linklist) malloc (sizeof (struct lnode)); (*l)->next=null;
/* First set up a leading node of the single linked list/printf ("Please enter%d data \ \ n"); for (i=n;i>0;--i) {p= (linklist) malloc (sizeof (struct));/* Generate new node */Lnode ("%d", scanf);/* INPUT element value * * p->next= (*L)->next;
/* insert to Table head//(*L)->next=p;
} void CreateList2 (linklist *l,int N) {/* * * positive sequence (inserted at the end of the table) to enter the value of n elements, to establish a single chain with the head structure of the linear table */int i;
Linklist p,q; *l= (linklist) malloc (sizeof (struct lnode));
/* Generate head Node * * (*L)->next=null;
Q=*l;
printf (Please enter%d data \ n);
for (i=1;i<=n;i++) {p= (linklist) malloc (sizeof (struct lnode));
scanf ("%d", &p->data);
q->next=p;
q=q->next;
} p->next=null; } void Mergelist (linklist la,linklist *lb,linklist *LC)/* Algorithm 2.12/{/* The elements of La and Lb, which are known as single chain linear tables, are arranged in a non descending order of values.
*/ /* Merge La and Lb the elements of the new single chain linear table LC,LC are also arranged by value not descending/linklist pa=la->next,pb= (*lb)->next,pc; *lc=pc=la;
/* Use the head node of LA as the head node of the LC */while (PA&&PB) if (pa->data<=pb->data) {pc->next=pa;
PC=PA;
pa=pa->next;
else {pc->next=pb;
PC=PB;
pb=pb->next; } pc->next=pa?pa:pb; * * Insert the remaining section/free (*LB);
/* Release LB head node * * LB=NULL;
} void Visit (elemtype c)/* Listtraverse () called function (type to be consistent) */{printf ("%d", c);
} void Main () {int n=5;
Linklist LA,LB,LC;
printf ("In non-descending order,"); CreateList2 (&la,n); /* Positive bit order input n element value/printf ("la=");
/* Output list La content/Listtraverse (la,visit);
printf ("In non-ascending order,"); CreateList (&lb,n); /* Reverse-order Input n elements of the value/printf ("lb=");
/* Output list Lb content/Listtraverse (lb,visit); Mergelist (LA,&LB,&LC); * * Merge LA and lb in non descending order to get new table Lc/printf ("lc=");
/* Output List Lc contents */Listtraverse (lc,visit); }