DS(vector、list实现)

来源:互联网 发布:程序员创业 方向选择 编辑:程序博客网 时间:2024/06/05 06:12
 

1.vector实现

注:部分代码没有完成,但是框架已经建起来了。

头文件中的代码:

#ifndef THREE_H_H

#define THREE_H_H

 

#include <iostream>

#include <cstring>

using namespace std;

typedef int T;

class Vector

{

       T  *p;//指向首地址的指针

       unsigned int max;//向量长度

       unsigned int sz;//向量个数

       void expand();//扩充函数,在此函数中开辟新的空间,并已处理好p,max,sz.

public:

       Vector(int max=0):max(max),sz(0),p(0){if(max>0) p=new T[max];}

       //实现增的功能

       int headinsert(const T &tttt);//在头表中插入数据tttt,然后返回插入的数据tttt的值。

       int tailinsert(const T &tttt);//在尾表中插入数据tttt,然后返回插入的数据tttt的值。

       int insert(const int &pppp,const T &tttt);//在位置pos中插入数据tttt,然后返回插入的位置,如果位置错误,返回-1.

       //实现删的功能

       int headdelete();//删除头元素,返回删除的那个元素的值。

       int taildelete();//删除尾元素,返回删除的那个元素的值。

       int deletepos(int pos);//删除指定位置元素,返回删除的那个元素的值。若输入的位置越界,抛越界异常

       int deleteone(const T &tttt);//删除指定元素的第一个,返回删除的那个元素的位置。

       int deleteall(const T &tttt);//删除指定元素的所有,返回删除的那个元素的首位置。

       //实现查找功能

      

       ~Vector() {if(p) delete []p;}

       void show();

};

#endif

Cpp中的代码:

#include "33.h"

void Vector::expand()//扩充函数,在此函数中开辟新的空间,并已处理好p,max,sz.

{

       max+=1;

       T *pnew=new T[max];

       int t=sz;

       for (int i=sz-1;i>=0;i--)

       {

              pnew[i]=p[i];

       }

       if(p) delete []p;

       p=pnew;

}

void Vector::show()

{

       cout<<"in show p[0]="<<p[0]<<endl;

       cout<<"in show p="<<p<<endl;

       for (int i=0;i<sz;i++)

       {

              cout<<p[i]<<' ';

       }

       cout<<endl;

}

int Vector::headinsert(const T &tttt)

{

       if(sz==max)

       {expand();}

       for (int i=sz-1;i>=0;i--)

       {     p[i+1]=p[i];  }

       p[0]=tttt;

       sz++;

       return tttt;

}

int Vector::tailinsert(const T &tttt)

{

       if(sz==max)

       {expand();}

       p[sz]=tttt;

       sz++;

       return tttt;

}

int Vector::insert(const int &pppp,const T &tttt)

{

       if (pppp<0 || pppp>=sz) return -1;

       if(pppp==0) {headinsert(tttt);return 0;}

       if(pppp==sz) {tailinsert(tttt);return sz;}

       if(max==sz) expand();

       for (int i=sz-1;i>=pppp;i--)

             p[i+1]=p[i];

       p[pppp]=tttt;

       sz++;

       return pppp;

}

int Vector::headdelete()//删除头元素,返回删除的那个元素的值。

{

       if (sz==0) return 0;

       T v=*p;

       T *q=p;

       p=p+1;

       cout<<"in headdelete p[0]="<<p[0]<<endl;

       cout<<"in headdelete p="<<p<<endl;

       delete q;

       max-=1;

       sz--;

       return v;

}

int Vector::taildelete()throw(const char*)//删除尾元素,返回删除的那个元素的值。

{

       if (sz==0) throw "元素是空的";

       else

       {

       }

}

//int deletepos(int pos);//删除指定位置元素,返回删除的那个元素的值。若输入的位置越界,抛越界异常

//int deleteone(const T &tttt);//删除指定元素的第一个,返回删除的那个元素的位置。

//int deleteall(const T &tttt);//删除指定元素的所有,返回删除的那个元素的首位置。

int main()

{

       Vector v1;

       v1.headinsert(4);

       v1.headinsert(9);

       v1.headinsert(1);

       v1.tailinsert(11);

       v1.insert(-1,8);

       v1.insert(0,0);

       v1.insert(1,3);

       v1.insert(3,57);

       v1.insert(2,7);

       v1.show();

       v1.headdelete();

       v1.show();

       //v1.add(3);

       return 0;

}

 

 

2.链表实现

#include <iostream>

using namespace std;

typedef int T;

class CList

{

       struct Node

       {

              T data;

              Node *next;

              Node(int x=0):data(x),next(0) {}

       };

