CPP 链表实现队列

链表实现队列

一、链表头文件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");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值