C++实验4

 1、学生类定义

#include <iostream>
#include <string.h>
using namespace std;
class student
{
    char name[20], id[20], college[20], major[20], sexy[10], address[20], phones[20];

public:
    void init(char name1[20], char id1[20], char college1[20], char major1[20], char sexy1[10], char address1[20], char phones1[20])
    {
        memcpy(name, name1, sizeof(name));
        memcpy(id, id1, sizeof(id));
        memcpy(college, college1, sizeof(college));
        memcpy(major, major1, sizeof(major));
        memcpy(sexy, sexy1, sizeof(sexy));
        memcpy(address, address1, sizeof(address));
        memcpy(phones, phones1, sizeof(phones));
    }
    void print();
};
int main()
{
    int t;
    cin >> t
    while (t--)
    {
        char name[20], id[20], college[20], major[20], sexy[10], address[20], phones[20];
        cin >> name >> id >> college >> major >> sexy >> address >> phones;
        student stu;
        stu.init(name, id, college, major, sexy, address, phones);
        stu.print();
    }
}
void student::print()
{
    cout << name << " " << id << " " << college << " " << major << " " << sexy << " " << address << " " << phones << endl;
};

2、存折类定义

#include <iostream>
#include <string.h>
using namespace std;
class CAccount
{
    long account;
    char name[10];
    float balance;

public:
    void yu_e();
    void deposit(float deposit);
    void withdraw(float withdraw_money);
    int check(float withdraw_money);
};
int main()
{
    CAccount C1, C2;
    long account1, account2;
    char name1[10], name2[10];
    float balance1, balance2;
    float deposit1, deposit2;
    float withdraw_money1, withdraw_money2;
    C1.yu_e();
    cin >> deposit1 >> withdraw_money1;
    C1.deposit(deposit1);
    C1.withdraw(withdraw_money1);
    C2.yu_e();
    cin >> deposit2 >> withdraw_money2;
    C2.deposit(deposit2);
    C2.withdraw(withdraw_money2);
}
void CAccount::yu_e()
{
    cin >> account >> name >> balance;
    cout << name << "'s balance is " << balance << endl;
}
void CAccount::deposit(float deposit)
{
    balance += deposit;
    cout << "saving ok!" << endl;
    cout << name << "'s balance is " << balance << endl;
}
void CAccount::withdraw(float withdraw_money)
{
    if (CAccount::check(withdraw_money) != 0)
    {
        balance -= withdraw_money;
        cout << "withdraw ok!" << endl;
        cout << name << "'s balance is " << balance << endl;
    }
    else
    {
        cout << "sorry! over limit!" << endl;
        cout << name << "'s balance is " << balance << endl;
    }
}
int CAccount::check(float withdraw_money)
{
    if (withdraw_money > balance)
        return 0;
    else
        return 1;
}

 3、音像制品

#include <iostream>
using namespace std;
#include <string.h>
class Audio
{
    int type;
    char name[5];
    int rent;
    int state;

public:
    void set_Audio();
    void print();
    void Free(int day);
    Audio()
    {
        type = 0;
        strcpy(name,"XXX");
        rent = 0;
        state = 0;
    }
};
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int type;
        char name[5];
        int rent;
        int state;
        int operate;
        Audio Au;
        Au.set_Audio();
        cin >> operate;
        Au.print();
        if (operate)
            Au.Free(operate);
    }
}
void Audio::set_Audio()
{
    cin >> type >> name >> rent >> state;
}
void Audio::print()
{
    char this_type[20];
    char statement[20];
    if (type == 1)
        strcpy(this_type, "黑胶片");
    else if (type == 2)
        strcpy(this_type, "CD");
    else if (type == 3)
        strcpy(this_type, "VCD");
    else
        strcpy(this_type, "DVD");
    if (state)
        strcpy(statement, "已出租");
    else
        strcpy(statement, "未出租");
    cout
        << this_type << "[" << name << "]" << statement << endl;
}
void Audio::Free(int day)
{
    if (!state)
        cout << "未产生租金" << endl;
    else
        cout << "当前租金为" << day * rent << endl;
}

