Data structure and algorithm analysis--1, linked list

Source: Internet
Author: User

Linked lists are one of the simplest data structures, and when we use arrays to store data, frequent insertions and deletions can consume a lot of performance, and a linked list is a linear data structure that is suitable for frequently inserting and deleting operations.

A detailed introduction to the linked list can be seen here, in layman's terms, the list is made up of a number of nodes, each node has a pointer, this pointer holds the position of the next node. As a result, the list is linked by pointers to those nodes that are physically not logically related, forming a linear table of relationships.

Let's take a look at "single-linked list", in C, the data type with encapsulation capability is struct. Therefore, we use a struct to represent a node. Defined as follows:

1 struct node{2     ElementType Element; 3      struct Node *4 };

Here, we assume that the element is of type int, so it is defined as: 

1 int ElementType;

  For a linked list, its basic operation is to determine whether the linked list is empty, whether the current element is the last, find an element, insert an element, delete an element, delete an entire table, and so on. So we have the following statement:

 1  int   IsEmpty (List L);  2   Islast (Position P);  3  position Find (ElementType X, List L);  4   Delete (ElementType X, List L);  5   Insert (ElementType X, Position P);  6  void  deletelist (List L); 

Yes, I forgot the type definition. Using a type definition can make your code look more concise and easier to understand. The Position and list types are pointers to node, but we use List to represent the head node and Position to represent any node. This way, the program is at a glance.

1 struct Node * Ptrtonode; 2 typedef ptrtonode List; 3 typedef ptrtonode Position;

The next step is to implement, and we'll start by judging if the list is empty. We pass in the head pointer, just to determine if the Next pointer of the head node is NULL. If Next is null, it indicates that the list has only the head node and is empty.

1 int 2 IsEmpty (List L) {3     return L->next = = NULL; 4 }

This is similar to determining whether the current element is the last element, because when a node is at the end, its Next pointer is NULL.

1 int 2 islast (Position P) {3     return P->next = = NULL; 4 }

then I put out the implementation of the next few functions, it is very easy to understand.

1 Position2 Find (ElementType X, List L) {3 Position P;4P = l->Next;5      while(P! = NULL && p->element! =X) {6p = PNext; 7     }8     returnP;9 }Ten  One void  A Insert (ElementType X, Position P) { - Position temp; -temp =malloc(sizeof(structNode)); the      -Temp->element =X; -Temp->next = p->Next; -P->next =temp; + } -  + void  A deletelist (List L) { at Position P, temp; -P = l->Next; -L-next =NULL; -      while(P! =NULL) { -temp = p->Next; -          Free(P); inP =temp; -     } to } +  - void the Delete (ElementType X, List L) { * Position P, temp; $P = FindPrevious (x,l);//find the previous element of XPanax Notoginseng     if(!islast (p,l)) { -temp = p->Next; theP-next = temp->Next; +          Free(temp); A     } the}

we found that there is a findprevious function to find the previous element of the current element, and this function appears as we didn't expect before. However, in the delete operation we find that we need such a function, so we also declare such a function, not to consider its specific implementation (this is the abstraction). In fact its implementation is very simple, but we will think, since you can use the pointer to save the next node, then why not a pointer to save the previous node? So the doubly linked list was born.

1 struct node{2    ElementType Element; 3     struct Node *4     struct node *5 };

  Now the delete operation can do this:

1 // p is the pointer to the node you want to delete. 2 p->next->prev = p->Prev; 3  Free (P);

  When the list is connected to the tail, the linked list becomes a circular list. In C # There is a LinkedList class to implement a two-way loop linked list, the document and source code can be seen here and here. It's really strong. Microsoft, the documentation is available.

Write here, it should be finished. Still did not reach the state of write, I think it should be related to the object of sharing. What kind of readers does this article want to read? In fact, it is for me to see, so it is, this series of articles will be purely for my summary of the record, there is no reader object. Pits have been dug for themselves, there are many deeper knowledge points, I will pits.

The next one should be "stack", and I'll explain an example of a stack.

Finish

Data structure and algorithm analysis--1, linked list

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.