一个简单的int型C++单链表的实现
IntSLList.h
//************************ intSLList.h **************************
// singly-linked list class to store integers #ifndef INT_LINKED_LIST
#define INT_LINKED_LIST class IntSLLNode {
public:
IntSLLNode() {
next = ;
}
IntSLLNode(int el, IntSLLNode *ptr = ) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
}; class IntSLList {
public:
IntSLList() {
head = tail = ;
}
~IntSLList();
int isEmpty() {
return head == ;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head, *tail;
}; #endif
IntSLList.cpp
//************************ intSLList.cpp ************************** #include <iostream>
#include "intSLList.h" using namespace std; IntSLList::~IntSLList() {
for (IntSLLNode *p; !isEmpty();) {
p = head->next;
delete head;
head = p;
}
} void IntSLList::addToHead(int el) {
head = new IntSLLNode(el, head);
if (tail == )
tail = head;
} void IntSLList::addToTail(int el) {
if (tail != ) { // if list not empty;
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
} int IntSLList::deleteFromHead() {
int el = head->info;
IntSLLNode *tmp = head;
if (head == tail) // if only one node on the list;
head = tail = ;
else head = head->next;
delete tmp;
return el;
} int IntSLList::deleteFromTail() {
int el = tail->info;
if (head == tail) { // if only one node on the list;
delete head;
head = tail = ;
}
else { // if more than one node in the list,
IntSLLNode *tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = ;
}
return el;
} void IntSLList::deleteNode(int el) {
if (head != ) // if non-empty list;
if (head == tail && el == head->info) { // if only one
delete head; // node on the list;
head = tail = ;
}
else if (el == head->info) { // if more than one node on the list
IntSLLNode *tmp = head;
head = head->next;
delete tmp; // and old head is deleted;
}
else { // if more than one node in the list
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next; // and a non-head node
tmp != && !(tmp->info == el);// is deleted;
pred = pred->next, tmp = tmp->next);
if (tmp != ) {
pred->next = tmp->next;
if (tmp == tail)
tail = pred;
delete tmp;
}
}
} bool IntSLList::isInList(int el) const {
IntSLLNode *tmp;
for (tmp = head; tmp != && !(tmp->info == el); tmp = tmp->next);
return tmp != ;
} void IntSLList::printAll() const {
for (IntSLLNode *tmp = head; tmp != ; tmp = tmp->next)
cout << tmp->info << " ";
cout << endl;
}
main.cpp
#include <iostream>
#include "intSLList.h" using namespace std; int main(int argc, char* argv[])
{
int end; IntSLList *list = new IntSLList(); list->addToHead();
list->addToHead();
list->addToHead();
list->addToHead();
list->addToHead();
list->addToHead(); list->printAll(); delete list; cout << "-----------------------------------------------------------" << endl; IntSLList *list2 = new IntSLList(); list2->addToTail();
list2->addToTail();
list2->addToTail();
list2->addToTail();
list2->addToTail();
list2->addToTail(); list2->printAll(); delete list2; cout << "Press Any Key to Continue ... " << endl;
cin >> end; return ;
}
运行结果:
999 500 400 300 200 100
-----------------------------------------------------------
100 200 300 400 500 999
Press Any Key to Continue ...
一个简单的int型C++单链表的实现的更多相关文章
- 链表习题(2)-一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点。
/*一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点.*/ /* 算法思想:使用pre,p,premax,max四个指针,pre和p进行比较,premax和max进行最后的删除操作 通过遍 ...
- (C++)读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔)
1 /* 2 程序功能:读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔). 3 例如:当输入985这个数字时,显示如下信息: 4 985是一个3位数字! ...
- 简单约瑟夫环的循环单链表实现(C++)
刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...
- 用最简单的方式学Python单链表
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...
- 如何用WebSocket实现一个简单的聊天室以及单聊功能
百度百科中这样定义WebSocket:WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端.简单的说,We ...
- C++ Daily 《4》----一个简单的 int to string 的方法
经常会在项目中用到 int to string, 之前一般用C语言的 sprintf, 发现C++ 中的 ostringstream 可以轻松完成这个任务. #include <iostream ...
- 一个简单的jquery ajax表单提交 带数据校验 layer弹框提示
<input type="hidden" id="url" value="index.php"/> <form id=&q ...
- JAVA单链表的实现-不带头结点但带有尾指针
1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...
- C++ 单链表的基本算法
线性表是最简单,最常用的一种数据结构.线性表的逻辑结构是n个数据元素的有限序列(a1,a2,…,an).而线性表的物理结构,我们已经学习过顺序表,也就是数组 :另一种线性表的物理结构——链表 . 什么 ...
随机推荐
- 【转载】SwipeRefreshLayout源码解析
原文地址:https://github.com/hanks-zyh/SwipeRefreshLayout/blob/master/README.md 官方文档 SwipeRefreshLayout 是 ...
- Thinkphp模板中函数的使用
1.在模板中使用php函数 在thinkphp的html中,我们经常会遇到一些变量难以直接从php控制端直接处理,这些变量只有在模板中循环输出的时候处理比较合适,这个时候,我们就要在模板中使用函数 1 ...
- POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- 【我要学python】open函数的简单用法
open函数 1,使用方法:open('文件路径', '模式',编码方式). 2,最好使用with open as: 省去每一次都需要close()的环节 3,模式介绍: ①w 可写(如果存在,会覆盖 ...
- ALL运算符
ALL在英文中的意思是“所有”,ALL运算符要求比较的值需要匹配子查询中的所有值.ALL运算符同样不能单独使用,必须和比较运算符共同使用. 下面的SQL语句用来检索在所有会员入会之前出版的图书: SE ...
- AGC 016 C - +/- Rectangle
题面在这里! 结合了贪心的构造真是妙啊2333 一开始推式子发现 权是被多少个w*h矩形覆盖到的时候 带权和 <0 ,权都是1的时候带权和 >0,也就是说被矩形覆盖的多的我们要让它尽量小. ...
- Activit(活动)实践--知晓当前活动
实际上,我们可能用的不是自己写的项目,而是从别人那里接手过来的代码,因为你刚进公司就有一个新项目开始的概率十分低.阅读别人代码时会有一个很头疼的问题,就是当你需要在某个界面上修改一些非常简单的东西时, ...
- NHibernate之一级缓存(第十篇)
NHibernate的一级缓存,名词好像很牛B,很难.实际上就是ISession缓存.存储在ISession的运行周期内.而二级缓存则存储在ISessionFactory内. 一.ISession一级 ...
- 微软工具ILMerge
释义 ILMerge是一个可用于将多个.NET程序集合并为单个程序集的实用程序. ILMerge接收一组输入程序集并将它们合并到一个目标程序集中.输入程序集列表中的第一个程序集是主程序集. 当主组件是 ...
- ejs循环实例
... //index page var items=[{title:"文章1"},{title:"文章2"}]; app.get('/',function(r ...