Data Structure-linked list, data structure --

Source: Internet
Author: User

Data Structure-linked list, data structure --

There are many articles on linked lists on the Internet, and there are no more articles than the predecessors I wrote. After one year of work, I always thought I had forgotten what I learned before. So I came up with the idea of writing a blog and wrote down my work and learning process. Think about it or use the linked list as my virgin blog. After all, this is the first data structure I wrote on the programmer's way. The following content is written in an uneasy and shy mood. If you have any questions, please correct them!

-- Unrelated to the linked list is purely emotional

Before talking about linked lists, let's talk about linear tables first. A linear table is the most basic and commonly used data structure. A linear table is a set of multiple data elements. Except the first and last data elements, other data elements are connected at the beginning and end. There are two storage methods for linear tables: sequential Storage Structure and chained storage structure.

The commonly used array is a typical sequential storage structure. The linked list is the chain storage structure described below.

The sequential storage structure is that two adjacent elements are also adjacent to each other in the memory. The advantage of this storage mode is that the query is convenient, and an element can be accessed through the first address and offset, there are also many matching search algorithms, and the shortest can reach O (logn ). The disadvantage is that insertion and deletion are inconvenient, and the worst complexity can reach O (n). For example, if you want to insert/delete elements at a certain position, you need to move all elements after this position one by one. In addition, it is inconvenient to determine the number of elements. You must create an array that is large enough before use to place all elements, however, the opened array space is often unable to be fully utilized, resulting in a waste of resources.

The chained storage structure is that two adjacent elements are not necessarily adjacent to each other in the memory. The advantage of this storage mode is that you only need to operate the pointer to add and delete elements, which is more convenient, the time complexity is O (1). Another advantage is memory saving. The elements need to be added to open up the memory, and are released when not needed, you do not need to estimate the number of elements in advance, which is much more flexible than the sequential storage structure. The disadvantage is that the search algorithm is relatively small. Generally, it can only be searched through traversal. the time complexity is O (n). Another disadvantage is that it takes time to apply for or release the memory, if the memory is frequently applied for release, the time consumed is considerable.

The elements in a linked list are called knots. They are generally composed of two parts: pointer fields and value fields. Value fields can be basic data types, struct data types, and other complex data types to store the required specific data; the pointer field is a pointer to the next node. Different linked lists in the pointer field can be divided into one-way linked list, two-way linked list, and cyclic linked list.

As shown in, it is a one-way linked list composed of four nodes. Each Data and Next is a node. The first node is called the header node, and the last node is called the end node, head is a pointer to the header node. Data is the value field of the node used to store Data. Next is the pointer field of the node and points to the Next node. The pointer field of the End Node is null.

Linked List operations mainly focus on Pointer fields and memory application release. General operations include adding, deleting, modifying, and querying. The header node can be different from other node data types. The Value Field of the header node can store information about linked lists, such as the length of the linked list, Creation Time, And creator.

The following is a simple C program.

The entire program is composed of three files: Chain -- chain. h stores declarations of some types and functions.

| -- Implementation of the chain. c storage function

| -- Main. c call and test implemented Functions

|____ Makefile MakeFile, Which is used during compilation. If it is the first time you contact, ignore it. It will be updated in later blogs.

The chain. h file is as follows:

# Ifndef _ CHAIN_H _ # define _ CHAIN_H _/* declare some data types */typedef int datatype; // declare the Data Type typedef unsigned short uint16; typedef unsigned char bool; /* returned results */typedef enum {TRUE, FALSE} bool_val;/* declared linked list node */typedef struct node {datatype data; struct node * next;} ListNode; /* declare the linked list header */typedef struct head {char info [20]; unsigned short length; ListNode * next;} ListHead; /* declaration of some linked list operation functions */ListHead * CreateList (); // create a linked list bool ViewList (ListHead * head ); // The traversal chain table bool AddNodeByLoc (ListHead * head, uint16 loc, datatype data); // Add the node bool DelNodeByLoc (ListHead * head, uint16 loc) at the specified position ); // Delete the node bool ModNodeByLoc (ListHead * head, uint16 loc, datatype data) at the specified location; // modify the node data datatype FindDataByLoc (ListHead * head, uint16 loc) at the specified location ); // return the node data bool DestoryList (ListHead * head) at the specified position; // destroy the linked list # endif
Chain. h

