注:学习了数据结构与算法分析后,对链表进行了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++实现——创建-插入-删除-输出-清空的更多相关文章

  1. 「C语言」单链表/双向链表的建立/遍历/插入/删除

    最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ...

  2. [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现

    链表获取元素1.声明结点p指向链表第一个结点,j初始化1开始2.j<i,p指向下一结点,因为此时p是指向的p的next,因此不需要等于3.如果到末尾了,p还为null,就是没有查找到 插入元素1 ...

  3. jQuery 节点操作(创建 插入 删除 复制 替换 包裹)

    一,创建元素节点: 第1个步骤可以使用jQuery的工厂函数$()来完成,格式如下: $(html); $(html)方法会根据传入的HTML标记字符串,创建一个DOM对象,并将这个DOM对象包装成一 ...

  4. HBase表创建、删除、清空

    HBase shell窗口进入 执行命令hbase shell HBase表的创建 # 语法:create <table>, {NAME => <family>, VER ...

  5. 单链表创建、删除、查找、插入之C语言实现

    本文将详细的介绍C语言单链表的创建.删除.查找.插入以及输出功能 一.创建 #include<stdio.h> #include<stdlib.h> typedef int E ...

  6. 使用OC实现单链表:创建、删除、插入、查询、遍历、反转、合并、判断相交、求成环入口

    一.概念 链表和数组都是一种线性结构,数组有序存储的,链表是无序存储的. 数组中的每一个元素地址是递增或者递减的关系,链表的每一个节点的地址没有此规律,它们是通过指针的指向连接起来. 链表种类:单链表 ...

  7. oracle创建、删除 数据库、建立表空间以及插入 删除 修改表

    一.创建.删除数据库 oracle OraDb11g_home->配置和移植工具->Database configration  Assistant->...然后可以创建或者删除数据 ...

  8. 各种隐藏 WebShell、创建、删除畸形目录、特殊文件名、黑帽SEO作弊(转自核大大)

    其实这个问题,经常有朋友问我,我也都帮大家解决了…… 但是现在这些现象越来越严重,而且手法毒辣.隐蔽.变态,清除了又来了,删掉了又恢复了,最后直接找不到文件了,但是访问网站还在,急的各大管理员.站长抓 ...

  9. MySQL数据库(8)----表的创建、删除、索引和更改

    MySQL允许使用 CREATE TABLE 语句和 DROP TABLE 语句来创建.删除表,使用 ALTER TABLE 语句更改其结构.CREATE INDEX 语句和 DROP INDEX 语 ...

随机推荐

  1. 【转】Kylin实践之使用Hive视图

    http://blog.csdn.net/yu616568/article/details/50548967 为什么需要使用视图 Kylin在使用的过程中使用hive作为cube的输入,但是有些情况下 ...

  2. kylin1.5新特性 new aggregation group

    终于啃完并理解了,我果然弱鸡.new aggregation group,是kylin 1.5的新特性:老版本中的agg是需要选中所有可能被使用的纬度字段,以供查询:但存在高纬度的查询需求,例如查询某 ...

  3. python 多态

    多态 类具有继承关系,并且子类类型可以向上转型看做父类类型,如果我们从 Person 派生出 Student和Teacher ,并都写了一个 whoAmI() 方法: class Person(obj ...

  4. 某app客户端数字签名分析

    最近测试app时发现某app对数据包做了签名,其直接后果就导致截获的数据包没法修改,因此对该app的数字签名了进行了一次分析.

  5. SpringJDBC解析3-回调函数(update为例)

    PreparedStatementCallback作为一个接口,其中只有一个函数doInPrepatedStatement,这个函数是用于调用通用方法execute的时候无法处理的一些个性化处理方法, ...

  6. Uva 11059 Maximum Product

    注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! #incl ...

  7. psql-01基本介绍

    安装与启动 安装: apt-get install postgresql / yum install postgresql.XXX; 启动: mac下直接打开 linux service postgr ...

  8. DOM--4 响应用户操作和事件(1)

    简介 事件:事件并不是以"on"开头的.例如,onclick引用的是一个对象的属性,click才是事件. 事件侦听器:当指定的事件发生时会执行的函数或方法. 事件注册:为DOM元素 ...

  9. Ue4全景图制作设想

    官方有个Scene Capture Cube与Cube Rander Target. 之后再想办法生成文件就好了吧

  10. Codeforces 677E Vanya and Balloons(DP + 一些技巧)

    题目大概说给一张地图,地图每个格子都有0到9中的某一个数字.现在要在一个格子放炸弹,炸弹爆炸后水柱有两种扩展方式,一种是上.下.左.右,另一种是左上.右下.右上.左下,且四个方向的长度都一样.问放哪个 ...