Data Structure Learning 1 --- linked list, Data Structure Learning 1 ---

Source: Internet
Author: User

Data Structure Learning 1 --- linked list, Data Structure Learning 1 ---

Lead node single-chain table

Data structure definition

ListNode.h#ifndef LISTNODE_H#define LISTNODE_Htemplate<class T> class ListNode{private:T data;ListNode<T> *next;public:ListNode();ListNode(T value);int Getdata();ListNode<T>* Getnext();void Setnext(ListNode<T>* p);};template<class T>ListNode<T>::ListNode():next(NULL){}template<class T> ListNode<T>::ListNode(T value):data(value),next(NULL){}template<class T> int ListNode<T>::Getdata(){return data;}template<class T> ListNode<T>* ListNode<T>::Getnext(){return next;}template<class T> void ListNode<T>::Setnext(ListNode<T>* p){next=p;}#endif
Implementation of all operations on the single-link table with the leading Node

LinkList. h # include <iostream> # include "ListNode. h "using namespace std; template <class T> class LinkList {private: ListNode <T> * head; ListNode <T> * tail; public: LinkList (); bool IsEmpty (); int ListLength (); // It does not include the header node void AddHead (T value); void AddTail (T value); // It starts from the second position, the 0th elements are node T GetAtIndex (int index) except the overhead node; bool InsertAt (int index, T value); bool RemoveAt (int index, T & value ); bool RemoveAtPtr (ListNode <T> * del); Lis TNode <T> * GetHead (); ListNode <T> * GetTail (); ListNode <T> * GetNodePtr (int index); void DestroyList (); void TraverseList () ;}; template <class T> LinkList <T >:: LinkList () {head = new ListNode <T>; tail = head ;} template <class T> bool LinkList <T>: IsEmpty () {if (head-> Getnext () = NULL) return 1; return 0 ;} template <class T> int LinkList <T>: ListLength () {int len = 0; ListNode <T> * p = head-> Getnext (); while (p) {len ++; p = p-> Getnext () ;}Return len ;}template <class T> void LinkList <T >:: AddHead (T value) {ListNode <T> * p = new ListNode <T> (value); p-> Setnext (head-> Getnext (); head-> Setnext (p );} template <class T> void LinkList <T>: AddTail (T value) {ListNode <T> * p = new ListNode <T> (value ); tail-> Setnext (p); tail = tail-> Getnext ();} template <class T> T LinkList <T >:: GetAtIndex (int index) {if (index> ListLength () | index <0) {cout <"Incorrect location! \ N "; return-1; // error} int I = 0; ListNode <T> * p = head-> Getnext (); while (I <index) {p = p-> Getnext (); // P points to the index node I ++;} return p-> Getdata ();} template <class T> bool LinkList <T>: InsertAt (int index, T value) {if (index> ListLength () | index <0) {cout <"the Insertion Location is incorrect! \ N "; return 0; // error} int I =-1; ListNode <T> * p = head; listNode <T> * add = new ListNode <T> (value); while (I <index-1) {p = p-> Getnext (); // when jumping out, P points to the previous node I ++ of the index;} add-> Setnext (p-> Getnext (); p-> Setnext (add ); return 1 ;}template <class T> bool LinkList <T >:: RemoveAt (int index, T & value) {if (index> ListLength () | index <0) {cout <"The deletion location is incorrect! \ N "; return 0; // error} int I =-1; ListNode <T> * p = head; while (I <index-1) {p = p-> Getnext (); // when it jumps out, P points to the previous node of index I ++ ;} listNode <T> * del = p-> Getnext (); p-> Setnext (del-> Getnext (); value = del-> Getdata (); delete del; return 1;} template <class T> bool LinkList <T>: RemoveAtPtr (ListNode <T> * del) {ListNode <T> * p = GetHead (); while (p-> Getnext ()! = Del) p = p-> Getnext (); // upon bounce, p-> next = del; ListNode <T> * ptrdel = p-> Getnext (); p-> Setnext (ptrdel-> Getnext (); delete ptrdel; return 1;} template <class T> ListNode <T> * LinkList <T>: GetHead () {return head;} template <class T> ListNode <T> * LinkList <T >:: GetTail () {return tail ;} template <class T> ListNode <T> * LinkList <T>: GetNodePtr (int index) {ListNode <T> * p = head-> Getnext (); int I = 0; while (I <index) {p = p-> Setnext (p); I ++;} return p ;} template <class T> void LinkList <T>: DestroyList () {while (head-> Getnext () {ListNode <T> * del = head-> Getnext (); head-> Setnext (del-> Getnext (); delete del ;}} template <class T> void LinkList <T >:: TraverseList () {if (IsEmpty ()) cout <"Empty linked list"; ListNode <T> * p = head-> Getnext (); while (p) {cout <p-> Getdata () <endl; p = p-> Getnext () ;}} test: # include "LinkList. h "# include" ListNode. h "# include <iostream> using namespace std; void main () {LinkList <int> myList1; LinkList <int> myList2; myList1.AddTail (2); myList1.AddTail (4 ); myList1.AddTail (6); myList1.AddTail (8); myList1.AddTail (10); myList1.RemoveAtPtr (myList1.GetHead ()-> Getnext (); cout <"myList1 element: \ n "; myList1.TraverseList (); myList2.AddTail (3); myList2.AddTail (3); myList2.AddTail (5); myList2.AddTail (7); myList2.AddTail (12); cout <"myList2 element: \ n "; myList2.TraverseList (); cout <"merged linked list element: \ n"; int pos = 0; ListNode <int> * p = myList2.GetHead ()-> Getnext (); while (p & pos <myList1.ListLength () {if (p-> Getdata () <myList1.GetAtIndex (pos) {myList1.InsertAt (pos, p-> Getdata ()); // ListNode <int> * del = p; p = p-> Getnext (); // myList2.RemoveAtPtr (del); // Delete the node int s corresponding to the pointer; myList2.RemoveAt (0, s); // Delete the first node, that is, delete 0th elements} pos ++;} if (p) {myList1.GetTail ()-> Setnext (p );} myList1.TraverseList (); getchar ();}
Single-chain table with no leading nodes
Single-chain table with no leading node /********************************** **************************************// * Create a node-less linked list *//******************************* **************************************** */# include <iostream. h> typedef struct node {int data; struct node * next;} Node, * LinkList; LinkList CreateListHead (); // create the linked list LinkList CreateListRear () by inserting the header (); // create the linked list void TraverseList (LinkList L) by inserting it at the end; // The traversal chain table bool IsEmpty (LinkList L); // judge whether it is null int ListL Ength (LinkList L); // The length of the linked list bool GetElem (LinkList L, int I, int & e); // obtain the I-th element to ebool InsertElem (LinkList & L, int I, int e); // Insert the ebool DeleteIndexElem (LinkList & L, int I) element at position I ); // insert element ebool DeletePointElem (LinkList & L, LinkList del) at position I; // insert element ebool DestroyList (LinkList & L) at position I ); // destroy the linked list/* Header Insertion Method */LinkList CreateListHead () {LinkList head = NULL; for (int I = 1; I <42; I ++) {LinkList p = new Node; p-> data = I; p-> next = head; head = p;} r Eturn head;}/* tail Insertion Method */LinkList CreateListRear () {LinkList head = NULL; LinkList tail = head; for (int I = 1; I <42; I ++) {LinkList p = new Node; p-> data = I; p-> next = NULL; if (I = 1) {head = p; tail = head ;} else {tail-> next = p; tail = p;} return head;} void TraverseList (LinkList L) {LinkList p = L; cout <"The linked list element is: "<endl; while (p) {cout <p-> data <"; p = p-> next ;}cout <endl ;} bool IsEmpty (LinkList L) {if (L = NULL) return 1; return 0;} int ListLengt H (LinkList L) {int I = 0; LinkList p = L; while (p) {I ++; p = p-> next;} return I ;} bool GetElem (LinkList L, int I, int & e) {int j = 1; LinkList p = L; if (I <1 | I> ListLength (L )) return false; while (j <I) {p = p-> next; j ++;} e = p-> data; return 1;} bool InsertElem (LinkList & L, int I, int e) {LinkList p = L; int j = 1; LinkList temp = new Node; if (I <1 | I> ListLength (L) return 0; else if (1 = I) {temp-> data = e; temp-> next = L; L = temp;} else {while (j <i-1) {P = p-> next; j ++;} temp-> data = e; temp-> next = p-> next; p-> next = temp ;} return 1;} bool DeleteIndexElem (LinkList & L, int I) {LinkList p = L; int j = 1; if (I <1 | I> ListLength (L )) return 0; else if (1 = I) {L = L-> next; delete (p);} else {while (j <i-1) {p = p-> next; j ++;} LinkList temp = p-> next; p-> next = p-> next; delete (temp );} return 1;} bool DeletePointElem (LinkList & L, LinkList del) {LinkList p = L; if (del = L) {L = L-> next; delete p;} els E {while (p-> next! = Del) p = p-> next; p-> next = del-> next; delete del;} return 1;} bool DestroyList (LinkList & L) {LinkList p = L; while (L) {p = L; L = L-> next; delete (p) ;}return 1 ;}// test void main () {// int e; LinkList List = CreateListRear (); TraverseList (List); // GetElem (List, 5, e); // cout <e <endl; // DeleteIndexElem (List, 1); // DeletePointElem (List, List-> next); // TraverseList (List );}

