DS-based linked list for reverse local configuration

Source: Internet
Author: User
Tags random seed

DS-based linked list for reverse local configuration

The reverse setting of data elements in a linked list is a very common example. An additional inverse function is required for the blog to reverse the input data in an ordered table, to implement the inverse function in the main function, you also need to call the inverse function. This article uses the knowledge of linked lists to implement local reverse configuration and output reverse data elements.

First, we need to build a single-chain table of the leading node, and then redefine the node type. The code for implementing local inversion in the main function is:

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h> typedef int ElemType; typedef struct LNode // defines the node type of the linked list {ElemType data; struct LNode * next;} LNode, * LinkList; int main () {LinkList L, q; L = (LinkList) malloc (sizeof (LNode); // The requested header node L-> next = NULL; LinkList p = L; cout <"Enter 10 Data Elements of the linked list:"; for (int I = 0; I <10; I ++) {LinkList s = (LinkList) malloc (sizeof (LNode); // apply for a new node cin> s-> data; // the data element p-> next = s of the input node; p = s;} p-> next = NULL; p = L-> next; cout <"sequential output of the linked list:"; while (p) // implement the sequential output of the chain table {cout <p-> data <","; p = p-> next ;}cout <endl; q = L-> next; L-> next = NULL; while (q) // implement reverse local settings of the linked list {p = q-> next; q-> next = L-> next; L-> next = q; q = p;} p = L-> next; cout <"reverse output :"; while (p) // output {cout <p-> data <","; p = p-> next;} cout <endl; return 0 ;}</span>

If the input ten data elements to the linked list are: 0 1 2 3 4 5 6 7 8 9

The output result is:

 

This input is quite troublesome, so we can use the method of generating random numbers to implement the input data element, and implement the independent algorithm of the inverse single-chain table to distinguish it from the main function. The complete code is:

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h ># include <ctime> typedef int ElemType; typedef struct LNode // defines the node type of the linked list {ElemType data; struct LNode * next;} LNode, * LinkList; int main () {srand (time (0); // implement Random Seed LinkList L, q; L = (LinkList) malloc (sizeof (LNode )); // The requested header node L-> next = NULL; LinkList p = L; for (int I = 0; I <10; I ++) {LinkList s = (LinkList) malloc (sizeof (LNode); // apply for a new node s-> data = rand () % 100 + 1; // The Data Element p-> next = s; p = s;} p-> next = NULL; p = L-> next; cout <"sequential output of the linked list:"; while (p) // implement sequential output of the linked list {cout <p-> data <","; p = p-> next;} cout <endl; q = L-> next; L-> next = NULL; while (q) // implement reverse {p = q-> next; q-> next = L-> next; L-> next = q; q = p ;} p = L-> next; cout <"reverse output:"; while (p) // reverse output of the local linked list {cout <p-> data <", "; p = p-> next;} cout <endl; return 0 ;}</span>

Click to run the program twice and the result is

 

 

 

 


 

 

 

 

 


 

 

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.