Common Operations on linked lists

Source: Internet
Author: User

The linked list is an important part of the data structure and is widely used in computer programs. It is also the focus of PEN questions of various companies.

The following are some simple operations for the linked list, including creating, adding, deleting, and unconfiguring a single-chain table, and merging ordered linked lists.

I. Create a linked list

There are three main forms of linked list, including single-chain table, double-chain table, and circular linked list.

Each node in a single linked list contains only one post-drive pointer. The dual-Link Table Node also contains a pre-drive pointer and a post-drive pointer. The post-drive of the End Node of the circular linked list points to the header node.

The Code is as follows:

/* Single-chain table node Structure */
Typedef structnodetype
{
Char ELEM;
Nodetype * next;
} Node;

/* Double-stranded table node Structure */
Typedef structdnodetype
{
Char ELEM;
Dnodetype * next;
Dnodetype * Prev;
} Dnode;

/*
Create a linked list
*/
Node * createlist (node * head)
{
If (null = head) // allocate the space of the header Node
Head = (node *) malloc (sizeof (node )),
Head-> next = NULL;

Node * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ N input ELEM :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Current = temp;/* the current node is the end node of the linked list */

}

Return head;
}

 

/* Create a double-stranded table */
Dnode * doublelist (dnode * head)
{
If (null = head) // allocate the space of the header Node
Head = (dnode *) malloc (sizeof (dnode), head-> Prev = NULL, head-> next = NULL;

Dnode * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ N input ELEM :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (dnode *) malloc (sizeof (dnode ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Temp-> Prev = current;/* forward of the new node to the current node */
Current = temp;/* the current node is the end node of the linked list */

}

Return head;
}

 

/* Create a circular linked list */
Node * cancelist (node * head)
{
If (null = head)/* allocate the space of the header node */
Head = (node *) malloc (sizeof (node), head-> next = NULL;

Node * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ N input ELEM :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Current = temp;/* the current node is the end node of the linked list */

}
Current-> next = head;/* The End Node points to the header node */
Return head;
}

 

2. Linked List Operation

Including adding, deleting, and output linked lists to a single-chain table

Add Node

/* Insert nodes at the end # Add can be directly inserted in the specified parameter node */
Node * insertnode (node * head, char ELEM)
{
If (null = head | null = ELEM)
Return head;

Node * Current = head-> next;/* current node */
Node * Prev = head;/* precursor node */
Node * temp;/* transition node */

While (current)/* move to the End Node */
{
Prev = current;
Current = Current-> next;
}

Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = ELEM;
Temp-> next = NULL;
Prev-> next = temp;/* the rear drive of the last node points to the new node */

Return head;
}

/* Delete a node */
Node * deletenode (node * head, char ELEM)
{
If (null = head | null = ELEM)
Return head;
If (null = head-> next)
Return head;

Node * Prev, * current;
Prev = head;
Current = head-> next;

While (current)
{
If (current-> ELEM = ELEM)/* match node element */
{
Prev-> next = Current-> next;/* the rear drive of the precursor node points to the next node of the current node */
Free (current);/* release the current node */
Return head;
}
Prev = current;
Current = Current-> next;/* move to next node */
}

Return head;

}
/* Output linked list */
Void printlist (node * head)
{
Node * Current = head-> next;
Cout <"\ n list are :";
While (null! = Current)
{
If (null! = Current-> ELEM)
Cout <SETW (5) <Current-> ELEM;
Current = Current-> next;
}

Cout <"\ n ";
}

Iii. Reverse placement of a single-chain table

Reverse placement of a single-chain table is common in the pen questions of companies. One of the following is an implementation.

Algorithm Description: insert each node in the linked list to the header node (# Add inserts the next node of the head into the next node of the head ).

The Code is as follows:

/* Reverse configuration of a single-chain table */
Node * reverselist (node * head)
{
If (null = head)
Return head;
If (null = head-> next)
Return head;
If (null = head-> next)
Return head;

Node * curr = head-> next;/* current node */
Head-> next = NULL;
Node * temp;
While (curr)
{
Temp = curr-> next;/* save the next node */
/* Insert the current node to the head node */
Curr-> next = head-> next;
Head-> next = curr;

Curr = temp;/* move to the next node */
}

Return head;
}

4. Finding the intermediate node of a single-chain table

It is common in pen questions. Generally, the topic description is: give a single-chain table and do not know the value of node N. How can we retrieve the intermediate node only once.

Algorithm Description: sets up two pointers: P1, P2, and P1. Each time one node is moved, P2 moves two nodes. When P2 moves to the last node, P1 points to the intermediate node.

The Code is as follows:

Intermediate node

/* Calculate the intermediate node */
Node * middlenode (node * head)
{
If (null = head)
Return head;
If (null = head-> next)
Return head-> next;

Node * P1, * P2;
P1 = head;
P2 = head;

While (P2-> next)
{
/* P2 nodes move 2 node locations */
P2 = P2-> next;
If (P2-> next)/* determines whether the P2 drive node exists, and moves it again */
P2 = P2-> next;
/* The P1 node moves one node position */
P1 = p1-> next;

}
Return P1;
}

5. Merge ordered Single-Chain tables

Problem description: merge two ordered Single-Chain tables, and the merged linked list is sorted in order.

Algorithm Description: For each node element in linked list A, find its insertion position in Linked List B, and insert this element into list B.

The Code is as follows:

/* Merge ordered Single-Chain tables */
Node * mergelist (node * H1, node * H2)
{
If (null = h1 | null = h2)
Return H1;
If (null = h1-> next)
Return H2;
If (null = h2-> next)
Return H1;

Node * curr1, * curr2, * prev1, * temp;
Prev1 = h1;/* The precursor node of linked list 1 */
Curr1 = h1-> next;/* current node of linked list 1 */
Curr2 = h2-> next;/* current node of linked list 2 */
Temp = h2;
While (curr2)
{
While (curr1 & curr1-> ELEM <curr2-> ELEM)/* The 1 pointer of the linked list moves to the position of the current element of the large or equal linked list 2 */
Prev1 = curr1, curr1 = curr1-> next;

/* Insert the current element of linked list 2 into linked list 1 */
Temp = curr2-> next;/* save the next node of linked list 2 */
Prev1-> next = curr2;
Curr2-> next = curr1;

/* Move linked list 1 to the new node */
Curr1 = curr2;
/* Move linked list 2 to the next node */
Curr2 = temp;
}

Return H1;

}

6. Determine whether the linked list has a ring

To determine whether a linked list has a ring is to determine whether the linked list is a circular linked list. The algorithm is relatively simple. You can traverse and judge whether the tail Pointer Points to the header pointer at a time.

The Code is as follows:

/* Determine whether the linked list has a ring (Cyclic linked list )*/
Bool iscyclelist (node * head)
{
If (null = head)
Return false;
If (null = head-> next)
Return false;
Node * Current = head-> next;
While (current)
{
If (Head = Current-> next)
Return true;
Current = Current-> next;
}
Return false;
}

 

VII. Summary and source files

Some common operations of the linked list are implemented. The code for the source file linklist. cpp is as follows:

/*
* Author: Da Wendong
* Modification date:
* Description: Common Operations for linked lists.
*
*/
# Include <iostream>
# Include <iomanip>
Using namespace STD;

/* Single-chain table node Structure */
Typedefstruct nodetype
{
Char ELEM;
Nodetype * next;
} Node;

/* Double-stranded table node Structure */
Typedefstruct dnodetype
{
Char ELEM;
Dnodetype * next;
Dnodetype * Prev;
} Dnode;

/* ===================================================== ======================================================= */
/*
Create a linked list
*/
Node * createlist (node * head)
{
If (null = head) // allocate the space of the header Node
Head = (node *) malloc (sizeof (node )),
Head-> next = NULL;

Node * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ N input ELEM :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Current = temp;/* the current node is the end node of the linked list */

}

Return head;
}
/* ===================================================== ======================================================= */
/*
Output linked list
*/
Void printlist (node * head)
{
Node * Current = head-> next;
Cout <"\ n list are :";
While (null! = Current)
{
If (null! = Current-> ELEM)
Cout <SETW (5) <Current-> ELEM;
Current = Current-> next;
}

Cout <"\ n ";
}

/* ===================================================== ======================================================= */

/* Insert a node */

Node * insertnode (node * head, char ELEM)
{
If (null = head | null = ELEM)
Return head;

Node * Current = head-> next;/* current node */
Node * Prev = head;/* precursor node */
Node * temp;/* transition node */

While (current)/* move to the End Node */
{
Prev = current;
Current = Current-> next;
}

Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = ELEM;
Temp-> next = NULL;
Prev-> next = temp;/* the rear drive of the last node points to the new node */

Return head;

}
/* ===================================================== ======================================================= */

/* Delete a node */
Node * deletenode (node * head, char ELEM)
{
If (null = head | null = ELEM)
Return head;
If (null = head-> next)
Return head;

Node * Prev, * current;
Prev = head;
Current = head-> next;

While (current)
{
If (current-> ELEM = ELEM)/* match node element */
{
Prev-> next = Current-> next;/* the rear drive of the precursor node points to the next node of the current node */
Free (current);/* release the current node */
Return head;
}
Prev = current;
Current = Current-> next;/* move to next node */
}

Return head;

}

/* ===================================================== ======================================================= */

/* Reverse configuration of a single-chain table */
Node * reverselist (node * head)
{
If (null = head)
Return head;
If (null = head-> next)
Return head;
If (null = head-> next)
Return head;

Node * curr = head-> next;/* current node */
Head-> next = NULL;
Node * temp;

While (curr)
{
Temp = curr-> next;/* save the next node */
/* Insert the current node to the head node */
Curr-> next = head-> next;
Head-> next = curr;

Curr = temp;/* move to the next node */
}

Return head;

}

/* ===================================================== ======================================================= */

/* Calculate the intermediate node */
Node * middlenode (node * head)
{
If (null = head)
Return head;
If (null = head-> next)
Return head-> next;

Node * P1, * P2;
P1 = head;
P2 = head;

While (P2-> next)
{
/* P2 nodes move 2 node locations */
P2 = P2-> next;
If (P2-> next)/* determines whether the P2 drive node exists, and moves it again */
P2 = P2-> next;
/* The P1 node moves one node position */
P1 = p1-> next;

}
Return P1;

}

/* ===================================================== ======================================================= */

/* Merge ordered Single-Chain tables */
Node * mergelist (node * H1, node * H2)
{
If (null = h1 | null = h2)
Return H1;
If (null = h1-> next)
Return H2;
If (null = h2-> next)
Return H1;

Node * curr1, * curr2, * prev1, * temp;
Prev1 = h1;/* The precursor node of linked list 1 */
Curr1 = h1-> next;/* current node of linked list 1 */
Curr2 = h2-> next;/* current node of linked list 2 */
Temp = h2;
While (curr2)
{
While (curr1 & curr1-> ELEM <curr2-> ELEM)/* The 1 pointer of the linked list moves to the position of the current element of the large or equal linked list 2 */
Prev1 = curr1, curr1 = curr1-> next;

/* Insert the current element of linked list 2 into linked list 1 */
Temp = curr2-> next;/* save the next node of linked list 2 */
Prev1-> next = curr2;
Curr2-> next = curr1;

/* Move linked list 1 to the new node */
Curr1 = curr2;
/* Move linked list 2 to the next node */
Curr2 = temp;
}

Return H1;
}
/* ===================================================== ======================================================= */

/* Create a double-stranded table */
Dnode * doublelist (dnode * head)
{
If (null = head) // allocate the space of the header Node
Head = (dnode *) malloc (sizeof (dnode), head-> Prev = NULL, head-> next = NULL;

Dnode * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ n inputelem :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (dnode *) malloc (sizeof (dnode ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Temp-> Prev = current;/* forward of the new node to the current node */
Current = temp;/* the current node is the end node of the linked list */

}
Return head;
}

/* ===================================================== ======================================================= */
/* Output double-stranded table */
Void printdoublelist (dnode * head)
{
If (null = head)
Return;

Dnode * P;
P = head;
Cout <"\ n doublelistare :";
While (p-> next)
{
P = p-> next;
If (p-> ELEM)
Cout <SETW (5) <p-> ELEM;

}

Cout <"\ n doublelistare :";
While (p-> PREV)
{
If (p-> ELEM)
Cout <SETW (5) <p-> ELEM;
P = p-> Prev;
}

}

/* ===================================================== ======================================================= */
/* Create a circular linked list */
Node * cancelist (node * head)
{
If (null = head)/* allocate the space of the header node */
Head = (node *) malloc (sizeof (node), head-> next = NULL;

Node * Current = head, * temp;
Char ch;

While (1)
{
Cout <"\ n inputelem :";
Cin> CH;
If ('#' = CH)/* # End input */
Break;
Temp = (node *) malloc (sizeof (node ));
Temp-> ELEM = CH;
Temp-> next = NULL;
Current-> next = temp;/* the rear drive of the current node points to the new node */
Current = temp;/* the current node is the end node of the linked list */

}
Current-> next = head;/* The End Node points to the header node */
Return head;
}
/* ===================================================== ======================================================= */

/* Determine whether the linked list has a ring (Cyclic linked list )*/
Bool iscyclelist (node * head)
{
If (null = head)
Return false;
If (null = head-> next)
Return false;
Node * Current = head-> next;
While (current)
{
If (Head = Current-> next)
Return true;
Current = Current-> next;
}
Return false;
}
Int main ()
{
Node * head, * P;
Node * head2, * head3;
Dnode * dhead;
Char ch;
Head = NULL;
Head2 = NULL;
Head3 = NULL;
Dhead = NULL;

// Head = (node *) malloc (sizeof (node ));
// Head-> next = NULL;

// Create a single-chain table
Head = createlist (head );
Printlist (head );

Head2 = createlist (head2 );
Printlist (head2 );

// Insert a node
Cout <"\ N input ELEM toinsert :";
Cin> CH;
Insertnode (Head, CH );
Printlist (head );

// Delete a node
Cout <"\ N input ELEM todelete :";
Cin> CH;
Deletenode (Head, CH );
Printlist (head );

// Reverse configuration of a single-chain table
Head = reverselist (head );
Cout <"\ n reversed! ";
Printlist (head );

// Calculate the intermediate node
P = middlenode (head );
Cout <"\ n middle nodeis :";
Cout <p-> ELEM <Endl;

// Merge ordered Single-Chain tables
Mergelist (Head, head2 );
Cout <"\ n merged! ";
Printlist (head );

// Create a double-stranded table
Dhead = doublelist (dhead );
Printdoublelist (dhead );

/* Create a circular linked list and determine whether a ring exists */
Head3 = cancelist (head3 );
Cout <iscancelist (head3 );
Return 0;
}

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.