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:// ...
随机推荐
- PHP批量下载方法
PHP批量下载方法 界面: $.get(“< ?php echo url::base(true);?>inventory/report/buildCsv”, //后台路径 {‘para ...
- 度小于所述过程:es.exe
在防火墙管理,见未知的过程"es.exe" 程序信息: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font ...
- 自己定义控件-GifView
一.描写叙述 显示Gif 的View 原理是用 MediaPlayer 实现的 二.源代码 https://github.com/mentor811/Demo_GifView [ 声明:版权全部,欢迎 ...
- MYSQL++之Connect类型
原文转自:www.cnblogs.com/aicro mysqlpp:: Connect类型主要负责连接事宜,这是在所有开始mysql操作之前必须进行的(这是句废话). 该类型的主要的结果如下所示 m ...
- zoj 2067 White Rectangles
这题解决的算法处理,真的很难想清楚!!尤其是最后的正矩形如何处理.不过终于看懂了 #include<stdio.h> #include<stdlib.h> #include&l ...
- SQL Server2012新特性概述
公司最近要升级数据库,SQL Server 2008R2-->2012.再开始升级之前先找了点资料分析一下2012的新特性和功能,提前预热一下. 2012中主要关注一下三个领域: 性能:改进的核 ...
- 服务器表导入到本地数据库SQL语句
这是开启权限 exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed ...
- C++的常量折叠(三)
背景知识 在开始之前先说一下符号表,这个编译器中的东西.下面看一下百度百科中的描述: 符号表是一种用于语言翻译器中的数据结构.在符号表中,程序源代码中的每个标识符都和它的声明或使用信息绑定在一起,比如 ...
- 创建Xml的将但方法和向Xml中添加数据
</SendUserId>// ::</DateTime></AcceptUserId> <AcceptUserId></Accept ...
- JVM典型配置
堆大小设置: JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存 限制.32位系统下,一般限制在1.5G~2G:64为 ...