Create different representation methods and operations for a single Linear Linked List, and create a linear representation

Source: Internet
Author: User

Create different representation methods and operations for a single Linear Linked List, and create a linear representation

Create a single Linear Linked List. Common Operations include the head insertion method and the tail insertion method to create a Linear Linked List. Common Operations include creating a linked list, searching, deleting, adding elements, and finding inverse links.

First, create a linked list using the header insertion method:

// The header pointer uniquely identifies a single-chain table

# Define MaxSize 15 typedef int elem_type; typedef struct linklist {elem_type data; struct linklist * next;} Linklist; // create a linked list using the header insertion method: each time you assign the pointer to the header node to the new node, the new node is assigned to the header node pointer Linklist * CreateList () {Linklist * head, * p; head = (Linklist *) malloc (sizeof (Linklist); head-> next = NULL; // p = (Linklist *) malloc (sizeof (Linklist); int I; cout <"generates a random number:" <endl; for (I = 0; I <MaxSize; I ++) {int data; data = rand () % 100; p = (Linklist *) malloc (sizeof (Linklist); cout <"no." <I <"no." <data <endl; p-> data = data; p-> next = head-> next; head-> next = p;} return head ;}
// What are the characteristics of the linked list created by using the header insertion method?

Here we can write a lookup function to describe it:

Define a search function:

// Search (retrieve the value of the I number) elem_type Get_Elem (Linklist * L, int I) {int j; Linklist * p; p = L-> next; if (I> MaxSize) & (I <0) cout <"Enter valid I value"; for (j = 0; j <I-1; j ++) {p = p-> next; // flag-> next ++; // The linked list is not a matrix. The pointer auto-increment 1 does not point to the next node. That is, it can be like this in the matrix. } Return p-> data ;}
Compile: The result is as follows:

// The main function is as follows:

Int main () {Linklist * Q; Q = CreateList (); int I; cout <"Enter the element I ="; cin> I; int getNum = Get_Elem (Q, I); cout <"no." <I <"element:" <getNum <endl; // Insert_Elem (Q, 3, 5); while (1); return 0 ;}

According to the running results, we can know that for the header insertion method, the input node order is the opposite to the generated linked list order.

You can also delete the element and add the element to the function body:

// Delete a node in the linked list void Delete_Linklist (Linklist * L, elem_type I) {Linklist * p, * q; p = L-> next; int j = 0; if (I <0 & I> MaxSize) cout <"j input valid I value"; else {while (p-> next! = NULL) & (j <i-1) {p = p-> next; // find the I node + + j;} q = p-> next; p-> next = q-> next; free (q) ;}// Add a node (add a value at node I) void Insert_Elem (Linklist * L, int I, elem_type Number) {int j; Linklist * p, * q; p = L-> next; if (I> MaxSize) & (I <0 )) cout <"Enter valid I value"; for (j = 0; j <I-1; j ++) {p = p-> next; // flag-> next ++; // The linked list is not a matrix, and the pointer auto-increment 1 does not point to the next node, which is in the matrix. } If (I = j + 1) {q = (Linklist *) malloc (sizeof (Linklist); q-> data = Number; q-> next = p-> next; p-> next = q;} // return p ;}

Problem Analysis: Why is the data element sequence in the header Insertion Method opposite to the node sequence in the linked list?

First cycle:

P-> data = data; // the first time the data element is assigned a value of p-> next = head-> next; // The value is assigned cyclically. For the first time, NULL is assigned to p-> next, head-> next = p; // p after the first assignment is assigned to head-> next;

The second cycle:

P-> data = data; // The second value of the data element is p-> next = head-> next; // The value is assigned cyclically, the second value (<span style = "font-family: Arial, Helvetica, sans-serif; "> head-> next </span> <span style =" font-family: Arial, Helvetica, sans-serif; ">) assign to p-> next, </span> head-> next = p; // p after the second assignment is assigned to head-> next;

So the newly assigned node is closer to head-> next.






Create a one-way Dynamic Linked List and insert, delete, and input it, including the following tasks:

# Include <stdio. h>
# Define LEN sizeof (struct number)
Struct number/* definition number and number */
Int name;
Int num;
Struct number * next;
};

Struct number * create ()/* create a linked list function */
{
Struct number * head, * new, * tail, * p;
Int count = 0;
While (1)
{
New = (struct number *) malloc (LEN );
Printf ("input Name and Number \ n ");
Scanf ("% d", & new-> name, new-> num);/* user input number and number */
If (new-> name = 0)
{
Free (new );
Break;
}
Else if (count = 0)
{
Head = new;
Tail = new;
}
Else
{
Tail-> next = new;
Tail = new;
}
Count ++;
}
Tail-> next = NULL;
Return (head );
}

Struct number * delist (struct number * head, int name)/* function for deleting numbers */
{
Struct number * p0, * p1;
P1 = head;
If (head = NULL)
{
Printf ("\ nempty list! \ N ");
}
Else
If (p1-> name = name)/* Find the same number */
Head = p1-> next;
Else
{
While (p1-> name! = Name & p1-> next! = NULL)/* troubleshoot one by one */
{
P0 = p1;
P1 = p1-> next;
}
If (p1-> name = name)
{
P0-> next = p1-> next;
Printf ("The node is deleted \ n ");
}
Else
Printf ("The node can not been foud! \ N ");
}
Return head;
}

Struct number * insert (struct number * head, struct number * new)
{... Remaining full text>

Single-chain table operations

1. Single-chain table Type Definition
Typedef struct Node * PNode;/* Node pointer type */
Struct Node/* single-chain table Node Structure */
{
DataType data;/* value range */
Struct Node * next;/* pointer field */
};
To improve readability, you can define the following single-chain table types:
Typedef struct Node * LinkList;
LinkList list;/* define a single-chain table list */
PNode p;/* define a single-chain table node pointer
The two fields of the node referred to by pointer p are:
Value Field: p-> data
Pointer field: p-> next
Note: the purpose of setting the header node is to unify the operations of the empty linked list and non-empty linked list to simplify the implementation of the linked list operation. The empty linked list of the leading node is: list-> next = NULL; the empty linked list of the not leading node can only be expressed as list = NULL;
Common Single-chain table algorithms (Leading nodes)
Algorithm 1 creates an empty single-chain table LinkList createNullist_link (void)
Analysis: Apply for a storage space of a header node and leave the pointer field of this node empty.
LinkList CreateNullist_link (void)
{
LinkList list = (LinkList) malloc (sizeof (struct Node ));
// Storage space of the header node of the Application Form
If (list! = NULL) list-> next = NULL;
Else printf ("Out of space! \ N "); // creation failed
Return (list );
}
Note: In the algorithm, malloc is the memory application function and must be supported by the header file stdlib. h.
Algorithm 2 insert int insert_link (LinkList list, PNode p, DataType x) to a single-chain table)
Insert node algorithm:
First, generate the node q to be inserted, set its value to x, and then insert the node q to the node p by correcting the pointer.
Insert implementation:
Q-> data = x; // generate the node to be inserted
Q-> next = p-> next;
P-> next = q;
Algorithm 3 Delete int delete_link (LinkList list, DataType x) of a single-chain table Node)
Delete node algorithm: First, find the first node q with the x value in the list single-chain table with the header node, record the position p of its precursor node, and then delete the node q through pointer correction.
Deletion implementation:
Q = p-> next;
P-> next = q-> next;
Free (q );

... Remaining full text>


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.