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. python 重新执行循环中出错的那一次

    # coding: utf-8 li = [1,2,3,4,5] for num in li: while True: try: # do something except some error: c ...

  2. PHP文件包含小结

    协议 各种协议的使用有时是关键 file协议 file后面需是///,例如file:///d:/1.txt 也可以是file://e:/1.txt,如果是在当前盘则可以file:///1.txt 如果 ...

  3. 洛谷——P1927 防护伞

    P1927 防护伞 题目描述 据说 2012 的灾难和太阳黑子的爆发有关.于是地球防卫小队决定制造一个特殊防护 伞,挡住太阳黑子爆发的区域,减少其对地球的影响.由于太阳相对于地球来说实在是太 大了,我 ...

  4. 微软移除Visual Studio 2015中的UML

    微软已经在Visual Studio 2015中移除了UML(Unified Modeling Language,统一建模语言),原因是该语言使用率过低.因此微软要优化产品结构,把好钢用在刀刃上. V ...

  5. python 日历(Calendar)模块

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

  6. VB查询数据库之写入数据库——机房收费系统总结(三)

    在机房收费系统中,新注册的用户,更改的密码,上机下级记录,上机收费记录等等都要写入数据库,这样,后面的查询才能生效.像数据库中写入数据,首先,找到你要写入数据的数据库中的表,在表中建立新的行,让后再把 ...

  7. android 内存分哪些区

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com android 内存分哪些区 内存分哪些区 ============ 内存分为的5大区 1.栈区 ...

  8. AtCoder - 1999 Candy Piles

    Problem Statement There are N piles of candies on the table. The piles are numbered 1 through N. At ...

  9. [TCO2009]NumberGraph

    题意:给你一些带权的节点和一个正整数集合$S$,$S$中每一个数的二进制后缀$0$个数相同,节点$x$的权值为$v_x$,如果对于$x,y$存在$t\in S$使得$|v_x-v_y|=t$,那么连边 ...

  10. 【搜索】bzoj3109 [cqoi2013]新数独

    搜索,没什么好说的.要注意读入. Code: #include<cstdio> #include<cstdlib> using namespace std; ][]= {{,, ...