Code for linked list explanations and basic operation exercises

Source: Internet
Author: User

The following are the basic operations on a single-chain table. I have written them all over again. If you do not write the linked list for a long time, you will be unfamiliar with it. I will forget to take a look at it later, by the way, I 'd like to remind anyone who wants to learn the linked list.

First, write the header file head. h.

All examples here are linked lists with header nodes and single-chain table operations.

1) create a table with header insertion and Distribution 2) create a table with tail Insertion Method 3) print the linked list 4) insert and sort the values of the linked list 5) flip the linked list 6) find the nth element at the bottom of the linked list. 7) link two linked lists. 8) change a single linked list to a chain table.

9) Determine whether the linked list has a ring. 10) sort the existing linked list by insertion (different from 4). 4) sort the existing linked list when creating the linked list. 11) delete the repeat element of the linked list 12) Determine whether the linked list has an intersection point and output 13) make the two linked lists have an intersection point.

#ifndef _LINK_H_#define _LINK_H_#include<iostream>#include<stdio.h>#include<time.h>#define outusing namespace std;struct node_stu{int num;node_stu *next;};void link_init_tail(node_stu **head,int size);void link_init_head(node_stu **head,int size);void print_link(node_stu *head);void link_init_sort_insert(node_stu **head,int size);void turn_link(node_stu **head);int find_back_num(node_stu *head,int n);int find_back_num(node_stu *head);void unite_links(node_stu **head1,node_stu **head2);void make_link_into_circle(node_stu **head);int judge_circle(node_stu *head);void sort_link(node_stu **head);void delete_repetition(node_stu** head);int count_intersection(node_stu *head1,node_stu *head2,node_stu** node_intersection);void make_intersection_link(node_stu** head1,node_stu** head2,int ,int );#endif

 

1) create a table by inserting the table into the table

#include"head.h"void link_init_head(node_stu **head,int size){*head=(node_stu*)calloc(1,sizeof(node_stu));node_stu *p=*head;p->next=NULL;for(int i=0;i<size;i++){node_stu *temp=(node_stu*)calloc(1,sizeof(node_stu));temp->num=rand()%100;;temp->next=p->next;p->next=temp;}}

2) create a table by means of end insertion

#include"head.h"void link_init(node_stu **head,int size){node_stu *p;*head=(node_stu *)calloc(1,sizeof(node_stu));p=*head;p->next=NULL;for(int i=0;i<size;i++){p->next=(node_stu *)calloc(1,sizeof(node_stu));p=p->next;p->num=rand()%100;p->next=NULL;}}
 
 
3) print the linked list
 
#include"head.h"void print_link(node_stu* head){while(head->next!=NULL){head=head->next;cout<
 
4) insert and sort the values of the linked list.
 
 
<PRE class = "CPP" name = "code"> # include "head. H "Void link_init_sort_insert (node_stu ** head, int size) {* head = (node_stu *) calloc (1, sizeof (node_stu )); // define the header pointer node_stu * P = * head, * pre = * head; // define the pointer P and the front pointer pre (* head)-> next = NULL; // initialize the header pointer (* head)-> num =-1; for (INT I = 0; I <size; I ++) {P = * head; node_stu * temp = (node_stu *) calloc (1, sizeof (node_stu); temp-> num = rand () % 100; // define the node and assign the random number cout <temp-> num <""; temp-> next = NULL; Pre = P; int Flag_mid = 0; // mark the inserted while (p-> next! = NULL) {pre = P; P = p-> next; // P explores the path ahead of the path, followed by Pre, then temp finds the proper position and inserts it in the appropriate position of pre and P. If (temp-> num) >=( pre-> num) & (temp-> num) <= (p-> num) {flag_mid = 1; break ;}} if (flag_mid! = 1) // insert in the header or insert p-> next = temp; else {pre-> next = temp; // insert temp-> next = P ;}} between pre and P ;}}}

5) flip the linked list

