Dual-Table Implementation, dual-Table Implementation

Source: Internet
Author: User

Dual-Table Implementation, dual-Table Implementation

The single-table linked list is a bit similar. The main difference lies in creating a table, inserting elements, and deleting elements.

The data structure of the double-stranded table is as follows:

Typedef struct DNode {ElemType data; // node data struct DNode * prior; // point to the previous node pointer struct DNode * next; // point to the next node pointer} DLinkList;
Implement the following functions:

Void CreateListF (DLinkList * & L, ElemType a [], int n); // create the table void CreateListR (DLinkList * & L, ElemType a [], int n) by using the header insertion method ); // create the table void InitList (DLinkList * & L) by means of the end plug; // initialize the void DestroyList (DLinkList * & L) linked list; // destroy the int ListEmpty (DLinkList * L) linked list ); // judge whether the linked list is empty int ListLength (DLinkList * L); // calculate the length of the linked list void DispList (DLinkList * L); // output linked list int GetElem (DLinkList * L, int I, ElemType & e); // calculate the value of node I in the linked list and assign it to e int LocateElem (DLinkList * L, ElemType e ); // calculate the serial number of the first element equal to the e value int InsertElem (DLinkList * & L, int I, ElemType e); // Insert the int DeleteElem (DLinkList * & L, int I, ElemType & e); // deletes an element.
Specific implementation code:

# Include <stdio. h> # include <stdlib. h> # include <iostream> # define ElemType float # define GET_ARRAY_LEN (array) (sizeof (array)/sizeof (array [0]) using namespace std; typedef struct DNode {ElemType data; // node data struct DNode * prior; // point to the previous node pointer struct DNode * next; // point to the next node pointer} DLinkList; void CreateListF (DLinkList * & L, ElemType a [], int n); // create the table void CreateListR (DLinkList * & L, ElemType a [], int n) by using the header insertion method ); // create the table void Init using the end plug method List (DLinkList * & L); // initialize the void DestroyList (DLinkList * & L); // destroy the int ListEmpty (DLinkList * L ); // judge whether the linked list is empty int ListLength (DLinkList * L); // calculate the length of the linked list void DispList (DLinkList * L); // output linked list int GetElem (DLinkList * L, int I, ElemType & e); // calculate the value of node I in the linked list and assign it to e int LocateElem (DLinkList * L, ElemType e ); // calculate the serial number of the first element equal to the e value int InsertElem (DLinkList * & L, int I, ElemType e); // Insert the int DeleteElem (DLinkList * & L, int I, ElemType & E); // Delete the element void CreateListF (DLinkList * & L, ElemType a [], int n) {L = (DLinkList *) malloc (sizeof (DLinkList )); l-> prior = L-> next = NULL; DLinkList * p = L; for (int I = 0; I <n; I ++) {DLinkList * addNumber = (DLinkList *) malloc (sizeof (DLinkList); addNumber-> data = a [I]; if (p-> next! = NULL) {addNumber-> prior = p-> next-> prior; addNumber-> next = p-> next; p-> next-> prior = addNumber; p-> next = addNumber;} else {addNumber-> prior = p; addNumber-> next = NULL; p-> next = addNumber ;}}} void CreateListR (DLinkList * & L, ElemType a [], int n) {L = (DLinkList *) malloc (sizeof (DLinkList )); l-> prior = L-> next = NULL; DLinkList * p = L; for (int I = 0; I <n; I ++) {DLinkList * addNumber = (DLinkList *) malloc (sizeof (DLinkList); addNumber-> dat A = a [I]; addNumber-> prior = p; addNumber-> next = NULL; p-> next = addNumber; p = p-> next ;}} void InitList (DLinkList * & L) {L = (DLinkList *) malloc (sizeof (DLinkList); L-> prior = L-> next = NULL ;} void DestroyList (DLinkList * & L) {DLinkList * p = L; DLinkList * q = L-> next; while (q! = NULL) {free (p); p = q; q = q-> next;} free (p);} int ListEmpty (DLinkList * L) {return (L-> next = NULL);} int ListLength (DLinkList * L) {int count = 0; DLinkList * p = L-> next; while (p! = NULL) {count ++; p = p-> next;} return count;} void DispList (DLinkList * L) {DLinkList * p = L-> next; while (p! = NULL) {cout <p-> data <"; p = p-> next;} cout <endl;} int GetElem (DLinkList * L, int I, elemType & e) {DLinkList * p = L; int j = 0; while (j <I & p! = NULL) {p = p-> next; j ++;} if (I <1 | p = NULL) return-1; else {e = p-> data; return 1 ;}} int LocateElem (DLinkList * L, ElemType e) {DLinkList * p = L-> next; int I = 1; while ((! (-1e-9 <p-> data-e & p-> data-e <1e-9) & p! = NULL) {p = p-> next; I ++;} if (p = NULL) return-1; else return I;} int InsertElem (DLinkList * & L, int I, ElemType e) {DLinkList * p = L-> next; int j = 1; while (j <I & p! = NULL) {j ++; p = p-> next;} if (p = NULL & I> j + 1) | I <1) return-1; else {DLinkList * addNumber = (DLinkList *) malloc (sizeof (DLinkList); addNumber-> data = e; addNumber-> prior = p-> prior; addNumber-> next = p; p-> prior-> next = addNumber; p-> prior = addNumber; return 1 ;}} int DeleteElem (DLinkList * & L, int I, elemType & e) {DLinkList * p = L-> next; int j = 1; while (j <I & p! = NULL) {j ++; p = p-> next;} if (I <1 | p = NULL) return-1; else {e = p-> data; if (p-> next! = NULL) {p-> next-> prior = p-> prior; p-> prior-> next = p-> next; free (p );} else {p-> prior-> next = NULL; free (p);} return 1 ;}} int main () {DLinkList * L = NULL; float a [] = {3.4, 8.7, 9.7, 5.6, 4, 3}; ElemType e; CreateListF (L, a, GET_ARRAY_LEN ()); dispList (L); DestroyList (L); CreateListR (L, a, GET_ARRAY_LEN (a); DispList (L); GetElem (L, 4, e ); cout <e <endl; cout <LocateElem (L, e) <endl; DeleteElem (L, 3, e); DispList (L); InsertElem (L, 1, 78.4); DispList (L); cout <ListLength (L); return 0 ;}

The running result is as follows:


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.