"C" single-chain table/two-way linked list creation/traversal/insertion/deletion, single-chain table Traversal

Source: Internet
Author: User

"C" single-chain table/two-way linked list creation/traversal/insertion/deletion, single-chain table Traversal

Recently, the C language curriculum design near the end of the period is more than one level more difficult than the usual exercise assignments. The first time I came into contact with the C language framework development, I learned about the View (interface layer) and Service (business logic layer), Persistence (Persistence layer) Separation and coupling, a process-oriented MVC feeling.

All these are based on the creation, deletion, output, writing, and reading of linked lists ......

This article aims to consolidate the basic knowledge of the linked list (from chapter 10 of "C Language Programming Tutorial -- people's post and telecommunications Publishing House"). We will only discuss the concept of the linked list and add, delete, modify, and query it. Thank you for your advice.

 

I. linked list structure and static/Dynamic Linked List

Ii. Create and traverse a single-chain table

Iii. insert and delete a single-chain table

Iv. Concept of two-way linked list

5. Establish and traverse a two-way linked list

6. Searching elements in a two-way linked list

VII. Concept of circular linked list

8. merge two linked list instances

IX. Video Information Management System

Expand your thinking, and finally go to the end to see (• Fair fair Fair • Fair) fair Fair

 

I. linked list structure and static/Dynamic Linked List

A linked list is a common data structure-different from an array:

1. first, the array size must be declared during definition. If the number of elements added to the array exceeds the length of the array, all content cannot be properly saved; the linked list can be expanded based on the size.

2. The array is a collection of elements of the same data type, which are stored in a continuous order in the memory. Commonly Used functions such as malloc in the linked list dynamically and randomly allocate space and connect them with pointers.

The linked list structure is as follows:

In a linked list, each element contains two parts: the data part and the pointer part. The data part is used to store the data contained in the element, and the pointer part is used to point to the next element. The pointer to the last element points to NULL, indicating that the address to be pointed to is NULL. Struct is defined as a whole, and the pointer part is defined as the pointer type pointing to the struct type.

Static linked lists need to be implemented by arrays, that is, linear table elements are stored in arrays. The array unit stores the linked list node. The link field of the node points to the position of the next element, that is, the subscript of the array unit where the next element is located. These elements may be physically stored consecutively or discontinuous, and they are connected through logical relationships-This involves the definition of the array length, the implementation cannot predict the size of the defined array, and the dynamic linked list will appear immediately.

A Dynamic Linked List is a linked list established from scratch during program execution, that is, the data of nodes and input nodes are opened one by one, and the relationship between nodes is established.

 

Ii. Create and traverse a single-chain table

In a single-link table, each node has only one pointer, and all nodes are single-line connections. Except for the null node pointer at the end, each node's Pointer Points to the next node, forming a linear chain.

Next, create and traverse the source code to output a single-chain table.