4、月份查询

#include <iostream>
using namespace std;
class date
{
    int year;
    int month;
    int day;

public:
    void Get(date &, int year1, int month1, int day1);
    void now_next(date &da);
    void remain(date &a);
};
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int year, month, day;
        cin >> year >> month >> day;
        date da;
        da.Get(da, year, month, day);
        da.now_next(da);
        da.remain(da);
    }
}
void date::Get(date &, int year1, int month1, int day1)
{
    year = year1;
    month = month1;
    day = day1;
}
void date::now_next(date &da)
{
    char month_name[12][15] = {{"January"}, {"February"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"August"}, {"September"}, {"October"}, {"November"}, {"December"}};
    if (month == 12)
        cout << "This month is December and next month is January" << endl;
    else
        cout << "This month is " << month_name[month - 1] << " and next month is " << month_name[month] << endl;
}
void date::remain(date &a)
{
    int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, total = 365, now=0,surplus=0;
    if (year % 400 == 0 || ((year % 4) == 0) && (year % 100) != 0)
    {
        days[1] = 29;
        total = 366;
    }
    for (int i = 0; i < month-1; i++)
    {
        now+=days[i];
    }
    now+=day;
    surplus=total-now;
    cout<<"There are "<< surplus<<" days to the end of the year"<<endl;
}

 5、单链表

#include<stdio.h>
#include <iostream>
using namespace std;
struct Node{
  int data;
  Node* next;
};
void creatList(Node* head,int* value,int n);//创建链表
void deleteList(Node *head);//删除整个链表
void insertNode(Node *head,int pos,int value);//在指定位置i插入e
void printList(Node *head);//显示链表元素
void removeNode(Node *head,int pos);//删除第i个数据
int main()
{   //freopen("d:\\a.txt",  "r", stdin);//提交时,要注释掉本行
    int t;
    int n,m,e,*value;
    cin>>t;
    while(t--){
        cin>>n;
        value=new int[n];  //读入链表各节点数据
        for(int i=0;i<n;i++)
            cin>>value[i];
        Node* head=new Node{0,NULL};  //创建链表
        creatList(head,value,n);
        printList(head);
        cin>>n;    //插入操作
        for(int i=0;i<n;i++){
            cin>>m>>e;
            insertNode(head,m,e);
        }
        cin>>n;  //删除操作
        for(int i=0;i<n;i++){
            cin>>m;
            removeNode(head,m);
        }
        delete[] value;
        deleteList(head);   //释放链表
    }
       return 0;
}
void creatList(Node* head,int* value,int n){
 Node *p=head;
 for(int i=0;i<n;i++){
     Node* s=new Node{value[i],NULL};
     s->next=p->next;
     p->next=s;
     p=s;
     head->data++;
  }
}
void deleteList(Node *head){
  Node* p=head;
  while(head->next){
    p=head->next;
    head->next=p->next;
    delete p;
 }
  delete head;
}
void insertNode(Node *head,int pos,int value){
  Node* p=head;
  if(pos<1 || pos>head->data+1){
    cout<<"error"<<endl;
    return;
  }
  while(--pos)  //移到第pos-1个结点
      p=p->next;
  Node* s=new Node{value,NULL};
  s->next=p->next;
  p->next=s;
  head->data++;
  printList(head);
}
void removeNode(Node *head,int pos){
  Node* p=head;
  if(pos<1 || pos>head->data){
    cout<<"error"<<endl;
    return;
  }
  while(--pos) //移到第pos-1个结点
        p=p->next;
  Node* q=p->next;
  p->next=q->next;
  delete q;
  head->data--;
  printList(head);
}
void printList(Node *head){
  Node* p=head->next;
  if(p){
    while(p->next){
      cout<<p->data<<" ";
      p=p->next;
    }
    cout<<p->data<<endl;
  }
}

