zoukankan      html  css  js  c++  java
  • 【数据结构上机练习】4.链表的简单操作(2)

    上机3.1    第三次上机第一题目,比较简单,链表类用了之前定义的。

    题目:

      1 //============================================================================
      2  // Name        : shangji3.1.cpp
      3  // Author      : menglei
      4  // Version     : 2012.10.30
      5  // Copyright   : Your copyright notice
      6  // Description : Hello World in C++, Ansi-style
      7  //============================================================================
      8  /**
      9   * 1、设计一个算法,将单链表中元素的次序完全颠倒
     10   */
     11  
     12  #include <iostream>
     13  #include<stdio.h>
     14  #include<stdlib.h>
     15  
     16  #define len sizeof(struct data)
     17  using namespace std;
     18  /*定义一个结构体,data  */
     19  typedef struct data{
     20      int num;
     21      class data *next;
     22  }data;
     23  
     24  /*下面定义链表类*/
     25  class mlink{
     26  private:
     27      data *head;   //   为链结的数据
     28  public :
     29      //mlink();    //无参构造函数
     30      mlink(int n);    //有参构造函数,传入参数n
     31      void add(int n);  //添加
     32      void delHead (void);  //删除
     33      int  find(int n);   //查找
     34      void print(void)const;
     35      void addtail(int n);   //add elements at tail
     36      void transform();//链表的反序************************************   new   ********
     37      int findValue(int pos){   //查找指定位置的元素
     38          data *p;
     39          p = head;
     40          int count=1;
     41          if(pos<=0)
     42          {
     43              cout<<"不存在的位置\n";
     44              return 0;
     45          }
     46          while(p!=NULL){
     47              if(count == pos)
     48                  return p->num;
     49              p = p->next;
     50              count ++;
     51          }
     52      }
     53  };
     54  void mlink::addtail(int n){   //add at tail
     55      data *temp = (data*)malloc (len);
     56      data *p;
     57      temp->num=n;
     58      cout<<"【添加】在表尾添加元素:"<<n<<"\n";
     59      if(head->next==NULL){
     60          head->next=temp;
     61      }else  //头非空
     62      {
     63          p=head->next;
     64          while (p->next!=NULL){  //find tail element
     65              p=p->next;
     66          }
     67          p->next=temp;
     68          temp->next=NULL;
     69      }
     70  }
     71  void mlink::print(void)const{    //print link
     72      data *p = head;
     73      while(p!=NULL){
     74          printf("%d",p->num);
     75          p = p->next;
     76          if(p!=NULL)
     77              printf("->");
     78      }
     79      printf("\n");
     80  }
     81  void mlink::add(int n){          //add at head
     82      data *temp =(data*) malloc(len);   //申请空间
     83      /*在头结点后加入结点*/
     84      if(head==NULL){
     85          printf("链表为空,只有表头\n");
     86          temp->num = n ;   //赋值
     87          head->next = temp;
     88          temp->next=NULL;
     89      }
     90      else   // 链表非空
     91      {
     92          data *p1;
     93          printf("【添加】链表非空,在表头添加结点:%d\n",n);
     94          temp->num= n;
     95          p1=head;
     96          head=temp;
     97          temp->next=p1;
     98      }
     99  }
    100  void mlink::delHead(void){   //删除
    101      //删除头结点
    102      data *p,*q;
    103      p=head->next;
    104      q=head;
    105      head=p;
    106      q->next=NULL;
    107      free(q);
    108      printf("【删除】头元素删除成功!\n");
    109  }
    110  int  mlink::find(int n){  //元素n存在于链表中则返回位置
    111      data *p = head;
    112      int count = 1;
    113      while(p!=NULL){
    114          if(p->num==n)
    115              return count;
    116          else
    117          {
    118              p=p->next;
    119              count++;
    120          }
    121      }
    122      return 0;
    123  }
    124  mlink::mlink(int n){
    125      data *first =(data*) malloc(len);   //申请空间
    126      //data *head = new data;
    127      head=first;  //****************important**********
    128      cout<<"【创建】创建链表    "<<"头结点为:"<<n<<endl;
    129      first->num=n;    //把n传入
    130      first->next=NULL;
    131  }
    132  void mlink::transform(){//****************************************  new  *********************
    133      //对链表进行反序操作
    134      data *prep, *nowp ,  *nextp;
    135      prep = NULL;
    136      nowp = head;
    137      while(nowp->next != NULL){
    138          nextp = nowp ->next;   //赋值
    139          nowp ->next = prep;    //改值
    140          prep = nowp;           //移动
    141          nowp = nextp;          //移动
    142      }
    143      nowp->next = prep;
    144      head = nowp ;
    145      cout<<"反序完成!"<<endl;
    146  
    147  }
    148  
    149  
    150  int main() {
    151      cout<<"hehe"<<endl;
    152      mlink m(37);
    153      m.add(3);
    154      m.addtail(5);
    155      m.addtail(32);
    156      m.addtail(65);
    157      m.addtail(34);
    158      m.addtail(26);
    159      m.print();
    160      cout<<"链表创建完成,下面对链表进行反序操作"<<endl;
    161      m.transform();
    162      m.print();
    163  
    164  
    165  
    166  
    167      return 0;
    168  }
    169  

    输出结果:

    注:编译环境为eclipse和codeblocks联合使用

    转载文章请注明出处: http://www.cnblogs.com/menglei/
  • 相关阅读:
    但是难道我就不能在JBuilder里面看我可爱的中文了吗?[小糊涂的灵感]
    why is j2me midp superior to wap?[小糊涂的灵感]
    NRF51822之GPIOTE介绍
    Unity脚本生命周期
    树状数组学习笔记
    模版—扩展欧几里德
    hdu4339Query
    POJ3273Monthly Expense
    多校第一场
    ZOJJ Watashi's BG3631
  • 原文地址:https://www.cnblogs.com/menglei/p/2746546.html
Copyright © 2011-2022 走看看