LinkList Operation
链表典型数据结构:
#define ElemType int
typedef struct LinkNode{
ElemType value;
struct LinkNode* next;
};
相比于顺序结构,链式存储结构好处在于在任意位置插入或者删除元素,操作时间比较短。 1 节点回收以及申请
注意指针和节点的关系,防止产生游离节点。记住不管是节点内容以及指针一定要初始化
LinkList Create_Linkist(int elem){
LinkList head;
head = (LinkList)malloc(sizeof(LinkNode));
if(!head) exit(EXIT_FAILURE);
head->value = elem;
head->next = NULL;
return head;
}
假设head指针指向第一个元素,那么插入元素时
无序插入表头插入
void Insert_Head(LinkList head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = Create_Linkist(elem);
temp->next = head->next;
head->next = temp;
}
}
无序插入表尾插入 (q 为表尾指针)
void Insert_Tail(LinkList &head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = head;
while(temp->next) temp = temp->next;
temp->next = Create_Linkist(elem);;
}
}
有序插入操作递归实现
void InsertSorted(LinkList &head, int elem){
if(head == NULL || head->value >= elem){
LinkList temp = Create_Linkist(elem);
temp->next = head;
head = temp;
}
else InsertSorted(head->next, elem);
}
打印函数
void print(LinkList head){
while(head){
cout<<head->value<<" ";
head = head->next;
}
}
源代码:
#include <iostream>
#include <stdlib.h>
using namespace std; #define ElemType int
typedef struct LinkNode{
ElemType value;
struct LinkNode* next;
}*LinkList; LinkList Create_Linkist(int elem){
LinkList head;
head = (LinkList)malloc(sizeof(LinkNode));
if(!head) exit(EXIT_FAILURE);
head->value = elem;
head->next = NULL;
return head;
} void Insert_Head(LinkList head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = Create_Linkist(elem);
temp->next = head->next;
head->next = temp;
}
} void Insert_Tail(LinkList &head, int elem){
if(!head) head = Create_Linkist(elem);
else{
LinkList temp = head; while(temp->next) temp = temp->next;
temp->next = Create_Linkist(elem);;
}
} void InsertSorted(LinkList &head, int elem){
if(head == NULL || head->value >= elem){
LinkList temp = Create_Linkist(elem);
temp->next = head;
head = temp;
}
else InsertSorted(head->next, elem);
} void print(LinkList head){
while(head){
cout<<head->value<<" ";
head = head->next;
}
} int main()
{
LinkList linkTest;
int testNum;
cout<<"enter test number: ";
cin>>testNum;
linkTest = Create_Linkist(testNum);
print(linkTest);
cout<<linkTest<<endl;
InsertSorted(linkTest, --testNum);
//Insert_Tail(linkTest, ++testNum);
//Insert_Head(linkTest, ++testNum);
cout<<"link after insert: "<<endl;
print(linkTest); system("pause");
return 0;}
版权声明:本文为博主原创文章,未经博主允许不得转载。
LinkList Operation的更多相关文章
- 终端mysql Operation not permitted错误解决方案
前言 前段时间装mysql,就遇到了ln: /usr/bin/mysql: Operation not permitted的错误,网上好多方法都过时了,下边是我的解决方法 原因 这是因为苹果在OS X ...
- SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...
- Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b error: Illegal ...
- TNS-12535: TNS:operation timed out案例解析
一数据库突然连接不上,在自己电脑上使用SQL Developer也连接不上.立即使用SecureCRT连接上了这台服务器,从下面几个方面检查. 1:检查了数据库的状态是否正常 $ sqlplus / ...
- 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted
1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...
- the user operation is waiting
eclipse在编辑完代码保存的时候,弹出一个进度框,等N长时间,标题是"user operation is waiting",里面显示的是building workspace的进 ...
- Python安装pywinauto时遇到error: The read operation timed out解决方法
Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- windows下安装kibana出 "EPERM: operation not permitted
D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...
随机推荐
- Thml 小插件8 天气插件定制
网址:http://www.tianqi.com/plugin/
- Android网络框架技术
网络相关1. Asynchronous Http Client for Android Android异步Http请求项目地址:https://github.com/loopj/android-asy ...
- 宣布正式发布 Windows Azure Notification Hub,新增 SQL Server AlwaysOn 可用性组侦听器支持
今天,我们非常高兴地宣布,针对使用 Windows Azure 的移动和企业开发人员推出一些新功能.这些新功能可以减少构建移动应用程序的开发时间和成本,并能帮助企业开发人员实现高可用性和全球业务连续性 ...
- oracle 11g RAC Grid Infrastructure
grid infrastructure 软件介质下载: http://www.oracle.com/technetwork/database/database-technologies/cluster ...
- ios 中生成二维码和相册中识别二维码
iOS 使用CIDetector扫描相册二维码.原生扫描 原生扫描 iOS7之后,AVFoundation让我们终于可以使用原生扫描进行扫码了(二维码与条码皆可)AVFoundation可以让我们从设 ...
- 第八届河南省赛D.引水工程(kruthcra+prime)
D.引水工程 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 118 Solved: 41 [Submit][Status][Web Board] D ...
- 吝啬的国度(dfs+vector)
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...
- 315M无线发射模块天线的长度计算
波长=光速/频率=300/315=0.952米 1/4波长须要的天线长度=波长*1/4=0.952/4=0.238米 考虑导线传播高频信号的缩短率在0.98左右,因此天线长度=0.238*0.98=0 ...
- jsp 有哪些动作?作用分别是什么?
答:JSP 共有以下 6 种基本动作jsp:include: 在页面被请求的时候引入一个文件.jsp:useBean: 寻找或者实例化一个 JavaBean.jsp:setProperty: 设置 J ...
- No orientation specified, and the default is
链接地址:http://jingyan.baidu.com/article/a24b33cd7722dc19fe002bd0.html No orientation specified, and th ...