6、买彩游戏

//好像当时是不会写,orz

 7、链表原地反转

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node
{
    int data;
    Node *next;
    int flag;

public:
    Node()
    {
        next = NULL;
    }
    void init()
    {
        cin >> data;
    }
};
void set_(Node *&head, int n)
{
    Node *p1, *p2, *p3;
    p1 = head->next;
    p2 = p1->next;
    p1->next = NULL;
    if (n <= 1)
        return;
    p3 = p2->next;
    for (int i = 1; i < n; i++)
    {
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        if (p3 == NULL)
            break;
        else
            p3 = p3->next;
    }
    head->next = p1;
}
void display(Node *p)
{
    for (;p->next->next;)
    {
        cout << p->next->data << " ";
        p = p->next;
    }
    cout << p->next->data << " " << endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        Node *head = new Node;
        int i, n;
        cin >> n;
        Node *p1 = head;
        for (i = 0; i < n; i++)
        {
            p1->next = new Node;
            p1->next->init();
            p1 = p1->next;
        }
        set_(head, n);
        display(head);
    }
}
//老师的代码
#include<iostream>
#include<string>
using namespace std;

class student{
    string name;
    string num;
    string college;
    string major;
    string sex;
    string adress;
    string tele;

    public:
        void init(string name1,string num1,string college1,string major1,string sex1,string adress1,string tele1)
        {
                name=name1;
                num=num1;
                college=college1;
                major=major1;
                sex=sex1;
                adress=adress1;
                tele=tele1;
        }
        void print()
        {
            cout<<name<<" "<<num<<" "<<college<<" "<<major<<" "<<sex<<" "<<adress<<" "<<tele<<endl;
        }
};
int main()
{   
    int t;
    cin>>t;
    string a,b,c,d,e,f,g;
    while(t--)
    {
        cin>>a>>b>>c>>d>>e>>f>>g;
        student stu;
        stu.init(a,b,c,d,e,f,g);
        stu.print();
    }
    return 0;
}

/
数据的初始化也可用用构造函数完成
#include<iostream>
#include<string>
using namespace std;

class student{
    string name;
    string num;
    string college;
    string major;
    string sex;
    string adress;
    string tele;

    public:
        student(string name1,string num1,string college1,string major1,string sex1,string adress1,string tele1)
        {
                name=name1;
                num=num1;
                college=college1;
                major=major1;
                sex=sex1;
                adress=adress1;
                tele=tele1;
        }
        void print()
        {
            cout<<name<<" "<<num<<" "<<college<<" "<<major<<" "<<sex<<" "<<adress<<" "<<tele<<endl;
        }
};
int main()
{   
    int t;
    cin>>t;
    string a,b,c,d,e,f,g;
    while(t--)
    {
        cin>>a>>b>>c>>d>>e>>f>>g;
        student stu(a,b,c,d,e,f,g);
        stu.print();
    }
    return 0;
}

===============================================
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

class CAccount
{
    long account;
    string name;
    float balance;

  public:
    CAccount(long account1,string name1,float balance1){
       account=account1;
       name=name1;
       balance=balance1;
    }

     void deposit(float in)
    {
        balance += in;
        cout<<"saving ok!"<<endl;
     }

    void withdraw(float out)
    {
        if(out>balance)
           cout<<"sorry! over limit!"<<endl;
        else
        {
           balance -= out;
           cout<<"withdraw ok!"<<endl;
         }
     }

     void check()
     { cout<<name<<"'s balance is "<<balance<<endl;}
};

int main()
{   //freopen("d:\\a.txt","r",stdin);   //上传要注释掉这行
    int i;
    long a;
    string n;
    float b,temp;
    for(i=0;i<2;i++)
    {
        cin>>a>>n>>b;
        CAccount account(a,n,b);
        account.check();

        cin>>temp;
        account.deposit(temp);
        account.check();

        cin>>temp;
        account.withdraw(temp);
        account.check();
     }
     return 0;
}


