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();
}
};
- 各种实现队列的问题:链表实现,栈实现队列及队列实现栈
- 队列 队列的数组实现及链表实现
- 链表及队列的实现
- c++链表的实现及栈队列
- 双向链表、队列与栈的C/C++实现
- 【C++】队列的实现
- 顺序队列及链队列的基本操作实现
- 数据结构队列及栈的实现
- 队列及栈相关题目的实现
- 顺序栈、链栈已及队列的实现
- c语言实现多线程下的链表队列
- 队列-C语言-链表的实现方式
- 队列的C语言实现(通过内核链表)
- 链表、栈、队列的C语言实现
- (C++)栈和队列的模拟实现
- 链队列的实现 C语言
- 链队列的C语言实现
- 链队列的C语言实现
- 出色的html滑动效果库 swiper -例子1 横向滑动,结束自动切换回第一栏
- Word Break
- 用socketpair()进行进程间的全双工通讯
- aio,epoll,libevent,boost::asio解决的问题
- Ubuntu 将应用程序 固定到快快速启动栏
- c++链表的实现及栈队列
- UVA - 11549 Calculator Conundrum
- LeetCode Substring with Concatenation of All Words暴力法暴力法更加暴力的方法
- 次方求模
- Codeforces Round #216 (Div. 2) C - Valera and Elections(图论和dfs)
- 嵌入式学习开始分析之学习步骤及方法,有待考证!!
- 据说这是一堂价值300万元的课。有空看下,不要错过
- 面试题
- Qt编写串口通信程序全程图文讲解