# Include "head. H "Void turn_link (node_stu ** head) {// node_stu * newhead = (node_stu *) calloc (1, sizeof (node_stu); // newhead-> next = NULL; sort the chain table to node_stu * P = * head; P = p-> next; (* head)-> next = NULL; while (P! = 0) {node_stu * temp = (node_stu *) calloc (1, sizeof (node_stu); // defines a temporary struct node temp = P; P = p-> next; temp-> next = NULL; // extract a node from the head of the linked list temp-> next = (* head)-> next; // insert this node (header) insert to the header (* head)-> next = temp ;}}

6) Find the nth element of the linked list.
 
 
# Include "head. H "int find_back_num (node_stu * head, int N) {node_stu * pre; Pre = head; // two pointers to the roof, one head, one pre, and the middle interval NN --; while (n --) Pre = pre-> next; while (pre-> next! = NULL) {// When pre arrives, the head is the last n pre = pre-> next; head = head-> next;} return head-> num ;}
 
7) connect two linked lists
 
<PRE class = "CPP" name = "code"> # include "head. H "Void unite_links (node_stu ** head1, node_stu ** head2) {// combine the two linked lists with node_stu * P; P = (* head1); While (p-> next! = NULL) P = p-> next; P-> next = (* head2)-> next; // Delete (node_stu *) head2 ;}

8) Turn the linked list into a circular linked list

<PRE class = "CPP" name = "code"> # include "head. H "Void make_link_into_circle (node_stu ** head) {// The node_stu * P; P = (* head); While (p-> next! = NULL) P = p-> next; P-> next = (* head)-> next ;}

9) Determine whether the linked list has a ring

<pre class="cpp" name="code">#include"head.h"int judge_circle(node_stu *head){node_stu *p;p=head->next;head=head->next;while(head!=NULL){head=head->next;if(head==p)return 1;}return 0;}

10) insert and sort the linked list

The main idea is to remove a vertex from the linked list and use the header of the original linked list as the new header for insertion and sorting.
<PRE class = "CPP" name = "code"> # include "head. H "Void sort_link (node_stu ** head) {// sort the node_stu * Pre, * find_p, * original_p, * sort_p; // sort_p = (* head) -> next; original_p = (* head)-> next; // records the position of the original string (* head)-> next = NULL; // assign the header node next null, which starts as a new string find_p = (* head); // locate the position of the byte in the new string, corresponds to pre. find_p is on the left and pre is on the right while (original_p! = NULL) {// sort_p = original_p; // node where a new sort string needs to be added original_p = original_p-> next; // record the position of the original string to move back sort_p-> next = NULL; // isolate sort-P from find_p = (* head ); // start from the new header node every time to find int flag_mid = 0; // mark in the middle to insert while (find_p-> next! = NULL) {// pre = find_p; // find_p = find_p-> next; // search for the insert value if (sort_p-> num)> = (pre-> num) & (sort_p-> num) <= (find_p-> num) {// flag_mid = 1; // break ;}} if (flag_mid! = 1) // insert in the header or find_p-> next = sort_p; else {pre-> next = sort_p; // insert sort_p in the middle-> next = find_p ;}} 11) delete duplicate elements in the linked list
 
<PRE class = "CPP" name = "code"> # include "head. H "Void delete_repetition (node_stu ** head) {node_stu * end, * First, * temp; end = (* head)-> next; first = end-> next; while (first! = NULL) {If (end-> num = first-> num) {// Delete temp = end-> next when the same is encountered; end-> next = first-> next; first = first-> next; Delete (node_stu *) temp;} First = first-> next; end = end-> next ;}}


 

