c++链表的实现及栈队列

来源:互联网 发布:mac制作win10安装u盘 编辑:程序博客网 时间:2024/06/06 05:37

链表的源代码实现:

typedef int T;

class List
{
private:
struct Node 
{
   T data;
   Node* next;
   Node(const T& t=T()):data(t)
   {
    next=NULL;
   }
};

//头节点
Node* head;

public:
List():head(NULL){}

//清除所有的节点
void clear()
{
   while (NULL!=head)
   {
    Node *q=head->next;
    delete head;
    head=q;
   }
}

//析构函数调用clear()
~List()
{
   clear();
}

//在链表最前面插入
void insert_front(const T& t)
{
   Node* p=new Node(t);
   p->next=head;
   head=p;
}

//在链表的最后面插入
void insert_back(const T& t)
{
   Node* p=new Node(t);
   if (NULL==head)
   {
    head=p;
   } 
   else
   {
    getPointer(size()-1)->next=p;
   }
}

//遍历所有的节点
void travel()
{
   Node* p=head;
   while (NULL!=p)
   {
    cout<<p->data<<' ';
    p=p->next;
   }
   cout<<endl;
}

//获得链表的元素个数
int size()
{
   int cnt=0;
   Node* p=head;
   while (NULL!=p)
   {
    cnt++;
    p=p->next;
   }

   return cnt;
}

//获得链表中头节点中的数据
T getHead()
{
   if (NULL==head)
   {
    throw "No head";
   }

   return head->data;
}

//获得链表中末节点中的数据
T getTail()
{
    if (NULL!=head)
    {
     throw "No tail";
    }

    Node *p=head;
    while(NULL!=p->next)
    {
     p=p->next;
    }

    cout<<p->data;
}

//判断链表是否为空
bool empty()
{
   return NULL==head;
}

//在链表中查找数据,返回值是要查找的数据在链表中的位置,从0开始计数
int find(const T& t)
{
   int pos=0;
   Node* p=head;

   while (NULL!=p)
   {
    if (p->data==t)
    {
     return pos;
    }

    p=p->next;
    pos++;
   }
   return -1;
}

//更新某个节点上的数据
bool update(const T& o,const T& n)
{
   int pos=find(o);
  
   if (-1==pos)
   {
    return false;
   }

   Node* p=getPointer(pos);
   p->data=n;
   return true;
}

//获得pos位置上的节点
Node* getPointer(const int pos)
{
   Node* p=head;
   for(int i=0;i<pos;i++)
   {
    p=p->next;
   }
   return p;
}

//删除节点数据是t的节点

bool earse(const T& t)
{
   int pos=find(t);

   if (-1==pos)
   {
    return false;
   }
   if (0==pos)
   {
    Node* q=head->next;
   delete head;
    head=q;
    return true;
   }
   else
   {
    Node* pre=getPointer(pos-1);
    Node* cur=pre->next;
    pre->next=cur->next;
    delete cur;
    return true;
   }
};

========================================

栈的源代码实现:

class Stack
{
private:
List l;
public:
//在栈中插入数据,即在链表的最前面插入节点
void push(const T &t)
{
   l.insert_front(t);
}

//删除栈顶节点,即删除链表中的头节点
void pop()
{
   l.earse(l.getHead());
}

//取出栈顶中的数据,即取出链表中头节点中的数据
T top()
{
   return l.getHead();
}

//判断栈是否为空
bool empty()
{
   return l.empty();
}

//取得栈中的元素个数
int size()
{
   return l.size();
}

//清空整个栈
void clear()
{
   l.clear();
}
};

============================================================

队列的源代码实现::

class Queue
{
private:
List l;
public:
//数据入队,即在链表的末尾插入数据节点
void push(const T &t)
{
   l.insert_back(t);
}
//数据出队,即在链表的开始删除数据节点
void pop()
{
   l.earse(l.getHead());
}

//取得队首元素,即获得链表的头节点
T front()
{
   return l.getHead();
}
//取得队尾元素,即获得链表的未节点
T back()
{
   return l.getTail();
}

//判断队列是否为空
bool empty()
{
   return l.empty();
}

//获得队列的元素个数
int size()
{
   return l.size();
}

//清空整个队列
void clear()
{
   l.clear();
}

};