Programmer interview book-Dual-Link Table of Data Structure, programmer interview book

Source: Internet
Author: User

Programmer interview book-Dual-Link Table of Data Structure, programmer interview book

  1 #include<stdlib.h>  2 #include<iostream>  3 #include<stdio.h>  4 #include<string>  5 using namespace std;  6 typedef struct Node{  7 int data;  8 struct Node* next;  9 struct Node* pre; 10 }dnode; 11  12 dnode *create() 13 { 14     dnode*head,*p,*s; 15     int x,cycle=1; 16     head=(dnode*)malloc(sizeof(dnode)); 17     p=head; 18      cout<<"\nPlease input the data:"<<endl; 19     while(cycle) 20     { 21         cin>>x; 22         if(x!=0) 23         { 24             s=(dnode*)malloc(sizeof(dnode)); 25             s->data=x; 26             p->next=s; 27             s->pre=p; 28             p=s; 29         } 30         else cycle=0; 31     } 32     head=head->next; 33     head->pre=NULL; 34     p->next=NULL; 35     cout<<"\nThe head data is"<


L Implementation of double-stranded tables with data structures?

Double-linked table
1. Double Linked List (Doubly Linked List)
The dual (forward) linked list has two different links, that is, in addition to storing the next node address in the next field, each node also adds a pointer domain prior pointing to its direct forward direction.
Note:
① The double-stranded table is uniquely determined by the head of the header.
② Some operations on the double-chain table at the leading node become convenient.
③ Link the header and tail nodes to form a dual (forward) Cyclic linked list.
2. Two-way linked list node structure and form description
① Node structure (see)
② Form description
Typedef struct dlistnode {
DataType data;
Struct dlistnode * prior, * next;
} DListNode;
Typedef DListNode * DLinkList;
DLinkList head;
3. insert and delete a node in front of a two-way linked list
Statements describing the symmetry of a double-Link Table: p → prior → next = p → next → prior. Due to the symmetry of the double-link table, you can easily perform various insert and delete operations on the double-link table.
① Forward insertion of double-stranded tables
Void DInsertBefore (DListNode * p, DataType x)
{// In the dual-Link Table of the lead node, insert the new node with the value of x into * p, and set p =null
DListNode * s = malloc (sizeof (DListNode); // ①
S-> data = x; // ②
S-> prior = p-> prior; // ③
S-> next = p; // ④
P-> prior-> next = s; // ⑤
P-> prior = s; // ⑥
}
② Delete node * p on a double-linked table
Void DDeleteNode (DListNode * p)
{// In the dual-Link Table of the lead node, delete the node * p and Set * p to a non-terminal node.
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 insert and delete operations on the double-chain table must modify the pointers in both directions.
The time complexity of the preceding two algorithms is O (1 ).
Reference: baike.baidu.com/view/1850617.htm

Why are two-chain tables instead of single-chain tables used in the data structure, double-chain tables, HASH lists, and link methods?

When deleting a node in a single-chain table, you must obtain the precursor of the node to be deleted. There are two ways to obtain the precursor, the first method is to locate the node to be deleted and store the current node along the way. The second method is to locate the node to be deleted and start from the header of the single-chain table again to locate the precursor. Although method 1 is usually used. However, the efficiency of the two methods is the same, and the total number of pointer moving operations is 2 x I. If a two-way linked list is used, no precursor node needs to be located. Therefore, the total number of pointer movement operations is I.

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.