最近在linux环境下 写了一个用链表实现队列小Demo,直接上代码
链表 list.h头文件
#ifndef _LIST_H_
#define _LIST_H_
#include <iostream>
using namespace std;
template<typename T>
class List
{
private:
struct Node
{
T data;
Node* next;
Node(const T& d=T()):data(d),next(NULL){}
operator T(){ return data;}
};
Node* head;//头指针,用来保存头节点的地址
int len;
public:
List():head(NULL),len(0){}
List(const List<T>& l)
{
len = l.len;
Node* items[len];
for(int i=0;i<len;i++)
{
items[i] = new Node(l.getElementAtIndex(i));
}
for(int i=0;i<len-1;i++)
{
items[i]->next = items[i+1];
}
head = items[0];
}
void operator=(const List<T>& l)
{
clear();
len = l.len;
Node* items[len];
for(int i=0;i<len;i++)
{
items[i] = new Node(l.getElementAtIndex(i));
}
for(int i=0;i<len-1;i++)
{
items[i]->next = items[i+1];
}
head = items[0];
}
~List()
{
clear();
}
//取得链表中指定位置的元素值
T getElementAtIndex(int index)const
{
if(index < 0 || index >= len) throw "索引位置越界";
if(index == 0) return head->data;
Node* p = head;
for(int i=1;i<index;i++)
{
p = p->next;
}
return p->next->data;
}
//前插
List<T>& push_front(const T& d)
{
insert(d,0);
return *this;
}
//尾插
List<T>& push_back(const T& d)
{
insert(d,len);
return *this;
}
//获取链表中节点个数
int size()const
{
return len;
}
//在链表中找指向指定位置的指针
Node*& getptr(int pos)
{
if(pos < 0 || pos > len) pos = 0;
if(pos == 0) return head;
Node* p = head;
for(int i=1;i<pos;i++)
{
p = p->next;
}
return p->next;
}
//在任意位置插入节点
List<T>& insert(const T& d,int pos)
{
Node*& pn = getptr(pos);
Node* p = new Node(d);
p->next = pn;
pn = p;
++len;
return *this;
}
//遍历
void travel()const
{
Node* p = head;
while(p)
{
cout << *p << ' ';
p = p->next;
}
cout << endl;
}
//清空链表
void clear()
{
while(head)
{
Node* p = head->next;
delete head;
head = p;
}
len = 0;
}
//删除链表中指定位置的节点
void erase(int pos)
{
if(pos < 0 || pos >= len) return;//有效位置为0~len-1
Node*& pn = getptr(pos);
Node* p = pn;
pn = pn->next;
delete p;
--len;
}
//查找指定数值的节点在链表中出现的位置
int find(const T& d)const
{
Node* p = head;
int pos = 0;
while(p)
{
if(p->data == d)
return pos;
else
p = p->next;
++pos;
}
return -1;
}
//删除链表中指定数值的节点
void remove(const T& d)
{
int pos;
while((pos = find(d))!= -1)
{
erase(pos);
}
}
//修改指定位置的节点数据
void set(int pos,const T& d)
{
if(pos < 0 || pos >= len) return;
getptr(pos)->data = d;
}
//判断链表是否为空
bool empty()const
{
return head == NULL;
}
//取得链表中第一个节点数据
const T& front()const
{
if(empty()) throw "空";
return head->data;
}
//取得链表中最后一个节点数据
const T& back()const
{
if(empty()) throw "空";
Node* p = head;
while(p->next)
{
p = p->next;
}
return p->data;
}
//将链表中的元素倒置
void reverse()
{
if(head == NULL) return;
Node *pre,*cur,*next;
pre = head;
cur = head->next;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = NULL;
head = pre;
}
};
#endif
队列头文件 queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "list.h"
template <typename T>
class Queue
{
private:
List<T> l;
public:
Queue& push(const T& d)
{
l.push_back(d);
return *this;
}
T pop()
{
T t = l.front();
l.erase(0);
return t;
}
const T& front()const
{
return l.front();
}
const T& back()const
{
return l.back();
}
int size()const
{
return l.size();
}
void clear()
{
l.clear();
}
bool empty()const
{
return l.empty();
}
bool full()const
{
return false;
}
};
#endif
测试代码
#include "queue.h"
#include <iostream>
using namespace std;
int main()
{
Queue<int> q;
q.push(1).push(2).push(3);
q.push(4).push(5);
cout << q.pop() << endl;
cout << q.pop() << endl;
q.push(6).push(7).push(8);
while(!q.empty())
{
cout << q.pop() << endl;
}
return 0;
}