文档章节

205LinkList

把南墙撞开
 把南墙撞开
发布于 2016/05/11 09:47
字数 839
阅读 20
收藏 0

注意:要严格按照后缀名新建文件。

如果按.h创建文件,后来简单重命名为.cpp文件,编译会出错。

顺序表的实现 包含6个文件:

    c1.h 是预处理指令;

    elemtype.h  定义Elemtype数据类型;

    c2-2.h 是LinkList的数据结构;

    bo-nohead.cpp 是SqList的基本操作函数(basic operations 缩写为 bo);

    function.h 定义bo2-2.cpp中,链表遍历函数ListTraverse所需要的访问函数;

    main.cpp 是实现、测试函数。

//c1.h
#include<iostream>
#include<process.h>
#include<malloc.h>

#define OK 1
#define ERROR 0
#define INFEASIBLE -1

typedef int Status;
//elemtype.h
typedef int ElemType;
//c2-2.h
#ifndef C2_2_H
#define C2_2_H

#include"elemtype.h"
struct LNode
{
	ElemType data;
	LNode *next;
};

typedef LNode* LinkList;

void InitList(LinkList &L);
void ListTraverse(LinkList L, void(*vi)(ElemType));
Status ListInsert(LinkList &L, int index, ElemType e);
Status ListDelete(LinkList &L, int index, ElemType &e);
bool ListEmpty(LinkList L);
int ListLength(LinkList L);
Status GetElem(LinkList L, int index, ElemType &e);
int LocateElem(LinkList L, ElemType e);
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e);
Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e);
void ClearList(LinkList &L);
void DestroyList(LinkList &L);

#endif
//bo-nohead.cpp
#include"c1.h"
#include"c2-2.h"
using namespace std;

void InitList(LinkList &L)
{
	L = nullptr;
}

void ListTraverse(LinkList L, void(*vi)(ElemType))
{
	int i = 0;
	if (L)
	{
		LinkList p = L;
		while (p)
		{
			vi(p->data);
			p = p->next;
			i++;
			if (!(i % 10))
				cout << endl;
		}
		cout << endl;
	}
}

Status ListInsert(LinkList &L, int index, ElemType e)
{
	int j = 1;
	LinkList p = L, s;

	if (index < 1)
		return ERROR;

	s = (LinkList)malloc(sizeof(LNode));
	s->data = e;

	if (index == 1)
	{
		s->next = L;
		L = s;
	}
	else
	{
		while (p && j < index - 1)
		{
			p = p->next;
			j++;
		}
		if (!p)
			return ERROR;
		s->next = p->next;
		p->next = s;
	}
	return OK;
}

Status ListDelete(LinkList &L, int index, ElemType &e)
{
	int j = 1;
	LinkList p = L, q;
	if (index == 1)
	{
		L = p->next;
		e = p->data;
		free(p);
		return OK;
	}
	else
	{
		while (p->next && j < index - 1)
		{
			p = p->next;
			j++;
		}
		if (!p->next || j > index - 1)
			return ERROR;
		q = p->next;
		p->next = q->next;
		e = q->data;
		free(q);
		return OK;
	}
}

bool ListEmpty(LinkList L)
{
	if (L)
		return false;
	else
		return true;
}

int ListLength(LinkList L)
{
	int i = 0;
	LinkList p = L;
	while (p)
	{
		i++;
		p = p->next;
	}
	return i;
}

Status GetElem(LinkList L, int index, ElemType &e)
{
	if (index < 1)
		return ERROR;
	int j = 1;
	LinkList p = L;
	while (p && j < index)
	{
		p = p->next;
		j++;
	}

	if (!p)
		return ERROR;
	e = p->data;
	return OK;
}

int LocateElem(LinkList L, ElemType e)
{
	int i = 0;
	LinkList p = L;
	while (p)
	{
		i++;
		if (p->data == e)
			return i;
		p = p->next;
	}
	return 0;
}

