Study Notes on data structure and algorithm analysis (3)-linked list ADT and Data Structure linked list

Source: Internet
Author: User

Study Notes on data structure and algorithm analysis (3)-linked list ADT and Data Structure linked list

Today, I simply learned the linked list. Later, I will attach some simple and classic questions for further study.

First, you must understand the linked list. The linked list is actually composed of nodes, and each node contains a data domain and a pointer domain. The data domain is used to store data, the pointer field is used to store the address of the next node.

1. Define the node first.

Typedef struct Node * PtrToNode;

Typedef PtrToNode List;

Typedef PtrToNode Position;


Struct Node

{

ElementType Element;

Position next;

};


2. Below are some common linked list operations.

List init (List L );

Int IsEmpty (List L );

Int IsLast (Position P, List L );

Position Find (ElementType X, List L );

Void Delete (ElementType X, List L );

Position FindPrevious (ElementType X, List L );

Void Insert (ElementType X, List L, Position P );

Void DeleteList (List L );

Position Header (List L );

Position First (List L );

Void Print (List L );





3. Specific analysis


List init (List L)

{

L = new struct Node;

L-> next = nullptr;

Return L;

}


This is the initialization of the linked list. By default, the linked list has an empty header pointer, which does not need to store data, but is used to handle some special situations, I personally think that the replacement of a node in exchange for concise code is a good choice,



Int IsEmpty (List L)

{

Return L-> next = nullptr;

}

This is to determine whether the linked list is empty.





Position Find (ElementType X, List L)

{

Position p;

P = L-> next;

While (p! = Nullptr & p-> Element! = X)

{

P = p-> next;

}

Return p;

}

Because the linked list is different from the pointer, no subscript can be accessed directly, so we need to traverse them one by one.



Int IsLast (Position P, List L)

{

Position p;

P = L-> next;

If (p-> next! = Nullptr)

{

P = p-> next;

}

Return p = P;

}


Determine whether it is the last one.





Void Delete (ElementType X, List L)

{

Position p, tempCell;

P = FindPrevious (X, L );

If (! IsLast (p, L ))

{

TempCell = p-> next;

P-> next = tempCell-> next;

Delete tempCell;

}

}

Do not forget to release the memory if you delete it.






Position FindPrevious (ElementType X, List L)

{

Position p;

P = L;

While (p-> next! = Nullptr & p-> next-> Element! = X)

{

P = p-> next;

}

Return p;

}

Search-related



// Insert (after legal Position P)

Void Insert (ElementType X, List L, Position P)

{

Position tempCell;

TempCell = new struct Node;

If (tempCell = nullptr)

{

Cout <("Out of space !! ") <Endl;

}

TempCell-> Element = X;

TempCell-> next = P-> next;

P-> next = tempCell;

}


Inserted after the node by default





Void DeleteList (List L)

{

Position p;

P = L-> next;

L-> next = nullptr;

While (p! = Nullptr)

{

Position pTemp = p-> next;

Delete p;

P = pTemp;

}

}

Clear linked list






Void Print (List L)

{

Position p;

P = L-> next;

While (p! = Nullptr)

{

Cout <p-> Element. coe <p-> Element. index <"";

P = p-> next

}

Cout <endl;

}


Print linked list


What books should I read if I want to learn algorithms and data structures?

I don't know much about your foundation, but Xu zhuoqun, the author of "data structure and algorithm", is a good book. It is a textbook book for our sophomore year.
In addition, if your C language is good, data structure and algorithm analysis is also quite good,
Introduction: Data Structure and Algorithm Analysis is a simplified Chinese Translation of Data Structures and Algorithm Analysis in C. The original book was named one of the top 30 computer books in the 20th century by Mark
Allen Weiss has made great achievements in data structure and algorithm analysis. His data structure and algorithm analysis books are particularly popular and well received. He has been used as a teaching material by more than 500 universities around the world.
In this book, the author has refined and strengthened his innovative processing methods for algorithms and data structures. Through the implementation of C program, the concept of abstract data type is elaborated, and the efficiency, performance and running time of the algorithm are analyzed.

The following websites may be helpful for your learning ~~
Reference: www.yuanma.org/


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.