Two-way linked list of data structure (non-circular two-way linked list)

Source: Internet
Author: User

The Code is a non-cyclic two-way linked list, including node insertion and deletion operations [cpp] // LinkList_D.cpp: Defines the entry point for the console application. // # include "stdafx. h "# include" stdio. h "# include" stdlib. h "# include" iostream "using namespace std; typedef int DataType; typedef struct DLNode {DataType data; struct DLNode * prior; struct DLNode * next;} DLNode, * DLinkList; void CreateDList (DLinkList & L, int n); void PrintDLinkList (DLinkLis T & L); int DListInset (DLinkList & L, int I, DataType e); int DListDelete (DLinkList & L, int I, DataType & e ); int _ tmain (int argc, _ TCHAR * argv []) {// create a linked list DLinkList L; CreateDList (L, 5 ); // create a two-way linked list PrintDLinkList (L) with five nodes; // output linked list // test the inserted element DListInset (L, 1,100 ); // Insert the element PrintDLinkList (L); DListInset (L, 2,200); PrintDLinkList (L) before the first element ); // test the function of deleting an element printf ("node deleted \ n"); int e; DListDelete (L, 2, e); PrintDL InkList (L); // test the function of deleting an element printf ("node deleted \ n"); DListDelete (L, 6, e); PrintDLinkList (L); getchar (); getchar (); return 0;} // create a chain table with a length of n, insert a new node before the header node, non-circular chain table void CreateDList (DLinkList & L, int n) {L = (DLinkList) malloc (sizeof (DLNode); L-> next = NULL; L-> prior = NULL; printf ("please input n nodes: \ n "); DLinkList p = (DLinkList) malloc (sizeof (DLNode); // Insert the first node scanf (" % d ", & p-> data ); l-> next = p; p-> prior = L; p-> Next = NULL; // insert other remaining nodes for (int I = n-1; I> 0; -- I) {DLinkList p = (DLinkList) malloc (sizeof (DLNode); scanf ("% d", & p-> data); p-> prior = L; L-> next-> prior = p; p-> next = L-> next; L-> next = p ;}// output node void PrintDLinkList (DLinkList & L) {DLinkList p = L-> next; // point to the first node while (p! = NULL) {printf ("% 5d", p-> data); p = p-> next;} printf ("\ n ");} // Insert the int DListInset (DLinkList & L, int I, DataType e) {DLinkList p = L-> next; // point to the first element int j = 1; while (p & j <I) {p = p-> next; j ++;} if (! P | j> I) {return-1;} DLinkList s = (DLinkList) malloc (sizeof (DLNode); if (! S) return-1; s-> data = e; s-> prior = p-> prior; p-> prior-> next = s; s-> next = p; p-> prior = s; return 1;} // Delete the int DListDelete (DLinkList & L, int I, DataType & e) element at the specified position) {DLinkList p = L-> next; // point to the first element int j = 1; while (p & j <I) {p = p-> next; j ++;} // point to the I-th element if (! P | j> I) return-1; if (p-> next = NULL) // The deleted element is the last element {p-> prior-> next = p-> next ;} else {// The deleted element is not the last element p-> prior-> next = p-> next; p-> next-> prior = p-> prior ;}} // LinkList_D.cpp: Defines the entry point for the console application. // # include "stdafx. h "# include" stdio. h "# include" stdlib. h "# include" iostream "using namespace std; typedef int DataType; typedef struct DLNode {DataType data; struct DLNode * Prior; struct DLNode * next;} DLNode, * DLinkList; void CreateDList (DLinkList & L, int n); void PrintDLinkList (DLinkList & L); int DListInset (DLinkList & L, int I, DataType e); int DListDelete (DLinkList & L, int I, DataType & e); int _ tmain (int argc, _ TCHAR * argv []) {// create a linked list DLinkList L; CreateDList (L, 5); // create a two-way linked list PrintDLinkList (L) with five nodes ); // output linked list // test the inserted element DListInset (L, 1,100); // Insert the element PrintDLinkList (L) before the first element; DListIns Et (L, 2,200); PrintDLinkList (L); // test the function of deleting an element printf ("node deleted \ n"); int e; DListDelete (L, 2, e ); printDLinkList (L); // test the function of deleting an element printf ("node deleted \ n"); DListDelete (L, 6, e); PrintDLinkList (L); getchar (); getchar (); return 0;} // create a chain table with a length of n, insert a new node before the header node, non-circular chain table void CreateDList (DLinkList & L, int n) {L = (DLinkList) malloc (sizeof (DLNode); L-> next = NULL; L-> prior = NULL; printf ("please input n nodes: \ n "); DLinkList p = (DLinkLis T) malloc (sizeof (DLNode); // Insert the first node scanf ("% d", & p-> data); L-> next = p; p-> prior = L; p-> next = NULL; // insert other remaining nodes for (int I = n-1; I> 0; -- I) {DLinkList p = (DLinkList) malloc (sizeof (DLNode); scanf ("% d", & p-> data); p-> prior = L; l-> next-> prior = p; p-> next = L-> next; L-> next = p ;}// output node void PrintDLinkList (DLinkList & L) {DLinkList p = L-> next; // point to the first node while (p! = NULL) {printf ("% 5d", p-> data); p = p-> next;} printf ("\ n ");} // Insert the int DListInset (DLinkList & L, int I, DataType e) {DLinkList p = L-> next; // point to the first element int j = 1; while (p & j <I) {p = p-> next; j ++;} if (! P | j> I) {return-1;} DLinkList s = (DLinkList) malloc (sizeof (DLNode); if (! S) return-1; s-> data = e; s-> prior = p-> prior; p-> prior-> next = s; s-> next = p; p-> prior = s; return 1;} // Delete the int DListDelete (DLinkList & L, int I, DataType & e) element at the specified position) {DLinkList p = L-> next; // point to the first element int j = 1; while (p & j <I) {p = p-> next; j ++;} // point to the I-th element if (! P | j> I) return-1; if (p-> next = NULL) // The deleted element is the last element {p-> prior-> next = p-> next ;} else {// The deleted element is not the last element p-> prior-> next = p-> next; p-> next-> prior = p-> prior ;}}

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.