Data Structure-Linked List Implementation

Source: Internet
Author: User

It mainly includes creating, inserting, deleting, locating, and merging linked lists in the linked list operation. [Cpp] // LinkList. cpp: Defines the entry point for the console application. // # include "stdafx. h "# include" iostream "# include" stdlib. h "# include" stdio. h "using namespace std; typedef int DataType; // defines the node typedef struct LNode {DataType data; struct LNode * next;} LNode, * LinkList; void CreateList (LinkList & L, int n); void PrintLinkList (LinkList & L); int GetElem (LinkList L, int I, DataType & E); int ListInsert (LinkList & L, int I, DataType e); int ListDelete (LinkList & L, int I, DataType & e); void MergeList (LinkList & La, linkList & Lb, LinkList & Lc); int _ tmain (int argc, _ TCHAR * argv []) {// create a linked list with a specified length, int LinkListLength = 6; LinkList L; createList (L, LinkListLength); // output linked list PrintLinkList (L); // search for 3rd elements int tobefound = 0; GetElem (L, 3, tobefound ); printf ("\ ntobefound = % d \ n", tobefound); // locate Insert a new node ListInsert (L, 3, 10); // output chain table printf ("new node is inserted: \ n"); PrintLinkList (L ); // delete an int e; ListDelete (L, 4, e); // output printf ("\ none node is deleted: \ n "); printLinkList (L); // create a new linked list printf ("\ ninput 3 new nodes of new list \ n"); LinkList L2; CreateList (L2, 3 ); printLinkList (L2); // combine two linked lists: LinkList L3; MergeList (L, L2, L3); printf ("\ nMerged lists: \ n"); PrintLinkList (L3 ); // getch Ar (); getchar (); return 0;} // create the void CreateList (LinkList & L, int n) of the chain table whose length is n {// L = (LinkList) malloc (sizeof (LNode); L-> next = NULL; printf ("please input n link nodes \ n"); for (int I = n; I> 0; -- I) {LNode * p = (LinkList) malloc (sizeof (LNode); scanf ("% d", & p-> data ); // Insert a new node p-> next = L-> next; L-> next = p;} in the head of the linked list // output chain table node void PrintLinkList (LinkList & L) {printf ("all list nodes is: \ n"); LinkList p = L-> ne Xt; // point to the first node while (p! = NULL) {printf ("% 5d", p-> data); p = p-> next ;}// find the linked list element, find the I-th element int GetElem (LinkList L, int I, DataType & e) {// LinkList 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; // No e = p-> data found; return 1; // indicates finding} // Insert the Element e int ListInsert (LinkList & L, int I, DataType e) before node I {LinkList p = L; int j = 0; while (p & j <i-1) {p = p-> next; + + j;} // until p points to the I-1 if (! P | j> i-1) return-1; LinkList s = (LinkList) malloc (sizeof (LNode); s-> data = e; s-> next = p-> next; p-> next = s;} // Delete the node at the specified position and delete the node I, int ListDelete (LinkList & L, int I, DataType & e) {// LinkList p = L; // point to the header node int j = 0; while (p-> next & j <i-1) {p = p-> next; j ++;} if (! (P-> next) | j> i-1) return-1; LinkList q = p-> next; // point to the deleted node p-> next = q-> next; e = q-> data; free (q );} // merge two linked lists void MergeList (LinkList & La, LinkList & Lb, LinkList & Lc) {LinkList pa = La-> next; // point to the first element LinkList pb = Lb-> next; // point to the first element LinkList pc = La; // use the header node of the La linked list as the header node of the merged linked list Lc = pc; while (pa & pb) {if (pa-> data <= pb-> data) {pc-> next = pa; pc = pa; pa = pa-> next ;} www.2cto.com else {pc-> next = pb; pc = pb; pb = pb -> Next ;}} pc-> next = pa? Pa: pb; free (Lb );}

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.