       Node *head;//指向头结点的指针

       int sz;//结点数目大小

       public:

              CList():head(0),sz(0) {}

              bool headinsert(const T &tttt);

              bool tailinsert(const T &tttt);

              bool insert(int pos,const T &tttt);

              ~CList(){if(head) delete []head;}

              int getsize(){return sz;}

              CList(const CList & cccc);//copy构造函数

              CList &operator= (const CList &cccc);//=重载

              //friend ostream &operator<<(ostream &osm,const CList &cccc );

              bool setvalue(int pos,const T value);//修改位置为pos的值为value

              bool deletehead();//删除头元素

              bool deletetail();//删除尾元素

              bool deletepos(int pos);//删除位置为pos的结点

              T querypos(int pos);//返回位置是pos的结点的值。

              void show();

             

};

T CList::querypos(int pos)

{

       if(pos<0 || pos>=sz)  throw "数组下标越界";

       if(head==0) throw "链表是空的";

       Node *p=head;

       for(int i=0;i<pos;i++)

              p=p->next;

       return p->data;

}

bool CList::deletepos(int pos)

{

       if(pos<0 || pos>=sz) return false;

       if(pos==0) deletehead();

       if(pos==sz-1) deletetail();

       Node *p=head;

       for (int i=0;i<pos-1;i++)

       {p=p->next;}

       Node *q=p->next;

       p->next=p->next->next;

       delete q;

       sz--;

       return true;

}

bool CList::deletetail()

{

       if(head==0) return false;

       Node *p=head;

       for (int i=0;i<sz-2;i++)

       {p=p->next;}

       Node *q=p->next;

       p->next=0;

       delete q;

       sz--;

       return true;

}

bool CList::deletehead()

{

       if(head==0) return false;

       Node *p=head;

       head=head->next;

       delete p;

       sz--;

       return true;

}

bool CList::setvalue(int pos,const T value)

{

       if(pos==0 || pos>=sz) return false;

       Node *p=head;

       while(pos--)

              p=p->next;

       p->data=value;

       return true;

}

CList &CList::operator=(const CList &cccc)

{

       if(this==&cccc) return *this;

       if(cccc.head==0) {sz=0;delete []head;head=0;return *this;}

       sz=0;

       delete []head;

       for (Node* p=cccc.head;p->next!=0;p=p->next)

       {tailinsert(p->data);}

       tailinsert(p->data);

       return *this;

}

CList::CList(const CList & cccc)

{

       head=0;sz=0;

       for (Node* p=cccc.head;p->next!=0;p=p->next)

       {tailinsert(p->data);}

       tailinsert(p->data);

}

bool CList::insert(int pos,const T &tttt)//向位置pos插入数据,成功返回true

{

       if(pos<0 || pos>=sz) return false;

       if(pos==0) {headinsert(tttt);return true;}

       Node *p=head;

       pos-=1;

       while(pos--)

              p=p->next;

       Node *pnew=new Node(tttt);

       pnew->next=p->next;

       p->next=pnew;

       sz++;

       return true;

}

bool CList::tailinsert(const T &tttt)//向尾插入数据,成功返回true

{

       Node *pnew=new Node(tttt);

       if(head==0) {head=pnew;sz++;return true;}

       Node *p=head;

       for (;p->next!=0;p=p->next);

       p->next=pnew;

       sz++;

       return true;

}

void CList::show()

{

       if(head==0) return;

       for (Node *p=head;p->next!=0;p=p->next)

          cout<<p->data<<' ';

       cout<<p->data<<endl;

}

bool CList::headinsert(const T &tttt)//向头中插入数据,成功返回true

{

       Node *pnew=new Node(tttt);

       pnew->next=head;

       head=pnew;

       sz++;

       return true;

}

int main()

{

       CList list1;

       list1.headinsert(31);list1.headinsert(98);list1.headinsert(56);list1.headinsert(61);

       list1.tailinsert(35);list1.tailinsert(57);list1.insert(-1,34);list1.insert(3,33);

       list1.setvalue(3,123);list1.setvalue(34,34);

       list1.show();

       cout<<list1.getsize()<<endl;

       //list1.deletehead();list1.deletetail();

       list1.deletepos(7);list1.deletepos(3);

       list1.show();

       cout<<list1.querypos(4)<<endl;

       //cout<<list1.querypos(90)<<endl;

       cout<<"..................."<<endl;

       cout<<list1.getsize()<<endl;

       CList list2;

       list2.show();

       cout<<list2.getsize()<<endl;

       CList list3;

       list3=list2;

       list3.show();

       cout<<list3.getsize()<<endl;

       return 0;

}