zoukankan      html  css  js  c++  java
  • 链表的拼接及反转

    链表拼接和反转常考查的内容,所以需要注意以及熟练地写出来

      1 // 链表拼接.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include "stdafx.h"
      5 #include<iostream>
      6 
      7 using namespace std;
      8 
      9 //链表结点
     10 typedef struct list
     11 {
     12     int x;
     13     struct list* next;
     14 }*Linklist;
     15 
     16 //函数声明
     17 void init_list(Linklist& l1);//初始化链表并赋值
     18 void destroy_list(Linklist& L);//销毁链表
     19 void show_list(Linklist& L);//打印链表中的元素
     20 void merge_list(Linklist& l1, Linklist& l2);//合并有序链表
     21 void reverse_list(Linklist& l1);//反转链表
     22 
     23 int main()
     24 {
     25     Linklist l1, l2;
     26 
     27     init_list(l1);
     28     init_list(l2);
     29     show_list(l1);
     30     show_list(l2);
     31 
     32     merge_list(l1, l2);
     33     show_list(l1);
     34 
     35     reverse_list(l1);
     36     show_list(l1);
     37 
     38     destroy_list(l1);
     39     destroy_list(l2);
     40 
     41     return 0;
     42 }
     43 
     44 //初始化链表并赋值
     45 void init_list(Linklist& l1)
     46 {
     47     l1 = new list;
     48     l1->next = NULL;
     49 
     50     Linklist p = l1, q;
     51 
     52     cout << "please input the number of list:"<<endl;
     53 
     54     int n,temp;
     55 
     56     cin >> n;
     57 
     58     for (int i = 0; i < n; ++i)
     59     {
     60         cin >> temp;
     61 
     62         q = new list;
     63 
     64         q->x = temp;
     65         p->next = q;
     66         q->next = NULL;
     67         p = p->next;
     68     }
     69 
     70 }
     71 
     72 //销毁链表
     73 void destroy_list(Linklist& L)
     74 {
     75     Linklist p = L;
     76 
     77     while (L != NULL)
     78     {
     79         L = L->next;
     80 
     81         delete p;
     82 
     83         p = L;
     84     }
     85 
     86 }
     87 
     88 //打印链表中的元素
     89 void show_list(Linklist& L)
     90 {
     91     Linklist p = L->next;
     92 
     93     while (p != NULL)
     94     {
     95         cout << p->x << " ";
     96 
     97         p = p->next;
     98     }
     99 
    100     cout << endl;
    101 }
    102 
    103 //合并有序链表
    104 void merge_list(Linklist& l1,Linklist& l2)
    105 {
    106     Linklist p = l1, q = l2->next;;
    107 
    108     while (q!=NULL&&p->next!=NULL)
    109     {
    110         if (p->next->x < q->x)
    111             p = p->next;
    112         else
    113         {
    114             l2->next = q->next;//断开
    115 
    116             //连接链表
    117             q->next = p->next;
    118             p->next = q;
    119             p = p->next;
    120 
    121             q = l2->next;
    122         }
    123     }
    124 
    125     if (p->next == NULL)
    126     {
    127         p->next = q;
    128 
    129         l2->next = NULL;
    130     }
    131 }
    132 
    133 //反转链表
    134 void reverse_list(Linklist& l1)
    135 {
    136     Linklist p = l1->next,q;
    137 
    138     while (p->next != NULL)
    139     {
    140         q = p->next;
    141 
    142         p->next = q->next;
    143         q->next = l1->next;
    144         l1->next = q;
    145     }
    146 }
  • 相关阅读:
    TCP心跳包
    interesting site
    TestNG环境搭建以及框架初识
    lambda表达式
    subprocess学习
    使用psutil模块获取电脑运行信息
    使用ssh和putty操控远程的linux server
    ubuntu系统源的更新
    将python的程序包装成windows下的service
    使用python进行re拆分网页内容
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/7250454.html
Copyright © 2011-2022 走看看