/
第二种:使用动态对象数组
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

class CAccount
{
    long account;
    string name;
    float balance;

  public:
    CAccount(){}
    void SetAccount(long account1,string name1,float balance1){
       account=account1;
       name=name1;
       balance=balance1;
    }

     void deposit(float in)
    {
        balance += in;
        cout<<"saving ok!"<<endl;
     }

    void withdraw(float out)
    {
        if(out>balance)
           cout<<"sorry! over limit!"<<endl;
        else
        {
           balance -= out;
           cout<<"withdraw ok!"<<endl;
         }
     }

     void check()
     { cout<<name<<"'s balance is "<<balance<<endl;}
};

int main()
{   //freopen("d:\\a.txt","r",stdin);   //上传要注释掉这行
    int i;
    CAccount *account=new CAccount[2];
    long a;
    string n;
    float b,temp;
    for(i=0;i<2;i++)
    {
        cin>>a>>n>>b;
        account[i].SetAccount(a,n,b);
        account[i].check();

        cin>>temp;
        account[i].deposit(temp);
        account[i].check();

        cin>>temp;
        account[i].withdraw(temp);
        account[i].check();
     }
     delete[] account;
     return 0;
}
=========================================
///使用string数组保存音像状态
#include<iostream>
using namespace std;
class Audio {
        int type;
        string  name;
        int price;
        int status;
        string type_p[5]={"","黑胶片","CD","VCD","DVD"};
        string status_p[2]={"未出租","已出租"};
    public:
        Audio(int t,string n,int p,int s){
           type=t;
           name=n;
           price=p;
           status=s;
        }
        void Print(){
          cout<<type_p[type]<<"["<<name<<"]"<<status_p[status]<<endl;
        }
        void Fee(int day){
           if(status==0)
            cout<<"未产生租金"<<endl;
           else
            cout<<"当前租金为"<<day*price<<endl;
        }
};

int main()
{
    int t;
    int type;
    string  name;
    int price;
    int status;
    int n;

    cin>>t;
    while(t--)
    {
        cin>>type>>name>>price>>status;
        Audio a(type,name,price,status);

        cin>>n;
        a.Print();
        if(n!=0)
            a.Fee(n);
    }
    return 0;
}


//
///使用二维字符数组
#include<iostream>
#include<cstring>
using namespace std;
class Audio {
    private:
        int type;
        char name[10];
        int price;
        int status;
        char *type_p[5]={"","黑胶片","CD","VCD","DVD"};
        char *status_p[2]={"未出租","已出租"};
    public:
        Audio(int t,char n[],int p,int s){
           type=t;
           strcpy(name,n);
           price=p;
           status=s;
        }
        void Print(){
          cout<<type_p[type]<<"["<<name<<"]"<<status_p[status]<<endl;
        }
        void Fee(int day){
           if(status==0)
               cout<<"未产生租金"<<endl;
           else
              cout<<"当前租金为"<<day*price<<endl;
        }
};

int main()
{
    int t;
    int type;
    char name[10];
    int price;
    int status;
    int n;

    cin>>t;
    while(t--)
    {
        cin>>type>>name>>price>>status;
        Audio a(type,name,price,status);

        cin>>n;
        a.Print();
        if(n!=0)
            a.Fee(n);
    }
    return 0;
}
===================================================
#include <iostream>
using namespace std;

int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
string months[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};

class Date{
   int y;
   int m;
   int d;
public:
   Date(int a,int b,int c):y(a),m(b),d(c){}
   int gety(){return y;}
   int getm(){return m;}
   int getd(){return d;}
};

int deal(Date& dd){
   int y=dd.gety();
   int m=dd.getm();
   int d=dd.getd();
   int count;
   if(y%400==0 || (y%4==0 && y%100!=0))
        days[1]=29;
   count=days[m-1]-d;
   for(int i=m;i<12;i++)
     count+=days[i];
   return count;
}

