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++单链表的实现的更多相关文章

  1. 链表习题(2)-一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点。

    /*一个集合用带头结点的单链表L表示,编写算法删除其值最大的结点.*/ /* 算法思想:使用pre,p,premax,max四个指针,pre和p进行比较,premax和max进行最后的删除操作 通过遍 ...

  2. (C++)读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔)

    1 /* 2 程序功能:读取一个输入的int型十进制数字的位数,并正序输出每个位上的值(不同数位的值用1个空格字符间隔). 3 例如:当输入985这个数字时,显示如下信息: 4 985是一个3位数字! ...

  3. 简单约瑟夫环的循环单链表实现(C++)

    刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...

  4. 用最简单的方式学Python单链表

    Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...

  5. 如何用WebSocket实现一个简单的聊天室以及单聊功能

    百度百科中这样定义WebSocket:WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端.简单的说,We ...

  6. C++ Daily 《4》----一个简单的 int to string 的方法

    经常会在项目中用到 int to string, 之前一般用C语言的 sprintf, 发现C++ 中的 ostringstream 可以轻松完成这个任务. #include <iostream ...

  7. 一个简单的jquery ajax表单提交 带数据校验 layer弹框提示

    <input type="hidden" id="url" value="index.php"/> <form id=&q ...

  8. JAVA单链表的实现-不带头结点但带有尾指针

    1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...

  9. C++ 单链表的基本算法

    线性表是最简单,最常用的一种数据结构.线性表的逻辑结构是n个数据元素的有限序列(a1,a2,…,an).而线性表的物理结构,我们已经学习过顺序表,也就是数组 :另一种线性表的物理结构——链表 . 什么 ...

随机推荐

  1. Sqli-labs less 6

    Less-6 Less6与less5的区别在于less6在id参数传到服务器时,对id参数进行了处理.这里可以从源代码中可以看到. $id = '"'.$id.'"'; $sql= ...

  2. Flask实战第46天:完成前台登录功能

    后台逻辑 首先进行表单验证, 编辑front.froms.py ... class SignInForm(BaseForm): telephone = StringField(validators=[ ...

  3. 2017腾讯OMG实习生面试总结

    2017腾讯OMG实习生面试总结 一面 一面有两个面试官,轮着来的,一共是一个半小时,中间等了10分钟左右.结合简历问了问项目,也考察了基础,手写了两道简单的算法题.问题包括: 第一个面试官 1.自我 ...

  4. python 日历(Calendar)模块

    另附一篇文章:http://www.jb51.net/article/77971.htm 序号 函数及描述 1. calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字 ...

  5. AvalonJS学习笔记(一)

    一.关于AvalonJS avalon是国内的一个MVVM框架,是从knockout发展起来的 分为两个版本 avalon.js版本,支持IE6及非常老的标准浏览器.这里的标准浏览器特指W3C阵营中的 ...

  6. 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles

    贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...

  7. 【带权并查集】poj1182 食物链

    带权并查集,或者叫做种类并查集,经典题. http://blog.csdn.net/shuangde800/article/details/7974668 这份代码感觉是坠吼的. 我的代码是暴力分类讨 ...

  8. 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分

    E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...

  9. linux shell实现随机数多种方法(date,random,uuid)

    参考: http://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html $ cat /proc/sys/kernel/random/uui ...

  10. GCC编绎详解

    http://www.cnblogs.com/lisuyun/p/4169395.html http://blog.csdn.net/kai_gai/article/details/45689247 ...