链表实现队列
一、链表头文件listcpp.h
#ifndef LISTCPP_H
#define LISTCPP_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
typedef std::string T;
class ListCpp
{
//定义节点
struct Node
{
T data;
Node *next;
//const T&d = T() 零初始化,基本类型就是数值0,
//对自定义类型而言就是建个对象不穿参数,适应所有类型
Node(const T&d = T()) :data(d), next(NULL){}
};
Node *head;//头指针
int len;
public:
ListCpp() :head(NULL), len(0)//链表构造函数
{
Node n;
}
/**
前面插入数据
*/
void push_front(const T&d)
{
//使用new 在push_front函数退出后,节点不会消失,调用delete才失去
/* Node *p = new Node(d);
//头部插入
p->next = head;
head = p;*/
//可以改变成:
insert(d, 0);
}
/**
尾部插入数据
*/
void push_back(const T&d)
{
insert(d, len);
}
/**
连续.push 尾部插入数据
*/
ListCpp& continue_push_back(const T&d)
{
insert(d, len);
return *this;
}
/*
Node*& &与Node* 没有关系,&引用,代表直接使用链表中的值而不是副本
链表中指定pos位置的指针
*/
Node *&getptr(int pos)
{
//当插入的位置小于0时,默认是头插入,插入位置大于末尾位置,就默认插入到尾位置!
if (pos <= 0)return head;
Node *p = head;
for (int i = 1; i < pos&&p->next != NULL; i++)
{
p = p->next;
}
return p->next;
}
/*
任意位置插入
*/
void insert(const T&d, int pos)
{
//没插入一个节点,len加1;
len++;
Node *p = new Node(d);
Node *&pn = getptr(pos);//pn是原始数据的别名
p->next = pn;
pn = p;
}
/*
删除节点(按照位置删除)
*/
void erase(int pos)
{
if (pos <0 || pos >len - 1) return;
Node *&pn = getptr(pos);
Node *p = pn;//另存一份pn 则在定义Node *p 的时候不要定义为:Node* &p;这样的定义意思就是pn的别名,二者是一样的地址。
pn = pn->next;
delete p;
p = NULL;
len--;//节点的数量减少1个;
}
/*
根据数据查找位置
*/
int find(const T&d)const
{
int pos = 0;
Node *p = head;
while (p)
{
if (p->data == d)
return pos;
pos++;
p = p->next;
}
return -1;
}
/*
删除节点(按照数值)
*/
void remove(const T&d)
{
//先找到这个数据的位置
int pos_find = find(d);
if (pos_find != -1)
{
erase(pos_find);
}
}
/*
设置节点数据
*/
void set(int pos, const T&d)
{
if (pos <0 || pos >= len)return;
getptr(pos)->data = d;
}
/*
遍历数据
*/
void showlist()
{
Node *p = head;
while (p != NULL)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
/*
判断是否为空
*/
bool isempty()const
{
return head == NULL;
}
/*
清空整个链表
*/
void clear()
{
while (!isempty())
{
Node *p = head->next;
delete head;
head = p;
}
len = 0;//清除节点后,len设置为0
}
/*
front();找到头节点
*/
T front()const
{
if (isempty())throw "空";
return head->data;
}
/*
back;找到尾部节点
*/
T back()const
{
Node *p = head;
if (isempty())throw "空";
while (p->next != NULL)
{
p = p->next;
}
return p->data;
}
/*
计算链表中节点个数
*/
int size()const
{
return len;
}
/*
析构
*/
~ListCpp()
{
clear();
}
};
#endif
二、队列
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include "listcpp.h"
using namespace std;
class Queue
{
ListCpp l;
public :
Queue& pushcontinue(const T&d)
{
l.push_back(d);
return *this;
}
void push(const T&d)
{
//队尾添加数据
l.push_back(d);
}
const T& front()const
{
return l.front();
}
T pop()
{
//T t = front();
T t = l.front();//注意使用上面的这个front(Queue中的也是直接调用l.front,但是出现问题),
l.erase(0);
return t;
}
const T& back()
{
return l.back();
}
int size() const
{
return l.size();
}
void clear()
{
l.clear();
}
bool isempty() const
{
return l.isempty();
}
bool isfull() const
{
return false;
}
};
void main()
{
Queue q;
q.pushcontinue("hang 1,").pushcontinue("hang 2,").pushcontinue("hang 3,");
while (!q.isempty())
cout << q.pop() << " , " << endl;
system("pause");
}