链表的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 语 ...
随机推荐
- vim 清空
插入模式 首先执行gg 跳至文件首行 然后执行dG就清空了整个文件 还有一种方法就要退出VIM,然后使用echo > file ,这样也能快速清空文件内容
- C语言连接SQLSERVER数据库
第一步:配置ODBC.在配置ODBC时有用户DSN.系统DSN.和文件DSN三种方法,为了稳妥起见,采用系统DSN. DSN的名字叫LocalServer,帐号:sa,密码123456 第二步:打开V ...
- Uva 11988 Broken Keyboard
因为要经常移动这些字符,所以采用的数据结构是链表. next[i]起到的作用大概就是类似链表里的next指针. 0.需要注意的是,判断cur == last ? 如果 是 则 last=i 1.另外一 ...
- (转)qsort和sort
1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)) ...
- iOS开发实用干货——强化你的Xcode控制台
f(x) 郑秀晶程序员不要整天看代码,偶尔也要看看风景?? www.90168.org先上一张我的Xcode控制台的图片让你们感受一下 酷炫控制台 是不是觉得很酷?不过仅仅是酷还是远远不够的,当你点击 ...
- BZOJ3257 : 树的难题
设$f[x][i][j]$表示以$x$为根的子树,与$x$连通部分有$i$个黑点,$j$个白点,不联通部分都是均衡的最小代价.若$i>1$,则视作$1$:若$j>2$,则视作$2$. 然后 ...
- BZOJ4107 : [Wf2015]Asteroids
首先将速度相减,变成A在动而B不动,若速度为0则显然永远不会相交. 枚举A的每个点以及B的每条线段,计算这三个点共线的时刻. 将时刻排序,对于每个区间进行三分,用半平面交计算相交面积. 注意特判相交面 ...
- Window.location
1.location 对象 // 假设当前url是 http://localhost/rpc/plugin.php#hash?a=aaa&b=bbb alert(window.location ...
- JS保留小数点(四舍五入、四舍六入)实例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...