/* Include header file */#include <stdio.h> #include <malloc.h> #include <stdlib.h>/* macro definition and single-linked list type definition */#define Listsize 100typedef int datatype;typedef struct node{DataType data; struct Node *next;} Listnode,*linklist;void mergelist (linklist a,linklist b,linklist *c);/* A function declaration that merges elements of single-linked list A and B into C */void initlist (LinkList *head)/* Initializes the single-linked list to null. Generates a header node dynamically, and the pointer field of the head node is set to null. */{if ((*head= (linklist) malloc (sizeof (ListNode)))/* Allocates a storage space for head nodes ==null ( -1);(*/exit) *head Set the head node pointer field of a single-linked list to null */}int listempty (linklist head)//Determines whether the single-linked list is empty, by determining whether the pointer field of the head node is empty */{if (head->next==null)/* Determines whether the pointer field of a single-Link header node is empty/return 1;/* returns 1 when the single-linked list is empty, otherwise returns 0*/else return 0; ListNode *get (linklist head,int i)/* finds the first node in a single-linked list. A pointer to a successful return of the node indicates success, otherwise null indicates failure. */{listnode *p;int j;if (head) */* Before finding the element I, determine if the linked list is empty */return null;if (i<1)/* Before finding the element I, determine if the sequence number is legal */ Return Null;j=0;p=head;while (p->next!=null&&j<i) {p=p->next;j++;} if (j==i) return p;/* finds the first node, returns the pointer P*/elsereturn null;/* if no element i is found, returns Null*/}listnode *locAteelem (linklist head,datatype E)/* finds elements with an element value of E in a linear table, looks for a successful return of the corresponding element's node pointer, or returns a null representation failure. */{listnode *p;p=head->next;/* pointer p points to the first node */while (p) {if (p->data!=e)/* Finds an element equal to E, returns the ordinal */p=p->next; Elsebreak;} return p;} int Locatepos (linklist head,datatype E)/* finds elements with an element value of E in a linear table, the lookup succeeds in returning the ordinal of the corresponding element, otherwise returning 0 indicates a failure. */{listnode *p;int i;if (head)//* Before finding the element I, determine if the list is empty */return 0;p=head->next;/* pointer p points to the first node */i=1;while (p {if (p->data==e)/* Finds an element equal to E, returns the ordinal */return i;else{p=p->next;i++;}} if (!p)/* does not find an element equal to E, returns 0, indicating failure */return 0;} int insertlist (linklist head,int i,datatype e)/* Inserts a node in the single-linked list at position I, and the element value of the node is E. Insert successfully returned 1, failed to return 0*/{listnode *p,*pre;/* defines the predecessor node pointer to the element I, pointer p points to the newly generated node */int j;pre=head;/* pointer p points to the head node */j=0;while (pre- >NEXT!=NULL&&J<I-1)/* Find the I-1 node, which is the precursor node of the first node */{pre=pre->next;j++;} if (!pre)/* If not found, the insertion position error */{printf ("wrong insertion position"); return 0;} /* Newborn into a node and assign E to the data field of the node */if ((p= (listnode*) malloc (ListNode)) ==null) exit ( -1);p->data=e;/* Insert node Operation */p- >next=pre->next;pre->next=p;return 1;} int DeleteList (LiNklist head,int i,datatype *e)/* Delete nodes of position I in the single-linked list. Delete successfully returned 1, failed to return 0*/{listnode *pre,*p;int J; Pre=head; j=0; while (PRE->NEXT!=NULL&&PRE->NEXT->NEXT!=NULL&&J<I-1)/* Determine if the precursor node is found */{Pre=pre->nex T j + +; } if (j!=i-1)/* If no node location to delete is found, the deletion location error */{printf ("Delete location error"); return 0;} /* Pointer p points to the I node in the single-linked list and assigns the data field value of the node to the e*/p=pre->next;*e=p->data;/* the pointer field of the precursor node to the next node to delete the node, i.e. the node that points p to is disconnected from the single-linked list */pre- >next=p->next; Free (P);/* Releases the node to which P points */return 1;} int Listlength (linklist head) {ListNode *p; int count=0; P=head; while (p->next!=null) {p=p->next; count++; } return count;} void Destroylist (linklist head) {ListNode *p,*q; P=head; while (p!=null) {q=p; p=p->next; Free (q); }}void Main () {int I;datatype a[]={6,7,9,14,37,45,65,67};D atatype b[]={3,7,11,34,45,89}; Linklist a,b,c;/* declares single-linked list A and B*/listnode *p;initlist (&a);/* Initializes single-linked list a*/initlist (&B);/* Initializes single-linked list b*/for (i=1;i≪=sizeof (a)/sizeof (a[0]) i++)/* insert elements of array A into the single-linked list a */{if (Insertlist (a,i,a[i-1]) ==0) {printf ("illegal position"); return;} For (i=1;i<=sizeof (b)/sizeof (b[0); i++)/* Inserts the elements of array B into the single-link list B */{if (Insertlist (b,i,b[i-1]) ==0) {printf ("illegal position"); return;}} printf ("Single-linked list A has%d elements: \ n", Listlength (a)), for (I=1;i<=listlength (a); i++)/* Output single-linked list A In each element */{p=get (a,i);/* Returns the pointer to each node in a single-linked list */if (p) printf ("%4d", p->data);/* Output single-link list A In each element */}printf ("\ n");p rintf ("The element in single-chain table B has%d: \ n", Listlength (b)); for (I=1;i<=listlength (b); i++) {p=get (b,i);/* Returns a pointer to each node in single-link list B */if (p) printf ("%4d", P->data) /* * Output single-link list B for each element */}printf ("\ n"); Mergelist (A,B,&C);/* Merges elements from single-linked lists A and B into C */printf ("When merging elements of single-linked list A and B into C, there are%d elements in C: \ n", Listlength (c)); for (i=1;i<= Listlength (C); i++) {p=get (c,i);/* Returns the pointer */if (p) printf ("%4d", p->data) of each node in single-link list C, and/* Displays all elements */}printf ("\ n") in output C; Getch ();} void Mergelist (linklist a,linklist b,linklist *c)/* elements in single-linked lists A and B are not descending, merging elements from single-linked lists A and B into C, and elements in C are still in non-descending order */{listnode *pa,* pb,*pc;/* defines a pointer to a single-linked list A,b,c */pa=a->next;pb=b->next;*c=a;/* the head node of a single-linked list as the head node of C */(*C)->next=null;pc=*c;/* the smaller elements in the list A and b sequentially into the chain list C */while (PA&&PB) {if (pa->data<=pb->data) {pc->next=pa;/* If the element in a is less than or equal to the element in B, the node of the element in A is */pc=pa;pa=pa->next as the node of C;} else{pc->next=pb;/* if the element in a is greater than the element in B, the node of the element in B is */pc=pb;pb=pb->next the node of C;}} pc->next=pa?pa:pb;/* inserts the remaining nodes into C */free (b);/* Releases the head node of b */}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Non-descending sequence merging of chained tables