void print(Date& dd){
     int y=dd.gety();
     int m=dd.getm();
     int d=dd.getd();
     cout<<"This month is "<<months[m-1];
     cout<<" and next month is "<<months[m%12]<<endl;
     cout<<"There are "<<deal(dd)<<" days to the end of the year"<<endl;
}

int main(){
  int t,x,y,z;
  cin>>t;
  while(t--){
    cin>>x>>y>>z;
    Date d(x,y,z);
    print(d);
  }
}


也可以直接写
#include <iostream>
using namespace std;

class Date{
   int y;
   int m;
   int d;
   int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
   string months[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
   int count;
public:
   Date(int a,int b,int c):y(a),m(b),d(c){}

   int deal(){
     if(y%400==0 || (y%4==0 && y%100!=0))
        days[1]=29;
     count=days[m-1]-d;
     for(int i=m;i<12;i++)
        count+=days[i];
   }

   void print(){
     cout<<"This month is "<<months[m-1];
     cout<<" and next month is "<<months[m%12]<<endl;
     cout<<"There are "<<count<<" days to the end of the year"<<endl;
   }
};

int main(){
  int t,x,y,z;
  cin>>t;
  while(t--){
    cin>>x>>y>>z;
    Date d(x,y,z);
    d.deal();
    d.print();
  }
}
==========================================
#include<stdio.h>
#include <iostream>
using namespace std;

struct Node{
  int data;
  Node* next;
};

void creatList(Node* head,int* value,int n);//创建链表
void deleteList(Node *head);//删除整个链表
void insertNode(Node *head,int pos,int value);//在指定位置i插入e
void printList(Node *head);//显示链表元素
void removeNode(Node *head,int pos);//删除第i个数据

int main()
{   //freopen("d:\\a.txt",  "r", stdin);//提交时,要注释掉本行
    int t;
    int n,m,e,*value;
    cin>>t;
    while(t--){
        cin>>n;
        value=new int[n];  //读入链表各节点数据
        for(int i=0;i<n;i++)
            cin>>value[i];

        Node* head=new Node{0,NULL};  //创建链表
        creatList(head,value,n);
        printList(head);

        cin>>n;    //插入操作
        for(int i=0;i<n;i++){
            cin>>m>>e;
            insertNode(head,m,e);
        }

        cin>>n;  //删除操作
        for(int i=0;i<n;i++){
            cin>>m;
            removeNode(head,m);
        }
        delete[] value;
        deleteList(head);   //释放链表
    }
        return 0;
}


void creatList(Node* head,int* value,int n){
 Node *p=head;
 for(int i=0;i<n;i++){
     Node* s=new Node{value[i],NULL};
     s->next=p->next;
     p->next=s;
     p=s;
     head->data++;
  }
}

void deleteList(Node *head){
  Node* p=head;
  while(head->next){
    p=head->next;
    head->next=p->next;
    delete p;
 }
  delete head;
}

void insertNode(Node *head,int pos,int value){
  Node* p=head;
  if(pos<1 || pos>head->data+1){
    cout<<"error"<<endl;
    return;
  }

  while(--pos)  //移到第pos-1个结点
      p=p->next;
  Node* s=new Node{value,NULL};
  s->next=p->next;
  p->next=s;

  head->data++;

  printList(head);
}

void removeNode(Node *head,int pos){
  Node* p=head;
  if(pos<1 || pos>head->data){
    cout<<"error"<<endl;
    return;
  }

  while(--pos) //移到第pos-1个结点
        p=p->next;
  Node* q=p->next;
  p->next=q->next;
  delete q;

  head->data--;

  printList(head);
}

void printList(Node *head){
  Node* p=head->next;
  if(p){
    while(p->next){
      cout<<p->data<<" ";
      p=p->next;
    }
    cout<<p->data<<endl;
  }
}
====================================
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

class Lottery
{
        int code[7];
    public:
        Lottery(int*);
        int matchOne(int*);
        void matchTotal(int,int* [],string);
};

Lottery::Lottery(int *c)//设置摇奖号码
{
    for(int i=0; i<7; ++i)
        code[i]=c[i];
}

int Lottery::matchOne(int *p) //判断一注彩票是否中奖,返回值为奖项类型
{
    int ans=0,price=0; //ans记录号码匹配数目
    for(int i=0; i<7; ++i){
        if(code[i]==p[i])
            ++ans;
    }
    if(ans==7)
       price=1;  //一等奖
    else if(ans==5 || ans==6)
       price=2;  //二等奖
    else if(ans>=2)
       price=3;  //三等奖
    else
       price=0;  //未中奖
    return price;
}

void Lottery::matchTotal(int t,int* p[],string name)//判断t注彩票是否中奖
{
    int a[4]={0,0,0,0}; //记录各个奖项中奖数目
    string b[4]={"","注一等奖!","注二等奖!","注三等奖!"};//用于输出结果

    for(int i=0; i<t; ++i){
           a[matchOne(p[i])]++;
    }

    if(a[0]==t)  //未中奖
        cout<<"加油!继续"<<endl;
    else{
       for(int i=1; i<4; ++i){
            if(a[i])
               cout<<"恭喜"<<name<<"中了"<<a[i]<<b[i]<<endl;
       }
   }
}

int main()
{  // freopen("d:\\a.txt",  "r", stdin);
    int n,i,j;
    string name;
    cin>>n>>name;
    int c[7];
    int** p=new int*[n];  //二维数组存放用户的n组彩票信息

    for(i=0; i<n; i++){
        p[i]=new int[7];  //存放用户的一组彩票信息
        for(j=0;j<7;j++)
              cin>>p[i][j];  //输入一组彩票的各个号码
    }

    for(j=0;j<7;j++)  //输入开奖号码
          cin>>c[j];

    Lottery L(c);
    L.matchTotal(n,p,name);

   for(int i=0;i<n;i++)
      delete[] p[i];
   delete[] p;

   return 0;
}
===============================================
简单的写法,没有类,没有函数
#include<iostream>
using namespace std;

struct node{
  int e=0;
  node* next=NULL;
};

int main(){
  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    node* head=new node;  //创建链表
    node* p=head;
    for(int i=0;i<n;i++)
    {  int e;
       cin>>e;
       node* s=new node{e,NULL};
       p->next=s;
       p=s;
    }

    node* k1,*k2=NULL; //k2指向当前结点,k1指向上一个处理完的结点
    p=head->next;

    if(p->next!=NULL){ //若链表有两个以上的结点
       while(p){
         k1=p;
         p=p->next;
         k1->next=k2;
         k2=k1;
       }
       head->next=k1;
    }

    p=head->next;  //输出
    while(p){
      cout<<p->e<<" ";
      p=p->next;
    }
    cout<<endl;
 }
 return 0;
}

