Header file Head.h
#include <string.h> #include <ctype.h> #include <malloc.h>/* malloc () */#include <limits.h>/* Int_max */#include <stdio.h>/* EOF (=^z or F6), NULL */#include <stdlib.h>/* atoi () */#include <io.h>/* EOF () */#include <math.h>/* Floor (), ceil (), ABS () */#include <process.h>/* exit () *//* function Result status code */#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define infeasible-1/* #define OVERFLOW-2 Since the value of math.h defined in OVERFLOW is 3, remove this Line */typedef int Status; /* Status is the type of function whose value is the function result status code, such as OK, etc. */typedef int Boolean; /* Boolean is a Boolean type whose value is True or false */typedef int elemtype;//defines different storage types by defining different types of typedef struct LNODE{ELEMTYPE data;struct Lnode *next;} Lnode, *linklist;//typedef struct Lnode *linklist; Status initlist_l (linklist *l); Status destorylist_l (linklist *l); Status clearlist_l (linklist *l); Boolean listempty_l (linklist l); int listlength_l (linklist l); Status getelem_l (linklist l, int i, elemtype *e); int locateelem_l (linklist l, elemtype e); Status priorelem_l (LiNklist L, Elemtype cur_e, Elemtype *pre_e); Status nextelem_l (linklist L, Elemtype cur_e, Elemtype *next_e); Status listinsert_l (linklist *l, int i, elemtype e); Status listdelet_l (linklist *l, int i, elemtype *e); Status listtraverse_l (linklist L); void createlist_r_l (linklist *l, int n); void createlist_f_l (linklist *l, int n); void Me rgerlist_l_l (Linklist A, linklist B, linklist *c); void mergerlist_h_l (Linklist A, linklist *b, linklist *C);
Algorithm implementation
#include "head.h"//single-linked list with head node status initlist_l (Linklist *l) {//Operation result: Constructs an empty linear table l*l = (linklist) malloc (sizeof (Lnode)); ! (*l)) {printf ("Construct linear table failed!"} \ n "); exit (OVERFLOW);} (*l)->next = Null;return OK;} Status destorylist_l (linklist *l) {//Initial condition: Linear table L already exists, each pointer to a successor element is free//operation result: Destroy linear table Llinklist P;while (*l) {p = (*l)- >next;free (*l);//The head node should also be released *l = p;} return OK;} Status clearlist_l (linklist *l) {//Initial condition: Linear table L already exists//operation result: Resets L to empty table, does not change L, that is, simply release all the pointers in the linked list but retain them in the form/ Note the contrast with destorylist linklist p, q;p = (*l)->next;//p points to the first node while (p)//To save the approximate structure {q = P->next;free (p);p = q;} (*l)->next = Null;return OK;} Boolean listempty_l (linklist L) {//Initial condition: Linear table L already exists//operation result: Returns True if L is empty, otherwise returns Fasleif (L->next) return False;elsereturn TRUE;} int listlength_l (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;//The first data node of a single-linked list while (p) {i++;p = P->next;} return i;} Status getelem_l (linklist L, int i, Elemtype *e) {//Initial condition: Linear table L already exists and 1<= I <= listlength (l), L is the single-link head pointer for the lead node// Operation Result: Returns the value of the I data element in L with e linklist p = L->next;int n = 1;while (p && N < i)//find the first element {p = p->next;n++;} if (!p | | n > I)//search for failure then exit the function return error;*e = P->data;return OK;} int locateelem_l (linklist L, elemtype e) {//Initial condition: Linear table L already exists//operation result: Returns the bit order of the first Data element in L that is equal to element e, and returns 0int n = 0 if no such element exists; Linklist p = l->next;//first node, non-head node while (p) {n++;if (P->data = = e) return n;p = P->next;} return 0;} Status priorelem_l (linklist L, Elemtype cur_e, Elemtype *pre_e) {//Initial condition: Linear table L already exists//operation result: If Cur_e is the data element of L and not the first one, then Pre_ E returns its predecessor, otherwise fails, pre_e undefined linklist p = l->next;//represents the precursor of the current element linklist Q = p->next;//represents the current element while (Q)//starts with the second element {if (q-> data = = cur_e) {*pre_e = P->data;return TRUE;} p = q;q = Q->next;} return infeasible;} Status nextelem_l (linklist L, Elemtype cur_e, Elemtype *next_e) {//Initial condition: Linear table L already exists//operation result: If Cur_e is the data element of L and not the last one, use Pre_ E returns its successor, otherwise the operation fails, next_e meaningless linklist p = l->next;while (p) {if (P->data = = cur_e) {p = p->next;if (p) {*next_e = P->d Ata;return TRUE;} Elsereturn infeasible;} p = p->next;} return infeasible;} Status Listinsert_l (linklist *l, int i, elemtype e) {//Initial condition: Linear table L already exists, 1<= I <=listlength (L) +1//operation result: Inserting a new data element e before the I position in L, L length plus 1LinkList p = *l;int n = 1; Linklist t;if (i<1 | | i>listlength_l (*L) + 1) return Error;while (P && n<i)//Find the node before the bit order I, which is the node of the i-1 {p = p-> next;n++;} if (!p | | n > i) return false;t = (linklist) malloc (sizeof (struct lnode)); t->data = E;t->next = P->next;p->ne XT = T;return OK;} Status listdelet_l (linklist *l, int i, Elemtype *e) {//Initial condition: Linear table L already exists//operation result: In single-linked table L of head node, delete element I, and e return its value linklist p = *l, q; int n = 1;if (i<1 | | i>listlength_l (*L) + 1) return Error;while (P->next && n < i)//Find the first node, and P point to its precursor {p = p->next;n++;} if (! ( P->next) | | n > I) return false;q = P->next;p->next = Q->next;*e = Q->data;free (q); return OK;} Status listtraverse_l (linklist L) {//Initial condition: Linear table L already exists//operation result: an element in the output linear list linklist p = l->next;while (p) {printf ("%d", p- >data);p = P->next;} printf ("\ n"); return OK;} Algorithm 2.11 algorithm for constructing single-linked list from footer to table header void createlist_r_l(linklist *l, int n) {linklist p;*l = (linklist) malloc (sizeof (Lnode));(*l)->next = null;printf ("Input%d element value" space bar separated by ":", N); for (; n >= 1; n-- {p = (linklist) malloc (sizeof (Lnode)), scanf_s ("%d", &p->data);p->next = (*l)->next;//inserted into the header (*l), Next = P;}} Algorithm 2.11-1 algorithm void createlist_f_l (linklist *l, int n) {linklist p, q; (*l) = (linklist) malloc (sizeof (struct) of a single linked list from the head to the end of the table Lnode));(*l)->next = Null;q = (*l),//q points to the head node of the empty table (equivalent to the tail node) printf ("Please enter%d data" space bar separated "", N); for (int i = 1; I <= n; i++) {p = (linklist) malloc (sizeof (struct lnode)), scanf_s ("%d", &p->data), q->next = p;//inserting new nodes into the footer q = q->next;// Q points to the tail node, forward}q->next = null;//The last node of the pointer field is empty}//algorithm 2.12 merges two ordered linked lists into an ordered list, drawing on the idea of sequential lists, inefficient void mergerlist_l_l (linklist A, Linklist B, linklist *c) {//known condition: The elements in single-linked list A and B are sorted by non-descending order//output: Merge A and B to get new single-linked list c,c elements are also sorted by value non-descending int sA = listlength_l (A), SB = listlength_l (B); int nA = 1, NB = 1, NC = 1; Elemtype EA, Eb;while (na <= sA && nB <= sB) {getelem_l (A, NA, &ea),//low efficiency in single-linked list getelem_l (B, NB,&EB);//low efficiency in single-linked list if (EA < EB) {listinsert_l (C, NC, EA); na++;nc++;} else{listinsert_l (C, NC, EB); nb++;nc++;}} while (Na <= sA) {getelem_l (A, NA, &ea); listinsert_l (C, NC, EA); na++;nc++;} while (NB <= SB) {getelem_l (B, NB, &eb); listinsert_l (C, NC, EB); nb++;nc++;}} Algorithm 2.12 Merges two ordered linked lists into an ordered list, taking full advantage of the example linked list, high efficiency void mergerlist_h_l (linklist A, linklist *b, linklist *c) {linklist pa = a->next, PB = (*b)->next, pc;*c = PC = A;while (PA&&PB) if (pa->data <= pb->data) {pc->next = pa;//merges the points of the PA to C In PC = PA;//PC pointing to the last node of table C PA = pa->next;} Else{pc->next = pb;pc = PB;PB = Pb->next;} Pc->next = PA? pa:pb;//Insert remaining segment free (*b);//Release B's head node (*b) = NULL;}
Test file TEST.c
#include "head.h" void Main ()/* Except for a few output statements, the master and main2-1.c are much like */{linklist L;/* differs from main2-1.c */elemtype E, E0; Status I;int J, k;i = initlist_l (&l), for (j = 1; J <= 5; j + +) i = listinsert_l (&l, 1, j);p rintf ("After the table header in L is inserted in the following: l ="); Listtraverse_l (L); /* The element is called visit (), the value of the output element */i = listempty_l (L);p rintf ("\nl is empty: i=%d (1: Yes 0: NO) \ n", i), i = clearlist_l (&l);p rintf (" After emptying L: l= "); Listtraverse_l (l); i = listempty_l (l);p rintf ("L is empty: i=%d (1: Yes 0: NO) \ n", I); for (j = 1; J <=; j + +) listinsert_l (&l, J, j);p rintf ("At the end of L 1~10: l="); Listtraverse_l (L); Getelem_l (L, 5, &e);p rintf ("\ n the value of the 5th element is:%d\n", e);//Detection locateelem_lfor (j = 0; J <= 1; j + +) {k = locateelem_l (L, j); if (k) printf ("The value of element%d is%d\n", K, j); elseprintf ("element with no value%d \ n", j);} for (j = 1; J <= 2; j + +)/* Test the first two data */{getelem_l (L, J, &E0);/* Assign the first J data to E0 */i = priorelem_l (L, E0, &e);/* Ask for E0 The precursor */if (i = = infeasible) printf ("element%d no precursor \ n", E0); elseprintf ("The Precursor of element%d:%d\n", E0, E);} for (j = listlength_l (l)-1; J <= listlength_l (L); j + +)/*The last two data */{getelem_l (L, J, &E0);/* Assigns the first J data to E0 */i = nextelem_l (L, E0, &e);/* E0 's successor */if (i = = infeasible) printf ( "Element%d no successor \ n", E0); elseprintf ("Successor of element%d:%d\n", E0, E);} K = listlength_l (L); /* k for Table length */for (j = k + 1; j >= K; j--) {i = listdelet_l (&l, J, &e);/* Delete section J data */if (i = = ERROR) printf ("Delete%d data Failed \ n ", j); elseprintf (" deleted element:%d\n ", e);} printf ("Output the elements of L sequentially:"); Listtraverse_l (L);D estorylist_l (&l);p rintf ("\ n Destroy L: l=%u\n", l); system ("pause");
Running Result:
The table header in L is inserted after the following: L=5 4 3 2 1L is empty: i=0 (1: Yes 0: NO) after emptying L: L=l is empty: I=1 (1: Yes 0: NO) at the end of L after inserting 1~10: l=1 2 3 4 5 6 7 8 9 10 The value of the 5th element is: 5 The element with no value 0 has a value of 1 element 1 No precursor element 2 has a precursor of: 1 element 9 is followed by: 10 elements 10 No subsequent deletions 11th data failed to delete the element is: 10 output L element: 1 2 3 4 5 6 7 8 9 destroy L after: L=0 Press any key to continue ...
Test file test2.c
#include "head.h" void Main () {linklist A, B, C; Linklist D, E, f;int n = 0;initlist_l (&a); initlist_l (&b); initlist_l (&c); for (int i = 1; I <= 5; i++) listins ert_l (&a, I, (i + 3)), for (int i = 1; i <=; i++) listinsert_l (&b, I, i);p rintf ("elements in single-linked list A:"); Listtraverse_l (a);p rintf ("\ n the elements in single-chain list B are:"); Listtraverse_l (B); Mergerlist_l_l (A, B, &c);p rintf ("\ n elements in single-linked list C:"); Listtraverse_l (C);p rintf ("\ n");p rintf ("Enter the number of elements in a single-linked list D:"), scanf_s ("%d", &n); createlist_f_l (&d, N);p rintf ("Enter the number of elements in the single-linked list E n:"); scanf_s ("%d", &n); createlist_f_l (&e, N); Mergerlist_h_l (D, &e, &f);p rintf ("The elements in the \ n single-linked list F are:"); Listtraverse_l (F);p rintf ("\ n"); system ("Pause");}
Running Result:
Elements in a single-linked list A are: 4 5 6 7 8 elements in a single-link list B are: 1 2 3 4 5 6 7 8 9 10 elements in single-linked list C are: 1 2 3 4 4 5 5 6 6 7 7 8 8 9 10 Enter the number of elements in the single-linked list D N:3 Please enter 3 data "space bar separated" 1 2 3 Please enter the number of elements in the single-linked list e N:3 Please enter 3 data "Space Key separated" 4 5 6 single-linked list F elements are: 1 2 3 4 5 6 Please press any key to continue ...
Chain representation and implementation of linear tables----linear (single) linked list