Cyclic linked list of the lead Node

/*************************************** * ******************************* // Create a linked list *//********************************** **************************************/# include <iostream. h> typedef struct node {int data; struct node * next;} Node, * LinkList; LinkList CreateListHead (); // create the linked list LinkList CreateListRear () by inserting the header (); // create the linked list void TraverseList (LinkList L) by inserting it at the end; // The traversal chain table bool IsEmpty (LinkList L); // judge whether it is null int ListLength (LinkList L); // The length of the linked list bool GetElem (LinkList L, int I, int & e); // obtain the I-th element to ebool InsertElem (LinkList L, int I, int e); // Insert the ebool DeleteIndexElem (LinkList L, int I) in position I; // Delete the bool DeletePointElem (LinkList L, linkList del); // Delete the void main () {// int e; LinkList List = CreateListRear (); TraverseList (List ); // cout <IsEmpty (List); // cout <ListLength (List); // GetElem (List, 44, e); // cout <e; // InsertElem (List, 41,1 00); // TraverseList (List); // DeleteIndexElem (List, 1); // TraverseList (List); // DeleteIndexElem (List, 40 ); // TraverseList (List); // DeletePointElem (List, List-> next); // TraverseList (List);}/* Header Insertion Method */LinkList CreateListHead () {LinkList head = new Node; head-> next = head; for (int I = 1; I <42; I ++) {LinkList p = new Node; p-> data = I; p-> next = head-> next; head-> next = p;} return head;}/* tail Insertion Method */LinkList CreateListRear () {LinkList head = new N Ode; head-> next = head; LinkList tail = head; for (int I = 1; I <42; I ++) {LinkList p = new Node; p-> data = I; p-> next = head; tail-> next = p; tail = p;} return head;} void TraverseList (LinkList L) {LinkList p = L-> next; cout <"linked list element:" <endl; while (p! = L) {cout <p-> data <"; p = p-> next;} cout <endl;} bool IsEmpty (LinkList L) {if (L-> next = L) return 1; return 0;} int ListLength (LinkList L) {int I = 0; LinkList p = L-> next; while (p! = L) {I ++; p = p-> next;} return I;} bool GetElem (LinkList L, int I, int & e) {int j = 0; linkList p = L; if (I <1 | I> ListLength (L) return 0; while (j <I) {p = p-> next; j ++;} e = p-> data; return 1;} bool InsertElem (LinkList L, int I, int e) {LinkList p = L; int j = 0; linkList temp = new Node; if (I <1 | I> ListLength (L) return 0; while (j <i-1) {p = p-> next; j ++;} temp-> data = e; temp-> next = p-> next; p-> next = temp; return 1;} bool deletdexdexelem (LinkList L, int I) {LinkList p = L; int j = 0; if (I <1 | I> ListLength (L) return 0; while (j <i-1) {p = p-> next; j ++;} LinkList temp = p-> next; p-> next = p-> next; delete (temp ); return 1;} bool DeletePointElem (LinkList L, LinkList del) {LinkList p = L; while (p-> next! = Del) p = p-> next; p-> next = del-> next; delete del; return 1 ;}
Circular linked list with no leading Node

