24 assume that two linear tables A and B are sequentially arranged by element values. They are stored in A single-chain table, compile an algorithm to MERGE table A and table B into A linear Table C in descending order of element values (I .e., non-ascending order, allowing the table to contain elements with the same value, the Node space of the original table (table A and Table B) must be used to construct table C.
001 # define true 1
002 # define false 0
003 # define OK 1
004 # define error 0
005 # define infeasible-1
006 # define overflow-2
007 # include <stdio. h>
008 # include <malloc. h>
009 typedef int ElemType;
010 typedef int Status;
011
012 typedef struct LNode
013 {
014 ElemType data;
015 struct LNode * next;
016} LNode, * LinkList;
017
018 void CreateList_TailInsert (LinkList * L) // create a chain table by means of end insertion;
019 {
020 LinkList p, r;
021 int ch;
022 scanf ("% d", & ch );
023 r = (* L); // At the beginning, L is an empty table with only header nodes. r points to this node, that is, r points to L's tail.
024 while (ch! = 0)
025 {
026 p = (LinkList) malloc (sizeof (LNode ));
027 p-> data = ch;
028 r-> next = p; // after adding p, add p to r.
029 r = p; // point r to the tail of L again;
030 scanf ("% d", & ch );
031}
032 r-> next = NULL;
033}
034 void InitList (LinkList * L) // initialize the linked list to construct a single-chain table of the leading node;
035 {
036 * L = (LinkList) malloc (sizeof (LNode ));
037 (* L)-> next = NULL;
038}
039
040 Status Merger_AandB_toC (LinkList * La, LinkList * Lb, LinkList * Lc)
041 {
042 // use the space of the ascending single-chain table A and the ascending single-chain table B to generate the descending single-chain table C
043 LinkList pa, pb, qa, qb;
044 (* Lc)-> next = NULL;
045 qa = (* La );
046 qb = (* Lb );
047 pa = (* La)-> next;
048 pb = (* Lb)-> next;
049 while (pa & pb)
050 {
051
052 if (pa-> data <pb-> data)
053 {
054 qa = pa;
055 pa = pa-> next;
056 qa-> next = (* Lc)-> next;
057 (* Lc)-> next = qa;
058}
059 else
060 {
061 qb = pb;
062 pb = pb-> next;
063 qb-> next = (* Lc)-> next;
064 (* Lc)-> next = qb;
065}
066}
067 if (! Pa)
068 {
069 while (pb)
070 {
071 qb = pb;
072 pb = pb-> next;
073 qb-> next = (* Lc)-> next;
074 (* Lc)-> next = qb;
075}
076}
077 if (! Pb)
078 {
079 while (pa)
080 {
081 qa = pa;
082 pa = pa-> next;
083 qa-> next = (* Lc)-> next;
084 (* Lc)-> next = qa;
085}
086}
087}
088 int main ()
089 {
090 LinkList La, Lb, p, Lc;
091 int I;
092 ElemType e;
093 InitList (& La );
094 printf ("\ n enter numbers one by one, end with 0 ");
095 CreateList_TailInsert (& La );
096 p = La;
097 printf ("the new single-chain table La is printed as \ n ");
098 while (p-> next! = NULL)
099 {
100 printf ("% d \ t", p-> next-> data );
101 p = p-> next;
102}
103 InitList (& Lb );
104 printf ("\ n enter numbers one by one, end with 0 ");
105 CreateList_TailInsert (& Lb );
106 p = Lb;
107 printf ("the new single-chain table Lb is printed as \ n ");
108 while (p-> next! = NULL)
109 {
110 printf ("% d \ t", p-> next-> data );
111 p = p-> next;
112}
113 InitList (& Lc );
114 printf ("\ nLa and Lb are merged into Lc and printed as: \ n ");
115 Merger_AandB_toC (& La, & Lb, & Lc );
116 p = Lc;
117 while (p-> next! = NULL)
118 {
119 printf ("% d \ t", p-> next-> data );
120 p = p-> next;
121}
122 return 0;
123}
Drawing is very important.
Author: Yao