/*
//this function is not efficient
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
int location = LocateElem(L, cur_e);
if (location > 1)
{
GetElem(L, location - 1, pre_e);
return OK;
}
else
return ERROR;
}

Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e)
{
int location = LocateElem(L, cur_e);
if (location >= 1 && location < ListLength(L))
{
GetElem(L, location + 1, next_e);
return OK;
}
else
return ERROR;
}
*/
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
	LinkList p = L, q ;
	while (p->next)
	{
		q = p->next;
		if (q->data == cur_e)
		{
			pre_e = p->data;
			return OK;
		}
		p = q;	
	}
	return ERROR;
}

Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e)
{
	if (L)
	{
		LinkList p = L;
		while (p->next)
		{
			if (p->data == cur_e)
			{
				next_e = p->next->data;
				return OK;
			}
			p = p->next;
		}
	}
	return ERROR;
}

void ClearList(LinkList &L)
{
	LinkList p;
	while (L)
	{
		p = L;
		L = L->next;
		free(p);
	}

}

void DestroyList(LinkList &L)
{
	LinkList q;
	while (L)
	{
		q = L->next;
		free(L);
		L = q;
	}
}
//function.h
#ifndef FUNCTION_CPP
#define FUNCTION_CPP

#include"c1.h"
#include"elemtype.h"
using namespace std;

void print(ElemType e)
{
	cout << e << " ";
}

#endif
//main.cpp
#include "c1.h"
#include "c2-2.h"
#include "function.h"
using namespace std;

int main()
{
	LinkList L;
	InitList(L);
	cout << "ListEmpty: " << ListEmpty(L) << endl;
	cout << "ListLength: " << ListLength(L) << endl;
	
	for (int i = 0; i < 100; i++)
		cout << ListInsert(L, i + 1, i);
	cout << endl;
	ListTraverse(L, print);
	cout << "ListEmpty: " << ListEmpty(L) << endl;
	cout << "ListLength: " << ListLength(L) << endl;
	cout << endl;

	ElemType e;
	ListDelete(L, 1, e);
	ListDelete(L, 97, e);
	ListTraverse(L, print);
	cout << endl;

	cout << "ListLength: " << ListLength(L) << endl;

	cout << "GetElem: " << GetElem(L, 98, e);
	cout << " e = " << e << endl;

	cout << "LocateElem: " << LocateElem(L, 99) << endl;

	cout << "PriorElem: " << PriorElem(L, 99, e);
	cout << "  e = " << e << endl;

	cout << "NextElem: " << NextElem(L, 98, e);
	cout << "  e = " << e << endl;

	ClearList(L);
	DestroyList(L);

	cin.get();
	return 0;
}

© 著作权归作者所有

共有 人打赏支持
把南墙撞开
粉丝 0
博文 73
码字总数 21068
作品 0
昌平

暂无文章

工作日志 ,城市ip地址整理

INSERT INTO sSELECT a.city,b.startip,b.endip FROM cityinfo a JOIN t_ip bON b.cheng LIKE CONCAT("%" ,a.city ,"%")ORDER BY a.cityHAVING COUNT(a.city)<=5ORD......

haifei2017
2分钟前
0
0
cdn是否可以抵御ddos的攻击

随着近年来来网络技术的不断进步,cdn不仅可以简单的用做网站加速,还能够更好的保护网站不被攻击。cdn在相关节点中成功的建立动态加速机制以及智能沉于等机制,能够帮助网站流量访问分配到每...

上树的熊
8分钟前
1
0
Oracle修改字符集

1.cmd下,cd到oracle数据库软件的服务器端 如:D:\app\Administrator\product\11.2.0\dbhome_1\BIN 2.输入set ORACLE_SID=你想进入的数据库的那个sid 3.输入 sqlplus /nolog 4.将数据库启动到...

lyle_luo
15分钟前
0
0
Xamarin Essentials教程打开文件

Xamarin Essentials教程打开文件 FileSystem类的OpenAppPackageFileAsync()方法可以用来打开App包中特定的文件,其语法形式如下: public static System.Threading.Tasks.Task<System.IO.St...

大学霸
26分钟前
0
0
Redis应用之分布式锁(set)

Redis应用之分布式锁(set) 在单机应用的场景下,我们常使用的锁主要是synchronized与Lock;但是在分布式横行的大环境下,显然仅仅这两种锁已经无法满足我们的需求; 需求:秒杀场景下,有若干...

GMarshal
39分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部