/*************************************** * *********************************** Node does not take the lead cyclic linked list *//*********************************** * ***********************************/# include <iostream. h> typedef struct node {int data; struct node * next;} Node, * LinkList; LinkList CreateListHead (); // create the linked list LinkList CreateListRear () by inserting the header (); // create the linked table void TraverseList (LinkList L) by inserting it at the end; // The traversal chain table // bool IsEmpty (LinkList L); // you can check whether the table is empty. // int ListLeng Th (LinkList L); // chain table length // bool GetElem (LinkList L, int I, int & e ); // obtain element I to e // bool InsertElem (LinkList L, int I, int e); // insert element ebool deletdexdexelem (LinkList L, int I); // Delete the bool DeletePointElem (LinkList & L, LinkList del) element at position I; // Delete the void Joseph (LinkList L) of the element corresponding to the given pointer ); // test void main () {// int e; LinkList List = CreateListRear (); TraverseList (List); // DeletePointElem (List, List); Joseph (List );} /* Header Insertion Method * // LinkL Ist CreateListHead () // {// LinkList head = NULL; // for (int I = 41; I> 0; I --) // {// LinkList p = new Node; // p-> data = I; // p-> next = head; // head = p; ///} // return head; //}/* tail Insertion Method */LinkList CreateListRear () {LinkList head = NULL; LinkList tail = head; for (int I = 1; I <42; I ++) {LinkList p = new Node; p-> data = I; p-> next = head; if (I = 1) {head = p; tail = head;} else {tail-> next = p; tail = p;} return head;} void TraverseList (LinkList L) {LinkL Ist p = L; cout <"linked list element:" <endl; while (p-> next! = L) {cout <p-> data <"; p = p-> next;} cout <p-> data; cout <endl ;} // bool IsEmpty (LinkList L) // {// if (L-> next = L) // return 1; // return 0; //} // int ListLength (LinkList L) // {// int I = 0; // LinkList p = L-> next; // while (p! = L) // {// I ++; // p = p-> next; //} // return I; // bool GetElem (LinkList L, int I, int & e) // {// int j = 0; // LinkList p = L; // if (I <1 | I> ListLength (L )) // return 0; // while (j <I) // {// p = p-> next; // j ++; ///} // e = p-> data; // return 1; //} // bool InsertElem (LinkList L, int I, int e) // {// LinkList p = L; // int j = 0; // LinkList temp = new Node; // if (I <1 | I> ListLength (L) // return 0; // while (j <i-1) // {// p = p-> next; // J ++; // temp-> data = e; // temp-> next = p-> next; // p-> next = temp; // return 1; // bool deletdexdexelem (LinkList L, int I) // {// LinkList p = L; // int j = 0; // if (I <1 | I> ListLength (L) // return 0; // while (j <i-1) // {// p = p-> next; // j ++; //} // LinkList temp = p-> next; // p-> next = p-> next; // delete (temp); // return 1; //} bool DeletePointElem (LinkList & L, LinkList del) {LinkList p = L; if (del = L) {while (p-> Next! = Del) p = p-> next; L = L-> next; p-> next = L; delete del;} else {while (p-> next! = Del) p = p-> next; p-> next = del-> next; delete del;} return 1 ;} // solution to the Joseph problem void Joseph PHUs (LinkList L) {LinkList p = L; while (L) {cout <p-> next-> data <endl; DeletePointElem (L, p-> next ); p = p-> next; if (p-> next = p) break ;}}

