链表的C++实现——创建-插入-删除-输出-清空
注:学习了数据结构与算法分析后,对链表进行了C++实现,参考博文:http://www.cnblogs.com/tao560532/articles/2199280.html
环境:VS2013
//节点的声明
#pragma once class structNode
{
public:
structNode();
~structNode();
struct Node
{
int Data;
Node *next;
}; };
typedef structNode::Node listNode;
//链表的创建
#include "creatLinkList.h"
#include <iostream>
using namespace std; creatLinkList::creatLinkList()
{ } creatLinkList::~creatLinkList()
{ } listNode *creatLinkList::create()
{
head = (listNode *)new(listNode);
head->next = NULL;
tem = head; int n;
cout << "please input the number of the node: " << '\n';
scanf_s("%d", &n); for (int i = ; i <= n; i++)
{
printf("input the data of %d : ", i);
p = (listNode *)new(listNode);
scanf_s("%d", &p->Data);
tem->next = p;
tem = p;
}
p->next = NULL;
return head;
}
//链表的插入
#include "insertLinkList.h"
#include <iostream>
using namespace std; insertLinkList::insertLinkList()
{
} insertLinkList::~insertLinkList()
{
} //在链表的第i个位置后添加一个节点,该节点的数据元素为x
listNode *insertLinkList::insertList(listNode *h)
{
listNode *tem, *p;
int i, x;
tem = (listNode*)new(listNode);
p = (listNode*)new(listNode);
tem = h->next;
int count = ;
cout << "Please input the position: " << '\n';
scanf_s("%d", &i); //在第1个位置插入节点
if (i == )
{
cout << "Please input the first data: " << '\n';
scanf_s("%d", &x);
p->Data = x;
p->next = tem;
h->next = p;
return h;
}
//在其他位置插入节点
while ((count != i) && (tem->next != NULL))
{
tem = tem->next;
count++;
}
if (count != i)
std::cout << "out of space! insert the new data at last of the list" << '\n'; cout << "Please input the data: " << '\n';
scanf_s("%d", &x);
p->Data = x;
p->next = tem->next;
tem->next = p;
return h; }
//链表的删除
#include "deleteLinkList.h"
#include <iostream>
using namespace std; deleteLinkList::deleteLinkList()
{
} deleteLinkList::~deleteLinkList()
{
} //删除链表中数据元素为x的节点
listNode *deleteLinkList::deleteList(listNode *h)
{
listNode *tem, *p;
int x;
tem = (listNode*)new(listNode);
tem = h->next; cout << "Please input the data to delete : " << '\n';
scanf_s("%d", &x); //如果删除第一个节点
if (tem->Data == x)
{
p = tem;
h->next = p->next;
free(p);
return h;
}
//如果删除其余节点
while (tem->next != NULL && tem->next->Data != x)
tem = tem->next;
if (tem->next == NULL)
cout << "oops! out of space!" << '\n';
else
p = (listNode*)new(listNode);
p = tem->next;
tem->next = p->next;
delete(p);
return h;
}
//链表的输出
#include "outputList.h"
#include "creatLinkList.h"
#include <iostream>
using namespace std; outputList::outputList()
{
} outputList::~outputList()
{
} void outputList::coutLinkList(listNode *h)
{
listNode *currentNode;
currentNode = h->next;
while (currentNode)
{
std::cout << currentNode->Data << " ";
currentNode = currentNode->next;
}
cout << "\n";
}
//链表的清空
#include "deleteWholeList.h"
#include <iostream> deleteWholeList::deleteWholeList()
{
} deleteWholeList::~deleteWholeList()
{
} listNode* deleteWholeList::endList(listNode *h)
{
listNode *p,*tem;
p = h->next;
h->next = NULL;
/*tem = p;*/
while (p != NULL)
{
tem = p->next;
free(p);
/*p = tem->next;*/
p = tem;
}
return h;
}
//主函数
#include "creatLinkList.h"
#include "outputList.h"
#include "structNode.h"
#include "insertLinkList.h"
#include "deleteLinkList.h"
#include "deleteWholeList.h"
#include <iostream>
using namespace std;
int main()
{
cout << '\n' <<"***************************************"<< '\n' << '\n';
cout << "Welcome to the linkList world! " << '\n';
cout << '\n' <<"***************************************" << '\n' << '\n'; int i = ;
//int j = 1;
listNode *h = NULL;
creatLinkList a;
outputList b;
insertLinkList c;
deleteLinkList d;
deleteWholeList e;
while ()
{
cout << '\n' << "***************************************" << '\n';
cout << " 0 : end the linkList " << '\n';
cout << " 1 : creat a linkList " << '\n';
cout << " 2 : display a linkList " << '\n';
cout << " 3 : insert a node in the linkList " << '\n';
cout << " 4 : delete a node from the linkList " << '\n';
cout << "***************************************" << '\n';
cout << "Please input the function your want with the number above : " << '\n';
scanf_s("%d",&i); switch (i)
{
case :
cout << "CreatList now begin : ";
h = a.create();
break;
case :
cout << "List now is : ";
b.coutLinkList(h);
break;
case :
cout << "InsertList now begin : ";
h = c.insertList(h);
break;
case :
cout << "DeleteList now begin : ";
h = d.deleteList(h);
break;
default:
cout << "End the list. ";
h = e.endList(h);
//j = 0;
break;
} //structNode::Node *h;
//cout << "CreatList now begin : ";
//creatLinkList a;
//h = a.create(); //cout << "List now is : ";
//outputList b;
//b.coutLinkList(h); //cout << "InsertList now begin : ";
//insertLinkList c;
//h = c.insertList(h);
//cout << "List after insert is : ";
//b.coutLinkList(h); //cout << "DeleteList now begin : ";
//deleteLinkList d;
//h = d.deleteList(h);
//cout << "List after delete is : ";
//b.coutLinkList(h);
} }
部分运行效果如下:




链表的C++实现——创建-插入-删除-输出-清空的更多相关文章
- 「C语言」单链表/双向链表的建立/遍历/插入/删除
		最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ... 
- [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现
		链表获取元素1.声明结点p指向链表第一个结点,j初始化1开始2.j<i,p指向下一结点,因为此时p是指向的p的next,因此不需要等于3.如果到末尾了,p还为null,就是没有查找到 插入元素1 ... 
- jQuery 节点操作(创建 插入  删除 复制 替换 包裹)
		一,创建元素节点: 第1个步骤可以使用jQuery的工厂函数$()来完成,格式如下: $(html); $(html)方法会根据传入的HTML标记字符串,创建一个DOM对象,并将这个DOM对象包装成一 ... 
- HBase表创建、删除、清空
		HBase shell窗口进入 执行命令hbase shell HBase表的创建 # 语法:create <table>, {NAME => <family>, VER ... 
- 单链表创建、删除、查找、插入之C语言实现
		本文将详细的介绍C语言单链表的创建.删除.查找.插入以及输出功能 一.创建 #include<stdio.h> #include<stdlib.h> typedef int E ... 
- 使用OC实现单链表:创建、删除、插入、查询、遍历、反转、合并、判断相交、求成环入口
		一.概念 链表和数组都是一种线性结构,数组有序存储的,链表是无序存储的. 数组中的每一个元素地址是递增或者递减的关系,链表的每一个节点的地址没有此规律,它们是通过指针的指向连接起来. 链表种类:单链表 ... 
- oracle创建、删除 数据库、建立表空间以及插入 删除  修改表
		一.创建.删除数据库 oracle OraDb11g_home->配置和移植工具->Database configration Assistant->...然后可以创建或者删除数据 ... 
- 各种隐藏 WebShell、创建、删除畸形目录、特殊文件名、黑帽SEO作弊(转自核大大)
		其实这个问题,经常有朋友问我,我也都帮大家解决了…… 但是现在这些现象越来越严重,而且手法毒辣.隐蔽.变态,清除了又来了,删掉了又恢复了,最后直接找不到文件了,但是访问网站还在,急的各大管理员.站长抓 ... 
- MySQL数据库(8)----表的创建、删除、索引和更改
		MySQL允许使用 CREATE TABLE 语句和 DROP TABLE 语句来创建.删除表,使用 ALTER TABLE 语句更改其结构.CREATE INDEX 语句和 DROP INDEX 语 ... 
随机推荐
- svg可缩放矢量图形
			可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集),用于描述二维矢量图形的一种图形格式.它由万维网联盟制定,是一个开放标准. <svg xmlns="http://www.w ... 
- hdu 1030  Delta-wave
			Delta-wave Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ... 
- 我的c++学习(7)引用和复制构造函数
			一.引用 什么是引用? 引用又称别名(alias),是一种非常特殊的数据类型.它不是定义一个新的变量,而是给一个已经定义的变量重新起一个别名,也就是 C++系统不为引用类型变量分配内存空间.引用主要用 ... 
- jqgrid在colModel中多次调用同一个字段值
			Debug: 代码1: { name : 'input', index : 'input', width : 100, align : "center", formatter : ... 
- UIImage两种初始化的区别
			UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ... 
- 使用Jaxb2进行xml与bean的转义时Date的format设置
			参考http://jackyrong.iteye.com/blog/1826699 JAXB转换JAVA OBJECT到XML的时候,对java.util.Date的转换有些要注意的地方 输出的格式为 ... 
- 编写unit test以及自动化测试WebDriver
			http://msdn.microsoft.com/en-us/library/hh694602.aspx#BKMK_Quick_starts http://www.seleniumhq.org/ ... 
- 20145304 刘钦令 Java程序设计第一周学习总结
			20145304<Java程序设计>第1周学习总结 教材学习内容总结 1995年5月23日,是公认的Java的诞生日,Java正式由Oak改名为Java. Java的三大平台是:Java ... 
- Leetcode Unique Paths
			A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ... 
- NOIP欢乐模拟赛 T3 解题报告
			3.小澳的葫芦 (calabash.cpp/c/pas) [题目描述] 小澳最喜欢的歌曲就是<葫芦娃>. 一日表演唱歌,他尽了洪荒之力,唱响心中圣歌. 随之,小澳进入了葫芦世界. 葫芦世界 ... 
