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

    非常简单,就当练个手吧

     1 //单链表逆序问题,其实很容易的,就是把指针指向给变一下,注意的几个问题
     2 //(1)如果就一个元素,不算头结点,直接返回
     3 //(2)注意头结点最后要单独处理问题
     4 #include <iostream>
     5 using namespace std;
     6 
     7 typedef struct linknode{
     8     int val;
     9     linknode * next;
    10 }node,*list;                  //加typedef说明node和list是类型,否则只是一个struct的变量
    11 
    12 void reverse(list head)
    13 {
    14     if(head==NULL||head->next==NULL||head->next->next==NULL)
    15         return;
    16     node * p=head->next;
    17     node * q=p->next;
    18     node * t;
    19     p->next=NULL;
    20     while(q!=NULL)
    21     {
    22         t=q->next;
    23         q->next=p;
    24         p=q;
    25         q=t;
    26     }
    27     head->next=p;
    28 }
    29 list createlist()
    30 {
    31     int data;
    32     list head=new node;
    33     head->val=0;
    34     head->next=NULL;
    35     node * p=head;
    36     while(cin>>data)
    37     {
    38         node * tmp=new node;
    39         tmp->val=data;
    40         tmp->next=NULL;
    41         p->next=tmp;
    42         p=tmp;
    43     }
    44     return head;
    45 }
    46 
    47 int main()
    48 {
    49     node * head=createlist();
    50     reverse(head);
    51     node *p=head->next;
    52     while(p)
    53     {
    54         node *q=p->next;
    55         cout<<p->val<<" ";
    56         delete p;
    57         p=q;
    58     }
    59     cout<<endl;
    60     system("pause");
    61 }
  • 相关阅读:
    websocket简单理解
    对两个列表合成一个列表后进行排序
    爬取今日头条财经版块新闻
    Python的hasattr(),getattr(),setattr()
    Django基础
    pymysql模块的使用
    我一定要学好英语
    Django项目的创建
    MySQL数据库(安装+增删改查)
    jQuery
  • 原文地址:https://www.cnblogs.com/zmlctt/p/3842566.html
Copyright © 2011-2022 走看看