Two-way linked list

# Include <stdio. h> # include <malloc. h> typedef struct node {int data; struct node * prior; struct node * next;} DNode, * DLinkList; DLinkList CreateDList (); void TraverseList (DLinkList L ); bool IsEmpty (DLinkList L); int Length (DLinkList L); DLinkList GetElem (DLinkList L, int I); bool InsertElem (DLinkList L, int I, int e ); bool DeleteElem (DLinkList L, int I, int & e); void main () {DLinkList Dlist = CreateDList (); printf ("list Meta Suru: \ n "); TraverseList (Dlist); InsertElem (Dlist, 3,3); printf (" list: \ n "after the element is inserted); TraverseList (Dlist );} DLinkList CreateDList () {// tail Plugging Method DLinkList head = (DLinkList) malloc (sizeof (DNode); DLinkList tail; head-> next = head-> prior = NULL; // create an empty table tail = head; for (int I = 0; I <4; I ++) {DLinkList p = (DLinkList) malloc (sizeof (DNode); scanf ("% d", & p-> data); p-> next = NULL; p-> prior = tail; tail-> next = p; tail = p;} return head;} bool IsEmpty (D LinkList L) {if (L-> next = NULL) return 1; return 0;} void TraverseList (DLinkList L) {if (! IsEmpty (L) {DLinkList p = L-> next; while (p! = NULL) {printf ("% d \ n", p-> data); p = p-> next ;}} int Length (DLinkList L) {int len = 0; if (! IsEmpty (L) {DLinkList p = L-> next; while (p! = NULL) {len ++; p = p-> next ;}} return len ;}dlinklist GetElem (DLinkList L, int I) {DLinkList p = L-> next; int j = 1; if (I <1 | I> Length (L) {return 0;} while (j <I) {p = p-> next; j ++;} return p;} bool InsertElem (DLinkList L, int I, int e) {DLinkList p; if (! (P = GetElem (L, I-1) return 0; DLinkList s = (DLinkList) malloc (sizeof (DNode); s-> data = e; s-> next = p-> next; p-> next-> prior = s; p-> next = s; s-> prior = p; return 1 ;}

