2. DataStructure Cp2 Doubly-Linked Node

2. DataStructure Cp2 Doubly-Linked Node

Function move can’t be compiled through if write in outside

注释提到了很多问题 随后应当分析一下,比如i,i-1

shape.h
#include"stdafx.h"
#include"targetver.h"
#ifndef list_h
#define list_h
#include<iostream>
using namespace std;

#pragma once

template <class type>
class list
{
public://virtual function shall be put into public

    virtual void clear() = 0;
    virtual int length()const = 0;
    virtual void insert(int i,const type&x) = 0;
    virtual void remove(int i) = 0;
    virtual int search(const type&x)const = 0;//一开始以为是用来调用结构中的数字
    virtual type visit(int i)const = 0;//加在后面不会修改数据
    virtual void traverse()const = 0;


    virtual ~list();
};

class OutOfBound {};
class IllegalSize {};



template <class type>
class linklist :public list<type>//<type>
{
private:

    //part I
    struct node//这个struct实际上是个class,注意构造函数
    {
        type data;
        node*next,*prev;
        node() :next(NULL), prev(NULL) {};
        node(const type& x, node*m, node*n)
        {
            data = x; next = m; prev = n;
        }
        ~node() {};//我就说不用析构函数
    };

    //partII
    node*head, *tail;
    int currentlength;

    //partIII 不写成内置类编译怎么都不通过
    node* move(int i)const
    {
        if (i<0 || i>currentlength)
            throw OutOfBound(); //at first,this hacebeen ignorant

        node*tmp = head;
        while (i>0)
        {
            tmp = tmp->next;
            i--;
        }//应该看看是不是移动到第i个节点了,有个问题,head是第0还是第一?
        return tmp;
    }

public:

    linklist();
    ~linklist();

      void clear() ;
      int length()const ;
      void insert(int i, const type&x) ;
      void remove(int i) ;
      int search(const type&x)const ;//一开始以为是用来调用结构中的数字
      type visit(int i)const ;//加在后面不会修改数据
      void traverse()const ;

};



#endif

main.cpp
#include "stdafx.h"
#include "list.h"

template<class type>//不知道写什么?为head tail申请空间,随后再连一起
linklist<type>::linklist()
{
    head = new node;
    tail = new node;
    head->next = tail;
    tail->prev = head;
    currentlength = 0;//忘写了
}

template<class type>
linklist<type>::~linklist()
{
    clear();
    delete head;
    delete tail;
}





template<class type>
 void linklist<type>::clear()
{
     node*tmp = head->next;
     head->next = tail;
     tail->prev = head;
     while (tmp!= tail)//跟课本上不太一样
     {
         tmp = tmp->next;
         delete tmp->prev;
     }
     currentlength = 0;//注意清零,忘写了
}

 template<class type>
 int linklist<type>::length() const
 {
     return currentlength;
 }

 template<class type>//插到i前面还是i后面? 是i和i-1
 void linklist<type>::insert(int i, const type & x)
 {
     node*p = move(i);
     node*tmp = new node(x, p, p->prev);
     p->prev->next = tmp;
     p->prev = tmp;

     currentlength++;//注意清零,忘写了
 }

 template<class type>
 void linklist<type>::remove(int i)
 {
     char*tmp = move(i);
     tmp->next->prev = tmp->prev;
     tmp->prev->next = tmp->next;//放上下个结构体的地址事实上就是下个结构体的指针

     delete tmp;//曾经在这一块有过疑问,事实上删除指针会自动启用对应的析构函数

     currentlength--;
 }

 template<class type>
 int linklist<type>::search(const type & x) const
 {
     node*tmp = head;
     int i = 0;

     while (tmp != tail && tmp->data != x)
     {
         tmp = tmp->next;
         i++;
     }

     if (tmp == tail)
     {
         cout << "none exit\t\n";
         return -1;
     }
     else return i;//same question,should i start from 0?

     return 0;
 }

 template<class type>
 type linklist<type>::visit(int i) const
 {
     if (i<0 || i>currentlength)throw OutOfBound();//at first,this hacebeen ignorant

     node*tmp=head;
     int n = 0;
     while (n < i)
     {
         tmp = tmp->next;
         n++;
     }

     return tmp->data;//我也不知道应该是前一个还是后一个
 }

 template<class type>
 void linklist<type>::traverse() const
 {
     node*tmp = head-next;
     while (node != tail)
     {
         cout << tmp->data << endl;
         tmp = tmp->next;
     }

 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我改一下代码#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 4 #define OK 1 typedef struct { int no; char name[20]; int DataStructure; int C; int SUM; }student; typedef struct{ student STU[N]; int length; }STUDENT; int input(STUDENT *stu) { int i; for(i=0;i<N;i++) { system("cls"); printf("请输入学生信息:\n"); printf("请输入第%d个学生的学号:",i+1); scanf("%d",&(stu -> STU[i].no)); printf("请输入第%d个学生的姓名:",i+1); scanf("%s",&(stu->STU[i].name)); printf("请输入第%d个学生的DataStructure成绩",i+1); scanf("%d",&(stu->STU[i].DataStructure)); printf("请输入第%d个学生的C语言成绩",i+1); scanf("%d",&(stu->STU[i].C)); stu->STU[i].SUM = stu->STU[i].DataStructure + stu->STU[i].C; stu->length = i+1; } return OK; } void count(STUDENT *stu){ int i; for(i=0;i<stu->length;i++) { printf("第%d名学生的学号:%d\n",i+1,stu->STU[i].no); printf("第%d名学生的姓名:%s\n",i+1,stu->STU[i].name); printf("第%d名学生的总成绩:%d\n",i+1,stu->STU[i].SUM); } } int main(){ int n; int i; char a; STUDENT *stu ; stu = (STUDENT *)malloc(sizeof(STUDENT)); while(n!=7){ system("cls"); printf("学生成绩管理系统:\n"); printf("****(1):信息输入(INPUT)***************************\n"); printf("****(2):总分统计(COUNT)***************************\n"); printf("****(3):按DataStructure项排序(SortDataStructure)**\n"); printf("****(4):按C项排序(SortC)**************************\n"); printf("****(5):按SUM项排序(SortSUM)**********************\n"); printf("****(6):输入C成绩,查找该成绩位置*******************\n"); printf("****(7):退出****************************************\n"); printf("****请选择输入(1-7): *************************\n"); scanf("%d",&n); switch(n){ case 1: i = input(stu); break; case 2: count(stu);break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; default : printf("输入不正确,请重新输入:\n"); scanf("%d",&n); break; } printf("是否继续Y/N"); scanf("%c",&a); if(a==N) break; } return 0; }
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值