一个简单的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).而线性表的物理结构,我们已经学习过顺序表,也就是数组 :另一种线性表的物理结构——链表 . 什么 ...
随机推荐
- 【转载】万能adapter
adapter总是自己写,其实使用现成的框架会节省不少代码 原文地址:https://github.com/hongyangAndroid/baseAdapter base-adapter Andro ...
- [Codeforces-div.1 68D] Half-decay tree
[Codeforces-div.1 68D] Half-decay tree 试题分析 增加显然是\(log\)的. 由于某一些叶子结点的答案是一样的,所以我们可以考虑一次性求解. 容易想到一个非常优 ...
- 【Kruskal】Slim Span
[Uva1395]Slim Span 题目略…… 试题分析:codevs1001舒适的路线上加一个判一下连通性就好,顺便把除改成减 代码: #include<iostream> #incl ...
- ZOJ 2589 Circles(平面图欧拉公式)
[题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2589 [题目大意] 给出一些圆,问这些圆可以把平面分为几个部 ...
- “过时”的SpringMVC我们到底在用什么?深入分析DispatchServlet源码
之前已经分析过了Spring的IOC(<零基础带你看Spring源码--IOC控制反转>)与AOP(<从源码入手,一文带你读懂Spring AOP面向切面编程>)的源码,本次就 ...
- [转]spring的ParameterMethodNameResolver
例1: ParameterMethodNameResolver,这个可以根据请求的参数来确定一个需要调用的方法.例如,http://www.sf.net/index.view?testParam=te ...
- SSL协议的握手过程(摘录)
SSL协议的握手过程 为了便于更好的认识和理解 SSL 协议,这里着重介绍 SSL 协议的握手协议.SSL 协议既用到了公钥加密技术(非对称加密)又用到了对称加密技术,SSL对传输内容的加密是采用的对 ...
- GDB的一些技巧
查看栈信息 bt info stack 查看源程序 list ctrl + x + a 分屏调试,上半部分显示代码,下半部分显示调试信息. 查看内存 p xxxptr@n 查看xxxptr 指针内 ...
- java多线程加锁是对谁加锁?
1.java多线程加锁是对谁加锁? 答:当然是对共享资源加锁啊,对谁进行访问修改,就对象进行加锁.以便使多线程按序访问此共享对象 比如: 在具体的Java代码中需要完成一下两个操作:把竞争访问的资源类 ...
- 【QQ输入法】QQ输入法-剪切板 释放内存
发现一个神奇的情况: 清除和关闭的操作: 1. 2.右键 3.点击 4.清空 5.最后需要关闭 QQ输入法的进程