zoukankan      html  css  js  c++  java
  • List倒数第K个数

    倒数第K个数和中间数。

    自己编码能力还不行,要多多加油才行啊!!!

    View Code
      1 #include<iostream>
    2 #include<string>
    3 using namespace std;
    4
    5 template<class Type> class List
    6 {
    7 private:
    8 template<class T> class LinkNode
    9 {
    10 public:
    11 LinkNode<Type> *link;
    12 Type data;
    13 LinkNode(LinkNode<Type>* ptr=NULL)
    14 {
    15 link=ptr;
    16 }
    17 LinkNode(const Type& item,LinkNode<Type>* ptr=NULL)
    18 {
    19 data=item;
    20 link=ptr;
    21 }
    22 };
    23 LinkNode<Type>* first;
    24 public:
    25 List()
    26 {
    27 first=new LinkNode<Type>; //想一下为什么不能写成LinkNode<Type>* ptr=NULL; first=ptr;
    28 }
    29
    30 void inputRear(const Type& endtag)
    31 {
    32 LinkNode<Type> *newNode,*curr;
    33 curr=first;
    34 Type val;
    35 cin>>val;
    36 while(val!=endtag)
    37 {
    38 newNode=new LinkNode<Type>(val);
    39 curr->link=newNode;
    40 curr=curr->link;
    41 cin>>val;
    42 }
    43 }
    44
    45 void output()
    46 {
    47 LinkNode<Type> *curr=first->link;
    48 while(curr!=NULL)
    49 {
    50 cout<<curr->data<<"";
    51 curr=curr->link;
    52 }
    53 cout<<endl;
    54 }
    55
    56
    57 void findReciprocalOfNum(int num) //需要遍历两次
    58 {
    59 int count=0;
    60 LinkNode<Type> *curr1,*curr2;
    61 curr1=curr2=first;
    62 while(curr1->link!=NULL)
    63 {
    64 count++;
    65 curr1=curr1->link;
    66 }
    67 if(count<num)
    68 {
    69 cout<<"链表长度不够,找不到倒数为第"<<num<<"个元素!";
    70 return;
    71 }
    72 count=count-num+1;
    73 while(count)
    74 {
    75 curr2=curr2->link;
    76 count--;
    77 }
    78 cout<<"倒数第"<<num<<"个元素为:"<<curr2->data<<endl;
    79 }
    80
    81 void findReciprocalOfNum2(int num)
    82 {
    83 LinkNode<Type> *flag,*loop;
    84 flag=loop=first;
    85 for(int i=0;i<num;i++)
    86 {
    87 loop=loop->link;
    88 if(loop==NULL)
    89 {
    90 cout<<"链表少于"<<num<<"个元素"<<endl;
    91 return;
    92 }
    93 }
    94 while(loop!=NULL)
    95 {
    96 loop=loop->link;
    97 flag=flag->link;
    98 }
    99 cout<<"倒数第"<<num<<"个元素为:"<<flag->data<<endl;
    100 }
    101
    102
    103 void findMidElement()
    104 {
    105 LinkNode<Type> *odd,*even;
    106 odd=even=first;
    107 while(even->link!=NULL)
    108 {
    109 odd=odd->link;
    110 even=even->link;
    111 if(even->link==NULL)
    112 {
    113 cout<<"中间元素是"<<odd->data<<endl;
    114 return ;
    115 }
    116 even=even->link;
    117 }
    118 cout<<"中间元素是"<<odd->data<<""<<odd->link->data<<endl;
    119 }
    120
    121
    122 };
    123
    124
    125 int main()
    126 {
    127 List<string> list;
    128 list.inputRear("end");
    129 list.output();
    130 list.findReciprocalOfNum(5);
    131 list.findReciprocalOfNum2(6);
    132 list.findMidElement();
    133 }
  • 相关阅读:
    论文笔记之:Speed Up Tracking by Ignoring Features
    深度学习中常见的几个基础概念
    (转)The AlphaGo Replication Wiki
    论文笔记之:Co-saliency Detection via A Self-paced Multiple-instance Learning Framework
    (转)CVPR 2016 Visual Tracking Paper Review
    论文笔记之:Generative Adversarial Text to Image Synthesis
    论文笔记之:Conditional Generative Adversarial Nets
    论文笔记之:Progressive Neural Network Google DeepMind
    (转)Let’s make a DQN 系列
    论文阅读之:Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2238620.html
Copyright © 2011-2022 走看看