首页 新闻 会员 周边

c++链表模板的问题

-1
[待解决问题]

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include  "Node.h"

template
class LinkedList
{
private:
 Node*front,*rear;
 Node*prevPtr,*currPtr;
 int size;
 int  position;
 Node*newNode(const T&item,Node*ptrNext=NULL);
 void freeNode(Node*p);
 void copy(const LinkedList&L);
public:
 LinkedList();
 LinkedList(const LinkedList &L);
 ~LinkedList();
 LinkedList &operator =(const LinkedList&L);
 int getSize()const;
 bool isEmpty()const;
 void reset(int pos=0);
 void next();
 bool endOfList()const;
 int currentPosition(void )const;
 void insertFront(const T &item);
 void insertRear(const T &item);
 void insertAt(const T &item);
 void insertAfter(const T &item);
 T deleteFront();
 void deleteCurrent();
 T&data();
 const T &data()const;
 void clear();
};
#endif

 

 

#ifndef NODE_H
#define NODE_H
template
class Node
{
private:
 Node*next;
public:
 T data;
 Node(const T&data,Node*next=0);
 void insertAfter(Node*p);
 Node *deleteAfter();
 Node*nextNode();
 const Node*nextNode()const;
};
template
Node::Node(const T& data,Node*next):data(data),next(next){}

template
Node*Node::nextNode()
{
 return next;
}

template
const Node*Node::nextNode()const
{
 return next;
}

template
void Node::insertAfter(Node*P)
{
 p->next=next;
 next=p;
}

template
Node*Node::deleteAfter()
{
 Node*tempPtr=next;
 if(next==0)
  return 0;
 next=tempPtr->next;
 return tempPtr;
}
#endif

 

#include
#include"LinkedList.h"
using namespace std;
void main()
{
 LinkedListlist;//定义一个int型的链表list
 cout<<"Please input 10 numbers:";
 for(int i=0;i<10;i++)
 {
  int item;
  cin>>item;
  list.insertFront(item);
 }
 cout<<"List:";
 list.reset();//输出各节点数据,直到链表尾
 while(!list.endOfList())
 {
  cout<<list.data()<<",";
  list.next();//指向下一个节点
 }
 cout<<endl;
 int key;
 cout<<"Please input the number needed to bedelete:";
 cin>>key;
 list.reset();
 while(!list.endOfList())
 {
  if(list.data()==key)
   list.deleteCurrent();
  list.next();
 }
 cout<<"list:";
 list.reset();
 while(!list.endOfList())
 {
  cout<<list.data()<<",";
  list.next();
 }
 cout<<endl;
}

#include
using namespace std;
#if 0
int const SIZE=100;
 template
 int input(T a[])
 {
 T n;
 int i=0;
 while((cin>>n)&&n!='+')
 {
  a[i]=n;
  i++;
 }
 int length=i;
 return length;
 }
template
 void sort(T a[],int length)
  {
  T temp;
  int i=0, j=0;
  for(i=1;i<=length-1;i++)
  {  
   temp=a[i];
   for(j=i;j>0&&a[j-1]>temp;j--)
   {  
    a[j]=a[j-1];
   }
   a[j]=temp;
  }
 
 }
  template
 void show(T a[],int length)
 {
  for(inti=0;i<=length-1;i++)
   cout<<a[i]<<",";
  cout<<endl;

 }

void main()
 {
  cout<<"输入要进行排序的数据:";
  int a[SIZE];
  int length=input(a);
  cout<<"原来数据为:";
  show(a,length);
  sort(a,length);
  cout<<"排序后数据为:";
  show(a,length);
  cout<<"输入要进行排序的数据:";
  char s[SIZE]="";
  cin.clear();
  cin.sync();
  length=input(s);
  cout<<"原来数据为:";
  show(s,length);
  sort(s,length);
  cout<<"排序后数据为:";
  show(s,length);
 }
#endif

做自己的北极星的主页 做自己的北极星 | 菜鸟二级 | 园豆:204
提问于:2015-12-07 20:01
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册