一个简单的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).而线性表的物理结构,我们已经学习过顺序表,也就是数组 :另一种线性表的物理结构——链表 . 什么 ...
随机推荐
- UVa 221 Urban Elevations 城市正视图 离散化初步 无限化有限
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 221 Urban Elevations 给出城市中建筑物的x, ...
- 洛谷——P2708 硬币翻转
P2708 硬币翻转 题目背景 难度系数:☆☆☆☆☆(如果你看懂了) 题目描述 从前有很多个硬币摆在一行,有正面朝上的,也有背面朝上的.正面朝上的用1表示,背面朝上的用0表示.现在要求从这行的第一个硬 ...
- Codeforces 555 C. Case of Chocolate
\(>Codeforces \space 555 C. Case of Chocolate<\) 题目大意 : 有一块 \(n \times n\) 的倒三角的巧克力,有一个人要吃 \(q ...
- vijos p1777 引水入城(bfs+贪心)
引水入城 描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使 ...
- ccpc秦皇岛部分题解
A. 题意:就是有一个大桌子,环绕有顺势站1~m共m个座位,n个选手坐在部分位置上.然后如果有一个人a了一道题,却没有立刻发气球给他,他产生怒气值是发气球给他的时间减去a题时间.现在有一个机器人顺时针 ...
- [拒绝毒瘤的小清新系列] give you a tree
题面在这里! (小声)这其实是我读错某题之后自己出的一道题...... 正解也很简单啦,直接扫描线+线段树水过(考虑一个合法的区间正好有 siz-1 对树上相邻的点对)23333 #include&l ...
- ListView EditText 实现的三种方式
原文网址:http://blog.sina.com.cn/s/blog_682139d001013omd.html 这段时间搞一个android表格,真是搞死人,让我想到iphone开发是何其的简单啊 ...
- http请求 url 传递参数(特殊字符)解决方法
有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码 十六进制值 1. + URL 中+号表示空格 %2B ...
- CentOS 6.9下KVM虚拟机网络Bridge(网桥)方式与NAT方式详解(转)
摘要:KVM虚拟机网络配置的两种方式:NAT方式和Bridge方式.Bridge方式的配置原理和步骤.Bridge方式适用于服务器主机的虚拟化.NAT方式适用于桌面主机的虚拟化. NAT的网络结构图: ...
- Web安全测试指南--权限管理
垂直权限提升: 编号 Web_Author_01 用例名称 垂直权限提升测试 用例描述 测试用户是否具有使用超越其角色范围之外的权限. 严重级别 高 前置条件 1. 目标系统拥有不同等级的角色和权限 ...