The chain. c file is as follows:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include "chain. h "/* Create a linked list */ListHead * CreateList () {ListHead * head = NULL; head = (ListHead *) malloc (sizeof (ListHead )); // apply for memory memset (head, 0, sizeof (head);/* initialize linked list information */head-> length = 0; strcpy (head-> info, "CangLing's List"); head-> next = NULL; return head;}/* prepaid table */bool ViewList (ListHead * head) {/* validity determination */if (head = NULL) {return FALSE ;} ListNode * p = NULL;/* print linked List information */printf ("The List Info is % s \ n", head-> info ); printf ("The List Length is % d \ n", head-> length);/* output node content */p = head-> next; while (p! = NULL) {printf ("% d \ n", p-> data); p = p-> next;} return TRUE;}/* Add a node based on the location, position greater than the length of the linked list is added at the end of the linked list */bool AddNodeByLoc (ListHead * head, uint16 loc, datatype data) {/* validity judgment */if (head = NULL) | (loc = 0) {return FALSE;} bool_val ret = FALSE; ListNode * node = head-> next; ListNode * tmp = NULL; /* initialize the node to be created */ListNode * p = (ListNode *) malloc (sizeof (ListNode); p-> data = data; p-> next = NULL; if (head -> Length = 0) // process the linked list with only the first node {head-> next = p; p-> next = NULL ;} else if (loc <= head-> length) // process 1 <loc <length. {/* Add it after the header node */if (loc = 1) {head-> next = p; p-> next = node;} else {/* Find the previous node in the corresponding position */while (loc> 2) {loc --; node = node-> next;} tmp = node-> next; // Save the node Address of the loc location node-> next = p; // place the node to be added in the loc position p-> next = tmp;} else // process the case of loc> length {while (node-> next! = NULL) {node = node-> next;} node-> next = p;} head-> length ++; // modify the chain table information ret = TRUE; return ret ;} /* Delete the node at the loc location */bool DelNodeByLoc (ListHead * head, uint16 loc) {/* determine the legality */if (head = NULL) | (loc = 0) | (loc> head-> length) {return FALSE;} bool_val ret = FALSE; ListNode * tmp = head-> next; listNode * freenode = NULL; if (loc = 1) // process the first node {freenode = tmp; head-> next = tmp-> next ;} Else // process 1 <loc <length {while (loc> 2) // locate the previous node of loc {loc --; tmp = tmp-> next;} freenode = tmp-> next; // Save the Node Address tmp-> next = freenode-> next ;} /* release the node and modify the linked list information */free (freenode); head-> length --; return ret ;} /* modify the node information of the loc location */bool ModNodeByLoc (ListHead * head, uint16 loc, datatype data) {/* legality judgment */if (head = NULL) | (loc = 0) | (loc> head-> length) {return FALSE;} bool_val re T = FALSE; ListNode * tmp = head-> next; while (loc> 1) // find the loc node {tmp = tmp-> next; loc --;} tmp-> data = data; // modify the node data return ret;}/* return the data of the loc node */datatype FindDataByLoc (ListHead * head, uint16 loc) {/* validity determination */if (head = NULL) | (loc = 0) | (loc> head-> length) {return FALSE ;} datatype ret = 0; ListNode * tmp = head-> next; while (loc> 1) // find the loc node {tmp = tmp-> next; loc --;} ret = tm P-> data; return ret;} bool DestoryList (ListHead * head) {ListNode * p = NULL; ListNode * node = NULL; if (head = NULL) {return TRUE;}/* release a node except the header node */p = head-> next; while (p! = NULL) {node = p; p = p-> next; free (node) ;}/ * release the header node */free (head); return TRUE ;}
Chain. c

The main. c file is as follows:

# Include <stdio. h> # include "chain. h "int main () {int I = 0; ListHead * head = NULL; head = CreateList (); // create a chain table printf ("Now we will Add four Nodes \ n"); for (I = 1; I <5; I ++) {AddNodeByLoc (head, I, i); // Add a linked list Node} ViewList (head); // the prepaid table printf ("Now we will Delete the third Node \ n"); DelNodeByLoc (head, 3 ); // Delete the third Node of the linked list ViewList (head); printf ("Now we will modify the third Node to 5 \ n"); ModNodeByLoc (head, 3, 5 ); // modify the information of the third Node to 5 ViewList (head); printf ("Now we will view the second Node \ n"); printf ("% d \ n ", findDataByLoc (head, 2); // view the data DestoryList (head) of the second node; // destroy the linked list}
Main. c

MakeFile

Main: chain. o main. o # generate the main dependent file # execute the command cc chain. o main. o-o main generates the final Executable File main cc chain. o main. o-o mainmain. o: main. c chain. h # generate main. o dependent file chain. o: chain. c chain. h # generate a chain. o dependent files # Delete the generated intermediate file clean: rm *. o main
MakeFile

The above four files are used in the Linux environment. Put the above files in the same folder, enter make to run, and then generate the chain. o main. o and the executable file main, run make clean to clear the three compiled files.

Here I will briefly talk about Makefile. Compiling in Windows is done by IDE. For example, you do not need to care about the dependency between files when compiling a project in VC6.0. However, in Linux, this part of work is done by MakeFile. MakeFile is related to the compilation rules of the entire project. There are countless files in a project, which are stored in different directories by module, type, and function. MakeFile specifies a series of rules to specify which files are compiled first, which files need post-compilation, which need to be re-compiled, and even some more complex functional operations. It brings the benefit of "automatic compilation". Once the MakeFile file is written, only one make command is required for project compilation, and the entire project will be fully automated. The above is a simple MakeFile file that I wrote for the linked list. The usage of MakeFile will be updated in subsequent blogs.

If you are not used to it, you can directly run the compilation command gcc main. c chain. c-o main. Of course, you can also directly copy the content of the three files and run them directly under VC6.0. The effect is the same.

  

The result of running the linked list is as follows:

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.