zoukankan      html  css  js  c++  java
  • 单链表的逆转

    1、采用c语言方法

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 typedef struct node
     5 {
     6     char data;
     7     struct node *next;
     8 }LNode;//单链表结点类型
     9 
    10 LNode *CreateLinkList()//生成单链表
    11 {
    12     LNode *head,*p,*q;
    13     char x;
    14     head=(LNode*)malloc(sizeof(LNode));//生成头结点
    15     head->next=NULL;
    16     p=head;
    17     q=p;//q始终指向链尾结点
    18     printf("Input any char string:
    ");
    19     scanf("%c",&x);
    20     while(x!='
    ')//生成链表的其它结点
    21     {
    22         p=(LNode*)malloc(sizeof(LNode));
    23         p->data=x;
    24         p->next=NULL;
    25         q->next=p;
    26         q=p;//q指向新的链尾
    27         scanf("%c",&x);
    28     }
    29     return head;//返回指向单链表的头指针head
    30 }
    31 
    32 void Convert(LNode *H)//单链表逆置
    33 {
    34     LNode *p,*q;
    35     p=H->next;//p始终指向剩余结点链表的第一个数据结点
    36     H->next=NULL;//新链表H初始为空
    37     while(p!=NULL)
    38     {
    39         q=p;//从剩余结点链表中取出第一个结点
    40         p=p->next;//p继续指向剩余链表新的第一个数据结点
    41         q->next=H->next;//将取出的结点*q插入到新链表H的链首
    42         H->next=q;
    43     }
    44 }
    45 
    46 void print(LNode *H)//打印链表
    47 {
    48  LNode *p=H->next;
    49  while(p!=NULL)
    50  {
    51      printf("%c ->",p->data);
    52      p=p->next;
    53  }
    54      printf("
    ");
    55 }
    56 
    57 void main()
    58 {
    59     LNode *A;
    60     A=CreateLinkList();//生成单链表A
    61     printf("打印输入链表:
    ");
    62     print(A);
    63     printf("打印逆向链表:
    ");
    64     Convert(A);//单链表A逆置
    65     print(A);
    66 }

    2.使用STL模板:

    a.使用队列操作,先把原来链表从队列尾部入队,后从队列头取出,进行链表的头部插入即可实现。

     1 #include<iostream>
     2 #include<list>
     3 #include<algorithm>
     4 #include<deque>//队列
     5 
     6 using namespace std;
     7 
     8 list<int>  convert(list<int> ilist)//单链表逆置
     9 {
    10     int length;
    11     deque<int> ideque;
    12     length=ilist.size();
    13     for(int i=0;i<length;++i)
    14     {ideque.push_back(*ilist.begin());
    15      ilist.pop_front();
    16     }
    17     length=ideque.size();
    18     for(i=0;i<length;++i)
    19     {
    20         ilist.push_front(*ideque.begin());
    21         ideque.pop_front();
    22     }
    23     return ilist;
    24 }
    25 
    26 void print(list<int> ilist)
    27 {
    28  list<int>::iterator ite;
    29  for(ite=ilist.begin();ite!=ilist.end();++ite)
    30         cout<<*ite<<"->";
    31         cout<<endl;
    32 }
    33 
    34 void main()
    35 {
    36     list<int> ilist;
    37     for(int i=1;i<10;i++)
    38     ilist.push_back(i);
    39     print(ilist);
    40     print(convert(ilist));
    41 }   

    b.使用栈操作,先把原来链表从头部入栈,后从栈顶取出,进行链表的尾部插入即可实现。

     1 #include<iostream>
     2 #include<list>
     3 #include<algorithm>
     4 #include<stack>//
     5 
     6 using namespace std;
     7 
     8 list<int>  convert(list<int> ilist) //单链表逆置
     9 {
    10     int length;
    11     stack<int> istack;
    12     length=ilist.size();
    13     for(int i=0;i<length;++i)
    14     {istack.push(*ilist.begin());
    15      ilist.pop_front();
    16     }
    17     length=istack.size();
    18     for(i=0;i<length;++i)
    19     {
    20         ilist.push_back(istack.top());
    21         istack.pop();
    22     }
    23     return ilist;
    24 }
    25 
    26 void print(list<int> ilist)
    27 {
    28  list<int>::iterator ite;
    29  for(ite=ilist.begin();ite!=ilist.end();++ite)
    30         cout<<*ite<<"->";
    31         cout<<endl;
    32 }
    33 
    34 void main()
    35 {
    36     list<int> ilist;
    37     for(int i=1;i<10;i++)
    38     ilist.push_back(i);
    39     print(ilist);
    40     print(convert(ilist));
    41 }

    结果:

  • 相关阅读:
    写代码的一些小心得
    javascript联动
    Web API 强势入门指南
    ajax(或者jquery)如何提交整个form表单
    JS Replace 详细用法讲解
    sql问题处理
    jQuery $.each详细用法讲解
    SQL中的declare用法
    C# 泛型
    A potentially dangerous Request.Form value was detected from the client
  • 原文地址:https://www.cnblogs.com/wxdjss/p/5450894.html
Copyright © 2011-2022 走看看