链表的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 语 ...
随机推荐
- loadrunner生成随机身份证和银行卡号
生成银行卡号码: Action() { char card[19] = {'6','2','2','7','0','0','0','0','0','0','0','0','0','0','0','0' ...
- js三级省市区选择
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- [转] FastMM、FastCode、FastMove的使用
http://blog.csdn.net/akof1314/article/details/6524767 FastMM是一个替换Embarcadero Delphi Win32应用程序的快速内存管理 ...
- wpf中手风琴控件Accordion编辑模板后控件不正常。
昨天有个网友Accordion控件从sl迁移到wpf时候显示不正常.也是就没有效果. 我也是sl做的比较多,wpf玩的少,Accordion模板里触发器,状态组调了一早上都没达到满意效果, 无奈只有百 ...
- enum枚举类型的使用
修饰符为public static enum,不用加final,否则提示错误. 枚举类的所有实例必须在枚举类中显式列出(,分隔,; 结尾).列出的实例系统会自动添加 public static fin ...
- while的使用
我在用while的时候, while(当这个对象的首个字是汉字){ 执行语句 } 我发现出现死循环的现象 其实我要再添加一个条件,就是不管是不是上述条件都成立,最终还是要结束的.
- fragment中嵌入viewpager的问题
今天终于解决了这个问题 原来问题是出现于viewpager在这里的layout_height不能设置为"wrap_content" 之前我遇到空白的问题,我还以为是管理的问题 所以 ...
- WPF:在ControlTemplate中使用TemplateBinding
A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to w ...
- 获取DLL中的方法名称
OpenFileDialog obj = new OpenFileDialog(); if (obj.ShowDialog() == System.Windows.Forms.DialogResu ...
- ccc 正态分布
cc.Class({ extends: cc.Component, properties: { prefab: { default:null, type:cc.Prefab }, root: { de ...