正文

面向对象c++数据结构描述==>>线性表的操作(类模版的应用)2005-09-23 23:46:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/5188.html

分享到:

#ifndef list_H
#define list_H
#include"List_Node.h"
template<class T>
class list:public listnode<T>
{
 private:
  listnode<T>* head;
  listnode<T>* tail;
  listnode<T>* current;
  static int size;
 public:
  list(T b,listnode<T>* ptrnext):listnode<T>(b,ptrnext)
  {
   head=tail=current=0;
  }
  ~list();
  bool insertnode(T num1,int);
  bool deletenode(T num2);
  int searchnode(T num3);
  void showlist();
  int getsize();
  int menu1();
};
template <class T>
int list<T>::size=0;
#endif
#ifndef List_Node_H
#define List_Node_H
template <class T>
class listnode
{
private:
 T value;
 listnode<T>* next;
public:
 listnode(T a=0,listnode<T>* ptrnext=0)
 {
  value=a;
  next=ptrnext;
 }
 ~listnode()
 {}
 T getnode()
 {
  return value;
 }
 listnode<T>* getnext()
 {
  return next;
 }
 void setnext(listnode<T>*  &othernode)
 {
  next=othernode;
 }
};
#endif
#ifndef listmean_H
#define listmean_H
#include"list.h"
#include<iostream>
#include<cstdlib>
#include<assert.h>
using namespace std;
template<class T>
list<T>::~list()
{
 listnode<T>* ptr=0;
 current=head;
 while(current)
 {
     ptr=current;
  current=getnext();
  delete ptr;
 }
}
template<class T>
bool list<T>::insertnode(T num1,int position)
{
 if(position==0)
 {
  listnode<T>* newptr=new listnode<T>(num1,head);
  assert(newptr);
  head=newptr;
  size++;
  return true;
 }
     else
  {
   if(position==1)
   {
    if(head)
    {
        listnode<T>* special=new listnode<T>(num1,head);
     head=special;
     size++;
     return true;
    }
    else
     return false;
   }
   else
   {

         if(position>1&&position<=size)
   {
   int i=1;
   listnode<T>* p1;
   listnode<T>* p2;
   p1=head;
   p2=p1->getnext();
   while(p2&&i<position-1)
   {
    p1=p2;
    p2=p2->getnext();
    i++;
   }
   listnode<T>* newptr2=new listnode<T>(num1);
   assert(newptr2);
      p1->setnext(newptr2);
   newptr2->setnext(p2);
   size++;
   return true;
   }
   else
     return false;
   }
  }
}
template<class T>
int list<T>::searchnode(T num3)
{
 int i=1;
 current=head;
 while(current&&current->getnode()!=num3)
 {
   current=current->getnext();
   i++;
 }
 if(!current)
 return 0;
 else
  return i;
}
template<class T>
bool list<T>::deletenode(T num2)
{
 int i=searchnode(num2);
 if(i)
 {
  listnode<T>* oldptr=head;
  current=head->getnext();
  if(i==1)
  {
   oldptr=head->getnext();
   delete head;
   head=oldptr;
   size--;
   return true;
  }
  else
  {
  int j=1;
  while(j<i)
  {
   oldptr=current;
   current=current->getnext();
  }
  oldptr=current->getnext();
  delete current;
  size--;
  return true;
  }
 }
 else
  return false;
}
template<class T>
void list<T>::showlist()
{
 current=head;
 while(current)
 {
  cout<<" "<<current->getnode();
  current=current->getnext();
 }
 cout<<endl;
}
template<class T>
int list<T>::getsize()
{
 return size;
}
template<class T>
int list<T>::menu1()
{
 cout<<endl<<"================线性表的基本操作==================";
 cout<<endl<<"1.向表中增加一个数据结点";
 cout<<endl<<"2.删除表中的一个数据结点";
 cout<<endl<<"3.查询表中的元素";
 cout<<endl<<"4.显示表中的所有元素.";
 cout<<endl<<"5.显示表中所有元素的数目.";
 cout<<endl<<"6.退出操作.";
 cout<<endl<<"==================================================";
 cout<<endl<<"请选择你的操作:";
 int choice;
 cin>>choice;
 while(choice>6||choice<1)
 {
  cout<<endl<<"你的输入操作有误,请重新输入:";
  cin>>choice;
 }
 return choice;
}
#endif

 


#include<iostream>
#include<string>
#include"listmean.h"
using namespace std;
int main()
{
 int choice2=0,num=0,position=0;
 int flag=0;
    list<int> L(0,0);
 while(1)
 {
  choice2=L.menu1();
  switch(choice2)
  {
  case 1:
   cout<<endl<<"请输入要加入的数据和它要加入的位置:";
   cin>>num>>position;
   flag=L.insertnode(num,position);
            if(flag)
    cout<<endl<<"成功地加入了数据";
   else
    cout<<endl<<"你输入的位置有误,不能够将数据正确的加入到表中.";
   break;
  case 2:
   cout<<endl<<"请输入你要删除的数据:";
   cin>>num;
   flag=L.deletenode(num);
            if(flag)
    cout<<endl<<"你已经成功的删除了该数据.";
   else
    cout<<endl<<"表中不存在这个数据.";
   break;
  case 3:
   cout<<endl<<"请输入你要查找的这个数据:";
   cin>>num;
   flag=L.searchnode(num);
   if(flag)
    cout<<endl<<"你要查找的数据是表中第"<<flag<<"个数据.";
   else
    cout<<endl<<"不存在你要查找的数据.";
   break;
  case 4:
   cout<<endl<<"表中的所有数据为:";
   L.showlist();
   break;
  case 5:
   flag=L.getsize();
   cout<<endl<<"表中有"<<flag<<"个数据.";
   break;
  case 6:
   exit(0);
  }
 }
 return 0;
}

 

阅读(5711) | 评论(3)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册