Classic interview questions: fast and slow pointers help you quickly find the intermediate elements of the linked list (the leading node single-chain table)

# Include <stdio. h> # include <malloc. h> typedef struct node {int data; struct node * next;} Node, * LinkList; LinkList CreateDList (); void TraverseList (LinkList L); bool IsEmpty (LinkList L ); int Length (LinkList L); LinkList GetElem (LinkList L, int I); bool InsertElem (LinkList L, int I, int e); // bool DeleteElem (LinkList L, int I, int & e); int MidElem (LinkList L); // returns the middle element void main () {LinkList list = CreateDList (); printf ("lis T element: \ n "); TraverseList (list); // InsertElem (list, 3, 3); // printf (" list: \ n "after the inserted element "); // TraverseList (list); printf ("the middle element is: % d \ n", MidElem (list);} LinkList CreateDList () {// tail plugging LinkList head = (LinkList) malloc (sizeof (Node); LinkList tail; head-> next = NULL; // create an empty table tail = head; for (int I = 0; I <3; I ++) {LinkList p = (LinkList) malloc (sizeof (Node); scanf ("% d", & p-> data); p-> next = NULL; tail-> next = p; tail = p;} return head;} bool I SEmpty (LinkList L) {if (L-> next = NULL) return 1; return 0;} void TraverseList (LinkList L) {if (! IsEmpty (L) {LinkList p = L-> next; while (p! = NULL) {printf ("% d \ n", p-> data); p = p-> next ;}} int Length (LinkList L) {int len = 0; if (! IsEmpty (L) {LinkList p = L-> next; while (p! = NULL) {len ++; p = p-> next ;}} return len ;}linklist GetElem (LinkList L, int I) {LinkList p = L-> next; int j = 1; if (I <1 | I> Length (L) {return 0;} while (j <I) {p = p-> next; j ++;} return p;} bool InsertElem (LinkList L, int I, int e) {LinkList p; if (! (P = GetElem (L, I-1) return 0; LinkList s = (LinkList) malloc (sizeof (Node); s-> data = e; s-> next = p-> next; p-> next = s; return 1;} // The speed pointer allows you to quickly find the intermediate element int MidElem (LinkList L) of the linked list) {LinkList p; LinkList mid; p = mid = L; // P must point to the header node to ensure that the linked list is not empty. p-> next is the correct statement. if (p-> next = NULL) return 0; // empty linked list while (1) {if (p = NULL | p-> next = NULL) // If p is NULL and it is an odd number, p-> next = If NULL is an even number, return mid-> data; else {p = p-> next; mid = mid-> next ;}}}





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.