Redefining linear list and its basic operation from the angle of practical application "5" _null

Source: Internet
Author: User

There is a mergelist () function. It's one of the most important functions. It's also the hardest, and by this function, many other functions are called.

Through this procedure, it is also a grasp of the overall view of the program.

Used a lot of pointers. Hope everybody slowly, calm down to see. Or you'll get dizzy by the pointer.

The first time in C to write this program more than 300 lines, debugging is good too laborious.

However, always feel that the use of C in the various modular program design skills Some lack of skill, need guidance. There are related books of hope to recommend.

/* Author:star data:2011.04.09 to redefine the linear list and its basic operation from the practical angle of application * * * #include <iostream> #include <stdio.h> #include <malloc.h> using namespace std; #define ERROR 0 #define OK 1 typedef int ELEMTYPE; typedef int STATUS; typedef struct LNODE {elemtype data; struct Lnode *next;} *link,*position; The typedef struct//linked list Type {link head,tail;//points respectively to the header node in the linear list and the last node int len;} linklist; Status makenode (link &p, Elemtype e) {//Assign a node with P pointing to the value E and return OK, if the allocation fails, return the error P = (link) malloc (sizeof (Lnode)); F (!p) {cout << "allocation node failed!"; return ERROR; else p->data = e; return OK; } void Freenode (Link &p) {//release P refers to node free (p); p = NULL;} Status initlist (linklist &l) {Link p; if (! (Makenode (P,-1)) return ERROR; P->next = NULL; L.head = p; L.tail = p; L.len = 0; return OK; } void Insfirst (linklist &l, link h, link s) {//known H point to Linked header node, insert S-point node after head node s->next = h->next; h->next = s ; L.len + +; if (S->next ==null) L.tail = s; } void Delfirst (LinklisT &l, link h, link &q {//known H points to the head node of the linear list, deletes the first node in the list, and returns if (!) with Q. L.len) {cout << ' List is empty! ' <<endl; return;} q = h->next; H->next = q->next; Q->next = null;//This sentence is critical, because Q->next =null in the Append function will be an error. L.len--; if (h->next = NULL) L.tail = h; } void Append (Linklist &l, link s) {//links a string of nodes referred to by pointer S (to each other as pointers) to the last node//In the list L, and changes the tail pointer of the list L to point to the new node int i = 1; Link p =s; while (P->next) {i++ p = p->next;} L.len = i; L.tail->next = s; L.tail = p; } void Remove (linklist &l, link &q) {//delete the tail node in the list L, and with Q return//change the tail pointer of the list L to point to the new tail node if (L.len = = 0) {cout << "table is empty "<< Endl; Return Link p = l.head; while (P->next!= l.tail) p = P->next; Q = P->next; P->next = NULL; L.tail =p; l.len--; } void Setcurelem (link &p,elemtype e) {//is known to point to a node in the list with E update p->data = e;} Elemtype Getcurelem (Link p) {//Returns the value of the data element in the node referred to by P->data;} int listlength (linklist L) {return l.len;} Status ListeMpty (linklist L) {//null operation if (L.len) return 1; return 0;} Position GetHead (linklist l) {//Return list L Middle head node returns l.head;} Position getlast (linklist L) {return l.tail;} Position Priorlast (linklist L, link p) {//known P points to a node in the linked list L, returns null if (P = = l.head) {cout << The node referred to in P refers to the head knot "<< Endl;" return NULL; Link q = l.head; while (q->next!= p) q = q->next; return q; Position Nextpos (linklist L, link p) {//known P points to a node in the list L, returns the direct successor of P to the node,///If no successor, returns null return P->next;} Status Locatepos (linklist l, int i, link &p) {//return p to indicate the position of node I in the list L and return OK, illegal return error if (I < 0 | | i > listlength ( L) {cout << "wrong" <<endl; return ERROR;} else {int j = 0; p = l.head; while (J < i) {p = p->next ; j + +; } return OK; } void visit (int e) {cout << e << "";} void Listtraverse (Linklist L, Void (*visit) (int)) {//traversal operation Link p = l.head p = P->next; while (p) {visit (P-&G T;data); p = P->next; } cout <&Lt Endl Status Listinsert (linklist &l, int i, elemtype e) {Link h,s; if (!) ( Locatepos (L, I-1, h)) return error;//i value is not valid if (!) ( Makenode (s,e)) return error;//is not divided into storage space Insfirst (L, H, s);//For the list starting from the first node, the I-1 node is its head node. int compare (Elemtype E1, elemtype E2) {return e1-e2;} Status mergelist (linklist &la, linklist &lb, linklist &lc, int (*compare) (Elemtype, Elemtype)) {//known single linked list L The elements of a and LB are arranged in a non descending order//merge LA and lb to get the new single linked list LC,LC elements are also arranged by the value not descending the link ha,hb; Link PA,PB; Link q = (link) malloc (sizeof (Lnode)); int a = 0, b = 0; if (! Initlist (Lc)) return ERROR; Storage space allocation Failed ha = GetHead (La); HB = GetHead (lb);//ha and HB point respectively to LA and Lb head node PA = nextpos (LA, ha); PB = Nextpos (Lb, HB);//pa and Pb point to LA and Lb respectively at the current node while (PA && pb) {a = Getcurelem (PA); b = Getcurelem (PB); if ( Compare (A, B) <= 0)//a <= B {delfirst (la,ha, q); Append (Lc, q); PA = Nextpos (La, ha); } else//a > B {delfirst (Lb, HB, Q); Append (Lc, q); PB = Nextpos (Lb,HB); } if (PA) Append (Lc, PA); else Append (LC,PB); Freenode (HA); Freenode (HB); return OK; int main () {linklist lista; Linklist Listb; Linklist LISTC; int i =0;//the return value of the judgment function is 1 or 0 initlist (lista); Initlist (LISTB); for (int j = 1; J <= 5; j)//insert element {i = Listinsert (Lista, J, J) in Lista; if (i = = 0) {cout << "input Error" <&lt ; Endl Break for (int j = 1; J <= 6; j +) {i = Listinsert (Listb, J, J + 2); if (i = = 0) {cout << "input error" << end L Break }} cout << "List A is:"; Listtraverse (lista, visit); cout << "lista length is" << listlength (lista) <<endl; cout << "Lista tail is:" << lista.tail->data << Endl; cout << "Listb for:"; Listtraverse (LISTB, visit); cout << "Listb length is" << listlength (LISTB) <<endl; cout << "Listb tail is" << listb.tail->data << Endl; Mergelist (Lista, listb, LISTC, compare); cout << "LISTC for:"; Listtraverse (LISTC, visit); cout << "Listb length is:" << listlength (LISTC) <<endl; cout << "Listb tail is:" << listc.tail->data <<endl; System ("pause"); return 0; }

VS2008 compile debugging Pass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.