5--Linked list Output

Source: Internet
Author: User

/*title: (1) First write a single-linked list (2) to the chain list direction output problem-solving ideas: (a) The use of stack, LIFO strategy. (b) Recursion*/#include<stdio.h>#include<stdlib.h>#include<stack>#include<iostream>using namespaceStd;typedefstructlistnode{intM_nvalue; structListNode *M_pnext;} Lnode;voidListaddnode (Lnode *head) {Lnode*p = head, *p_inter =NULL; if(! (P_inter = ((Lnode *)malloc(sizeof(Lnode))))) {printf ("The memery is don ' t create it\n"); return; } p_inter->m_pnext =NULL; intdata; printf ("Please enter a number: \ n"); scanf_s ("%d", &data); P_inter->m_nvalue =data;  while(P->m_pnext! =NULL) {P= p->M_pnext; } P->m_pnext =P_inter;} Lnode* CreateList (Lnode *head) {    if(! (head = ((Lnode *)malloc(sizeof(Lnode))))) {printf ("The memery is don ' t create it\n"); returnNULL; } head->m_pnext =NULL; intdata; printf ("Please enter a number: \ n"); scanf_s ("%d", &data); Head->m_nvalue =data; Lnode*p = head, *p_inter =NULL; CharX_cin ='Y';  while(true) {printf ("do you want to continue adding: n/n \ n"); CIN>>x_cin; if(X_cin = ='y'|| X_cin = ='Y')        {            ; }        Else if(X_cin = ='N'|| X_cin = ='N')        {            returnHead; }        Else        {            ;    } listaddnode (P); }}voidShowlist (Lnode *head) {    if(NULL = =head) {cout<<"list is empty \ n"<<Endl; return; } Lnode*p =Head;  while(P! =NULL) {printf ("%d\n", p->m_nvalue); P= p->M_pnext; }}voidReverseput (Lnode *Point ) {Stack<int>Stack_rev; Lnode*p =Point ;  while(P! =NULL) {Stack_rev.push (P-m_nvalue); P= p->M_pnext; }     while(!Stack_rev.empty ()) {cout<< stack_rev.top () <<Endl;    Stack_rev.pop (); }}intMain () {Lnode*head =NULL; Head=CreateList (head);    Showlist (head);        Reverseput (head); return 0;}

5--Linked list Output

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.