12) determine whether two linked lists have intersection points and output the intersection points.


 
# Include "head. H "int count_intersection (node_stu * head1, node_stu * head2, node_stu ** node_intersection) {int len1 = 0, len2 = 0, Len; node_stu * P1, * P2; p1 = head1; P2 = head2; while (P1-> next! = NULL) {// calculate the length of P1, len1 ++; P1 = p1-> next;} while (P2-> next! = NULL) {// calculate the length of P2 len2 ++; P2 = P2-> next;} Len = len1> len2? Len2: len1; P1 = head1; P2 = head2; while (Len --) // obtain the minimum value and find the last Len node of P1. p1 = p1-> next; while (P1-> next! = NULL) {p1 = p1-> next; head1 = head1-> next;} Len = len1> len2? Len2: len1; while (Len --) // obtain the minimum value and then find the P2 reciprocal Len node P2 = P2-> next; while (P2-> next! = NULL) {P2 = P2-> next; head2 = head2-> next;} while (head1-> next! = NULL) {// when P1 and P2 are the reciprocal Len at the same time, start backward comparison to check whether there are intersection head1 = head1-> next; head2 = head2-> next; if (head1 = head2) {* node_intersection = head1; // return the intersection return 1 ;}} return 0 ;}
 
13) intersection two linked lists
 
# Include "head. H "Void make_intersection_link (node_stu ** head1, node_stu ** head2, int N, int m) {// M generated, n is the head1 linked list, the input N is to set the nth node to the intersection of head1, and set m to the intersection of node_stu * P, * P1; * head2 = (node_stu *) calloc (1, sizeof (node_stu); P = * head2; P-> next = NULL; For (INT I = 0; I <m; I ++) {P-> next = (node_stu *) calloc (1, sizeof (node_stu); P = p-> next; P-> num = rand () % 100; p-> next = NULL;} p1 = * head1; // here, the chain table head1 is the while (n --) created previously. // Add head1 to N, p1 = p1-> next; P-> next = p1; // The m position of the newly generated head2 points to the N position of head1}
 
The primary function I wrote is used to test each function.
<PRE class = "CPP" name = "code"> # include "head. H "int main (INT argc, char * argv []) {srand (Time (null); node_stu * head = NULL; node_stu * head2 = NULL; node_stu * head3 = NULL; node_stu * node_intersection = NULL; int intersection_a, intersection_ B; int size, N; while (~ Scanf ("% d", & size) {// head plug/link_init_tail (& head, size); // tail plug/link_init_head (& head, size ); printf ("sort after insertion: \ n"); link_init_sort_insert (& head, size); // insert is sort cout <Endl; print_link (head ); // print the string printf ("chain table flip: \ n"); turn_link (& head); // chain table flip print_link (head ); // printf ("elements to which the output goes down:"); While (scanf ("% d", & N), (N> size | n <= 0 )) printf ("the input number range is incorrect. Please re-enter \ n"); //, printf ("% d \ n", find_back_num (Head, n )); // printf ("output link intermediate element: \ n"); cout <find_back_num (head) <Endl; // printf ("output two strings: \ n "); print_link (head); link_init_head (& head2, size );///
Print_link (head2); printf ("merged: \ n"); unite_links (& head, & head2); // print_link (head );/***
// Printf ("judge whether the linked list has a ring \ n"); // print_link (head2); // make_link_cir_circle (& head2 ); // The linked list is programmed with a chain table // If (judge_circle (head2) // printf ("with a ring \ n "); // else // printf ("Wuhuan \ n"); * // *****/printf ("sorting linked lists \ n"); sort_link (& head ); print_link (head);/**/printf ("delete duplicate element \ n"); delete_repetition (& head ); ///////////// // print_link (head); printf ("determines whether two linked lists have loops, if an output ring \ n "); printf (" creates two linked lists with intersections, input the first node of the head linked list to be the intersection, and the first node of the new linked list to be the intersection; "); scanf (" % d ", & intersection_a, & intersection_ B); make_intersection_link (& head, & head2, intersection_a, intersection_ B); // link_init_head (& head2, size); If (count_intersection (Head, head2, & node_intersection) {printf ("with \ n"); cout <node_intersection-> num <Endl ;} else printf (" \ n");} system ("pause ");}


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.