CPP链表实现栈

一:链表的方法文件集:list.h

#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
typedef std::string T;
class List
{
//定义节点
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:
List() :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 尾部插入数据
*/
List& 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;
}
/*
析构
*/
~List()
{
clear();
}
};
#endif

//stack.cpp文件

#pragma warning( disable : 4290 )
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <string>
typedef std::string T;
#include "list.h"
using namespace std;
class Stack
{
List l;
public: 
void push(const T& d);//数据入栈
T pop()throw (const char *);//栈顶数据出栈
const T& top()throw (const char*);//取得栈顶的数据,也就是最后的数据
bool isemple() const//是否是空栈
{
return l.isempty();
}
bool isfull() const//是否已经满
{
return false;
}
void clear()//清空栈
{
l.clear();
}
int size() const//栈里面元素的个数
{
return l.size() ;
}
};
void Stack::push(const T&d)
{
l.push_front(d);//头上出入数据效率高
}
T Stack::pop() throw (const char *)
{
if (Stack::isemple()) throw "空";
T t = l.front();//取出第一个元素
l.erase(0);//删除第一个元素
return t;
}
const T& Stack::top() throw (const char*)//取得栈顶的数据,也就是最后的数据
{
if (Stack::isemple()) throw "空";
return l.front();//栈顶元素
}
void main()
{
Stack s;
try
{
s.push("hang,");
s.push("zhou,");
s.push("zidognhua,");
}
catch (const char *e)
{

cout << "异常:" << e << endl;
}
while (!s.isemple())
{
cout << s.pop() << endl;
cout << s.size() << endl;
}
system("pause");


}

///注意typedef std::string  T;在list.h和stack.cpp中要一致,后期模板来解决这个问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值