Data Structure-bidirectional list and cyclic list

Source: Internet
Author: User

Data Structure-bidirectional list and cyclic list
Cyclic linked list

Circular Linked List (Circular Linked List): A chain table connected to the beginning and end. Its feature is that the pointer field of the last node points to the head node of the linked list, and the pointer field of the entire linked list links to a ring. You can find other nodes in the linked list from any node of the cyclic linked list, making table processing more convenient and flexible.

Cyclic Linked List Operation
For single-loop linked lists, except for the merging of linked lists, other operations are basically the same as those for single-linear linked lists. You only need to make the following modifications based on the single-Linear Linked List Operation algorithm:
(1) determine whether it is an empty linked list: head-> next = head;
(2) determine whether it is the end node of the Table: p-> next = head;

Two-way linked list
Double Linked List: indicates that two pointer fields are set up in each node of the Linked List:

A pointer domain prior pointing to its direct frontend,
A pointer to its direct successor domain next.
The linked list has two different links, which are called a two-way linked list.
Similar to a single-linked list, adding a header pointer to a two-way linked list can also facilitate some operations on the double-linked list.
Link the header and tail nodes to form a circular linked list, which is called a bidirectional circular linked list.
A two-way linked list is introduced to overcome the unidirectional defect of a single-chain table.
The structure of a two-way linked list is symmetric. If p points to a node in a two-way linked list, its symmetry can be described as follows:
(P-> prior)-> next = p = (p-> next)-> prior;

The storage location of node p is stored in the direct successor pointer domain of node p-> prior, it is also stored in the direct frontend pointer domain of its direct successor node p-> next.
① If only the direct precursor node is marked during insertion, you must note that the order of the Hooks is: "First right, then left ". Some statement groups are as follows: s = (DulNode *) malloc (sizeof (DulNode); s-> data = e; s-> next = p-> next; p-> next-> prior = s; p-> next = s; s-> prior = p; /* the hook chain order is very important */② during insertion, the direct precursor node p and the direct successor node q are also pointed out. When the hook chain is inserted, there is no need to pay attention to the order. Some statement groups are as follows: s = (DulNode *) malloc (sizeof (DulNode); s-> data = e; p-> next = s; s-> next = q; s-> prior = p; q-> prior = s;

Delete a node in a two-way linked list
If the node to be deleted is set to p, a new secondary pointer variable is not introduced during the deletion, and the link can be broken before the node is released. Some statement groups are as follows:

p->prior->next=p->next;p->next->prior=p->prior;free(p);

Note:
Unlike the insert and delete operations on a single-chain table, the insertion and deletion operations on a two-way linked list must modify the pointer fields in both directions.

Add two polynomial linked lists and generate a new linked list to store the added results. The original two polynomial linked lists still exist and do not change, other operations on the original two polynomials will not be affected.

Algorithm Description: Ploy * add_ploy (ploy * La, ploy * Lb)/* adds a polynomial represented by the header pointer La and Lb, generate a New Result polynomial */{ploy * Lc, * pc, * pa, * pb, * p; float x; Lc = pc = (ploy *) malloc (sizeof (ploy); pa = La-> next; pb = Lb-> next; while (pa! = NULL & pb! = NULL) {if (pa-> expn <pb-> expn) {p = (ploy *) malloc (sizeof (ploy )); p-> coef = pa-> coef; p-> expn = pa-> expn; p-> next = NULL; /* generate a new result node and assign a value */pc-> next = p; pc = p; pa = pa-> next ;} /* Insert the generated node to the end of the result linked list. pa points to the next node */if (pa-> expn> pb-> expn) {p = (ploy *) malloc (sizeof (ploy); p-> coef = pb-> coef; p-> expn = pb-> expn; p-> next = NULL; /* generate a new result node and assign a value */pc-> next = p; pc = p; pb = pb-> next ;} /* Insert the generated node to the end of the result linked list. pb points to the next node */if (pa-> expn = pb-> expn) {x = pa-> coef + pb-> coef; if (abs (x) <= 1.0e-6)/* The coefficient is 0 */{pa = pa-> next; pb = pb-> next;}/* pa, pb directly successor node */else/* coefficient and not 0 respectively, the generated node is inserted to the end of the result linked list, pa, petabytes direct successor node */{p = (ploy *) malloc (sizeof (ploy); p-> coef = x; p-> expn = pb-> expn; p-> next = NULL;/* generate a new result node and assign a value */pc-> next = p; pc = p; pa = pa-> next; pb = pb-> next ;}}/ * end of while */
If (pb! = NULL) while (pb! = NULL) {p = (ploy *) malloc (sizeof (ploy); p-> coef = pb-> coef; p-> expn = pb-> expn; p-> next = NULL;/* generate a new result node and assign a value */pc-> next = p; pc = p; pb = pb-> next ;} if (pa! = NULL) while (pa! = NULL) {p = (ploy *) malloc (sizeof (ploy); p-> coef = pb-> coef; p-> expn = pa-> expn; p-> next = NULL;/* generate a new result node and assign a value */pc-> next = p; pc = p; pa = pa-> next ;} return (Lc );

Related Article

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.