///
使用类实现
#include <iostream>
#include <stdio.h>
using namespace std;

struct Node{
  int e;
  Node* next;
};

class List{
  Node* head;
public:
  List(){
    head=new Node{0,NULL};
  }

  void createList(){
   int n,e;
   Node *p=head;
   cin>>n;
   for(int i=1;i<=n;i++){
     cin>>e;
     Node *s=new Node{e,NULL};
     p->next=s;
     p=s;
     head->e++;
   }
 }

  void showList(){
  if(head->e==0)
       return;
  Node* p=head->next;
  while(p){
     cout<<p->e<<" ";
     p=p->next;
  }
  cout<<endl;
  }

  void reverseList(){
//将head单链表中所有结点按相反次序链接
   Node *p, *q,*t;

   t=head->next;  //t指向待处理节点的前一个节点
   if(t)  //若链表不为空
      p=t->next;  //p指向下一个结点

   if(!p || !t) //若只包含头结点或只有一个结点,则不需要逆序
       return;
   else
      {t->next=NULL;q=t;}  //第一个节点单独处理

   while(p)
   {  q=p;                    //q指向当前待处理结点
      p=p->next;          //p指向下一个结点
      q->next=t;          //t指向逆转时q的后续结点
      t=q;
    }
   head->next=t; //头结点指向t
  }