# Include <stdio. h> # include <stdlib. h> # include <malloc. h>/* One-way linked list */struct Student/* Create a Student Information Structure Model */{char cName [20];/* Student name */int iNumber; /* student ID */struct student * next;/* pointer type pointing to the struct type */}; int iCount; /* The global variable indicates the length of the linked list */struct Student * Create ();/* Create a linked list function declaration */void print (struct Student *); /* declare the function of traversing the output linked list */int main () {int insert_n = 2;/* define and initialize the node number to be inserted */int delete_n = 2; /* define and initialize the node number to be deleted */struct Student * pHead;/* declare a knot pointing to the Student information The pointer of the body as pHead transmits */pHead = Create () for the header node;/* creates a linked list and returns the header pointer of the linked list to pHead */print (pHead ); /* pass the pointer pHead into the output function to traverse the output */return 0;} struct Student * Create () {struct Student * pHead = NULL;/* initialize the linked list, the header pointer is null */struct Student * pEnd, * pNew; iCount = 0;/* initialize the length of the linked list */pEnd = pNew = (struct Student *) malloc (sizeof (struct Student);/* dynamically opens up a space of the Student information struct type, so that pEnd and pNew both point to the struct space */scanf ("% s ", pNew-> cName);/* obtain the name of the first student from the input stream */scanf ("% d", & pNew-> iNumber );/ * Obtain the first student ID from the input stream */while (pNew-> iNumber! = 0)/* set the cycle end condition -- when the student ID is not 0 */{iCount ++;/* the length of the linked list + 1, that is, the number of student information + 1 */if (iCount = 1)/* if the length of the linked list is just added to 1, execute */{pNew-> next = pHead; /* point the pointer to null */pEnd = pNew;/* trace the newly added node */pHead = pNew; /* the header node points to the first node */} else/* If the linked list has been established and the length is greater than or equal to 2, run */{pNew-> next = NULL; /* the pointer to the new node is null */pEnd-> next = pNew;/* The original node points to the new node */pEnd = pNew; /* pEnd points to the new node */} pNew = (struct Student *) malloc (sizeof (struct Student )); /* re-allocate the node's memory space */scanf ("% s", pNew-> cName);/* Get the first student name from the input stream */scan F ("% d", & pNew-> iNumber);/* obtain the first student ID from the input stream */} free (pNew ); /* release Node space */return pHead;/* return the created header pointer */} void print (struct Student * pHead) {struct Student * pTemp; /* defines a temporary pointer pointing to a student information struct type */int iIndex = 1;/* defines and generates an error. The variable iIndex is used to identify the first student (information) */printf ("% d students in total (information): \ n", iCount); pTemp = pHead;/* pointer to get the address of the first node */while (pTemp! = NULL)/* when the temporary pointer does not point to NULL */{printf ("% d student information: \ n", iIndex); printf ("Name: % s ", pTemp-> cName);/* output name */printf (" student ID: % d ", pTemp-> iNumber ); /* output student ID */pTemp = pTemp-> next;/* move the temporary pointer to the next node */iIndex ++;/* perform auto-increment operations */}}

 

Iii. insert and delete a single-chain table

In this instance, the student ID is inserted after the student ID.

Delete and release the space based on the location of the linked list.

The main function is added as follows:

Int main () {int insert_n = 2;/* defines and initializes the node number to be inserted */int delete_n = 2; /* define and initialize the node number to be deleted */struct Student * pHead; /* declare a pointer to the student information struct as pHead to pass the header node */pHead = Create ();/* Create a linked list, return the head pointer of the linked list to pHead */pHead = Insert (pHead, insert_n);/* pass the pointer pHead and the node to be inserted to the Insert function */print (pHead ); /* pass the pointer pHead into the output function and traverse the output */Delete (pHead, delete_n);/* pass the pointer pHead and the node to be deleted to the Delete function */print (pHead ); /* pass the pointer pHead into the output function to traverse the output */return 0 ;}

Insert function:

Struct Student * Insert (struct Student * pHead, int number) {struct Student * p = pHead, * pNew; /* define pNew to point to the new space */while (p & p-> iNumber! = Number) p = p-> next;/* Make the temporary node trace to the location to be inserted (the instance must have information with the student ID number. After inserting the information, otherwise an error occurs) */printf ("Name and Student ID: \ n");/* allocate the memory space and return the address of the memory space */pNew = (struct Student *) malloc (sizeof (struct Student); scanf ("% s", pNew-> cName); scanf ("% d", & pNew-> iNumber ); pNew-> next = p-> next;/* The new node Pointer Points to the original node */p-> next = pNew;/* the header Pointer Points to the new node */iCount ++; /* increase the number of linked list nodes */return pHead;/* return header pointer */}

Delete function:

Void Delete (struct Student * pHead, int number) {int I; struct Student * pTemp;/* Temporary pointer */struct Student * pPre; /* indicates the node before the node to be deleted */pTemp = pHead;/* obtain the head node of the linked list */pPre = pTemp; for (I = 0; I <number; I ++) {/* point Temp to the node to be deleted through the for loop */pPre = pTemp; pTemp = pTemp-> next ;} pPre-> next = pTemp-> next;/* connect to and delete nodes on both sides of the node */free (pTemp);/* release the memory space of the node to be deleted */iCount --; /* reduce the number of nodes in the linked list */}

 

Iv. Concept of two-way linked list

A two-way linked list is based on a single-chain table. A single-chain table is unidirectional and has a head node and a tail node. to access any node, you must know the head node and cannot reverse it. The dual-link table adds a pointer field that points to the front and back nodes of the node respectively through two pointer fields. In this way, you can access the front and back nodes of a double-stranded table.

In a two-way linked list, in addition to data outside the node, there are two chain domains, one storage direct successor Node Address, generally called the right chain domain; one storage direct precursor node address, it is generally called the left Chain Domain.

Two-way linked list structure:

 

5. Establish and traverse a two-way linked list

The source code of a two-way linked list is similar to that of a single-chain table, but the control of the second pointer field is added. The source code without comments is directly pasted here.

 
# Include <stdio. h>
# Include <stdlib. h> # include <malloc. h> # define N 10 typedef struct Node {char name [20]; struct Node * llink, * rlink;} STUD; STUD * creat (int ); void print (STUD *); int main () {int number; char studname [20]; STUD * head, * searchpoint; number = N; head = creat (number ); print (head); printf ("enter the name of the person you want to search for:"); scanf ("% s", studname); searchpoint = search (head, studname ); printf ("the name of the person you want to search for is: % s", * & searchpoint-> name); return 0;} STUD * creat (int n) {STUD * p, * h, * s; int I; if (h = (STUD *) malloc (sizeof (STUD) = NULL) {printf ("memory cannot be allocated"); exit (0);} h-> name [0] = '\ 0'; h-> llink = NULL; h-> rlink = NULL; p = h; for (I = 0; I <n; I ++) {if (s = (STUD *) malloc (sizeof (STUD) = NULL) {printf ("memory cannot be allocated"); exit (0);} p-> rlink = s; printf ("enter the name of the % d individual", I + 1); scanf ("% s", s-> name); s-> llink = p; s-> rlink = NULL; p = s;} h-> llink = s; p-> rlink = h; return (h );}

Void print (STUD * h) {int n; STUD * p; p = h-> rlink; printf ("data information: \ n"); while (p! = H) {printf ("% s", & * (p-> name); p = p-> rlink;} printf ("\ n ");}

 

6. Searching elements in a two-way linked list

Search function STUD * search (STUD *, char *);

STUD * search (STUD * h, char * x) {STUD * p; char * y; p = h-> rlink; while (p! = H) {y = p-> name; if (strcmp (y, x) = 0) return (p); else p = p-> rlink ;} printf ("no data found! ");}

 

VII. Concept of circular linked list

Similar to a single-chain table, the circular linked list is also a chained storage structure, which is produced by a single-chain performer.

The pointer to the last node of a Single-linked list points to NULL, while the pointer to the last node of the circular linked list points to the head node of the linked list.

In this way, the head and end are connected to form a circular data link.

The establishment of a circular linked list does not require a special header node.

To determine whether the cyclic linked list is the end point, you only need to determine whether the pointer field of the node points to the head node of the linked list.

 

8. merge two linked list instances

Create a student linked list of two leading nodes. Each node contains a student ID, name, and score. The linked list is sorted in ascending order by student ID. The linked list is still sorted in ascending order by student ID.

Algorithm analysis:

The merged linked list is implemented using the merge () function. The function defines three working pointers a, B, and c. a and B point to the current node of the La linked list and Lb linked list respectively, and C point to the End Node of the merged linked list. The header nodes of the merged linked list share the header nodes of the La linked list.

① Before merging, let a and B point to the first node of the two linked lists respectively, and c point to the header node of the La linked list.

② The merging should be divided into three cases, that is, La and Lb are not processed; La is not processed, but Lb is processed; Lb is not processed, but La is processed.

③ During the merge process, a small link in the La AND Lb linked lists should always be in Lc to maintain order.

Void merge (struct stud * La, struct stud * Lb) {struct stud * a, * B, * c; c = La; a = La-> next; /* Before merging */B = Lb-> next; while (! = NULL & B! = NULL)/* La and Lb are not processed completely */{if (a-> num <= B-> num) {c-> next = a; c =; a = a-> next;} else {c-> next = B; c = B; B = B-> next ;}} if (! = NULL) c-> next = a;/* If La is not processed */else c-> next = B; /* If Lb is not processed */free (Lb);/* release the Lb header node */}

 

IX. Video Information Management System

 

 

 

Expanded thinking, (• Artificial Intelligence) Artificial Intelligence

Since a two-way linked list can have an extra pointer field to indicate a forward node, llink and rlink are used to distinguish the two pointer fields;

Is there a possibility that there are several more pointer fields to put ulink and dlink (pointing to the above node and pointing to the following node )?

In this way, a two-dimensional linked list instead of a one-dimensional linked list is formed.

Of course, these linked lists are still linear in physical memory.

The theory can be implemented. What about the three-dimensional linked list?

The picture is too beautiful, and it will be extended when I study the data structure in my sophomore year next semester.

 

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.