  void destroyList(){
    Node *p;
    while(head->next){
        p=head->next;
        head->next=p->next;
        delete p;
    }
    delete head;
  }
};


int main()
{   //freopen("d:\\a.txt",  "r", stdin);
    int t,n;
    cin>>t;
    while(t--){
        List list;
        list.createList();
        list.reverseList();
        list.showList();
        list.destroyList();
    }
    return 0;
}


///不使用类的方式
1、带头结点
#include <iostream>
#include <stdio.h>
using namespace std;

struct Node{
  int e;
  Node* next;
};

void createList(Node* head){
   int n,e;
   Node *p=head;
   cin>>n;
   for(int i=1;i<=n;i++){
     cin>>e;
     Node *s=new Node{e,NULL};
     p->next=s;
     p=s;
     head->e++;
   }
}

void showList(Node* head){
  if(head->e==0)
     return;
  Node* p=head->next;
  while(p){
    cout<<p->e<<" ";
    p=p->next;
  }
  cout<<endl;
}

void reverseList(Node* head){
//将head单链表中所有结点按相反次序链接
   Node *p, *q,*t;

   t=head->next;  //t指向待处理节点的前一个节点
   if(t)  //若链表不为空
      p=t->next;  //p指向下一个结点

   if(!p || !t) //若只包含头结点或只有一个结点,则不需要逆序
       return;
   else
      {t->next=NULL;q=t;}  //第一个节点单独处理

   while(p)
   {  q=p;                    //q指向当前待处理结点
      p=p->next;          //p指向下一个结点
      q->next=t;          //t指向逆转时q的后续结点
      t=q;
    }
   head->next=t; //头结点指向t
}

void destroyList(Node* head){
    Node *p;
    while(head->next){
        p=head->next;
        head->next=p->next;
        delete p;
    }
    delete head;
}

int main()
{   
    int t,n;
    cin>>t;
    while(t--){
        Node* head=new Node{0,NULL};
        createList(head);
        reverseList(head);
        showList(head);
        destroyList(head);
    }
    return 0;
}


/
2、不带头结点,可把头结点head当做t用,指向当前待处理节点的前一个节点。注意,在函数中想改变实参指针的值,要用引用方式
#include <iostream>
#include <stdio.h>
using namespace std;

struct Node{
  int e;
  Node* next;
};

void createList(Node* &head){
   int n,e;
   Node *p;
   cin>>n;
   for(int i=1;i<=n;i++){
     cin>>e;
     if(i==1){
        head=new Node{e,NULL};
        p=head;
     }
     else{
        Node *s=new Node{e,NULL};
        p->next=s;
        p=s;
     }
   }
}

void showList(Node* head){
  Node* p=head;
  while(p){
    cout<<p->e<<" ";
    p=p->next;
  }
  cout<<endl;
}

void reverseList(Node* &head){
//将head单链表中所有结点按相反次序链接
   Node *p, *q;

   if(!head->next)
    return;

   p=head->next;
   head->next=NULL;

   while(p->next)
   {  q=p;               //p指向当前待处理结点
      p=p->next;      //head指向前一个已经逆序的结点
      q->next=head;   //q为临时结点
      head=q;
    }
   p->next=head;
   head=p;
}

void destroyList(Node* head){
    Node *p;
    while(head){
        p=head;
        head=head->next;
        delete p;
    }
}

int main()
{   //freopen("d:\\a.txt",  "r", stdin);
    int t,n;
    Node *head;
    cin>>t;
    while(t--){
        createList(head);
        reverseList(head);
        showList(head);
        destroyList(head);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值