【考研-数据结构】线性表

【考研-数据结构】线性表

1.顺序表

静态空间
//静态顺序表
#include <iostream>
using namespace std;
#define Maxsize 10
typedef struct list {
	int data[Maxsize];
	int length;
}list;
//初始化
void Init(list& l) {
	for (int i = 0; i < Maxsize; i++) {
		l.data[i] = 0;
	}
	l.length = 0;
}
//插入操作
bool list_insert(list& l, int i, int e) {
	if (i<1 || i>l.length + 1) 
		return false;
	if (l.length >= Maxsize) 
		return false;
	for (int j = l.length; j >= i-1; j--) {
		l.data[j + 1] = l.data[j];
	}
	l.data[i - 1] = e;
	l.length++;
	return true;
}
//删除操作
bool list_delete(list& l, int i, int& e) {
	if (i<1 || i>l.length) 
		return false;
	e = l.data[i - 1];
	for (int j = i; j < l.length; j++) {
		l.data[j - 1] = l.data[j];
	}
	l.length--;
	return true;
}
//按位置查找
int list_locate(list& l, int i) {
	if (i<1 || i>l.length) 
		return -1;
	return l.data[i - 1];
}
//按数值查找
int list_search(list& l, int e) {
	for (int j = 0; j < l.length; j++) {
		if (l.data[j] == e) 
			return j+1;
	}
	return 0;
}
//打印顺序表
void list_print(list l) {
	for (int i = 0; i < l.length; i++) {
		cout << l.data[i] << endl;
	}
}
int main() {
	list l;
	int e = -1;
	Init(l);  //初始化
	list_insert(l, 1, 1);  //插入
	list_insert(l, 2, 2);
	list_insert(l, 3, 3);
	list_insert(l, 4, 4);
	list_insert(l, 5, 5);
	int a = list_locate(l, 2);  //按位置查找
	int b = list_search(l, 3);  //按值查找
	list_delete(l, 2,e);  //删除
	list_print(l);
	return 0;
}
动态空间
#include <iostream>
using namespace std;
#define Initsize 10
typedef struct list {
	int *data;
	int Maxsize;
	int length;
}list;
// 初始化
void Init_list(list& l) {
	l.data = new int[10];
	l.length = 0;
	l.Maxsize = Initsize;
}
//扩大最大容量
void add_list_size(list &l, int n) {
	int* p = l.data;
	l.data = new int[l.Maxsize + n];
	for (int i = 0; i < l.length; i++) {
		l.data[i] = p[i];
	}
	l.Maxsize = l.Maxsize + n;
	delete p;
}
//插入操作
bool list_insert(list& l, int i, int e) {
	if (i<1 || i>l.length + 1)
		return false;
	if (l.length >= l.Maxsize)
		return false;
		//add_list_size(l, 1);
	for (int j = l.length; j >= i - 1; j--) {
		l.data[j + 1] = l.data[j];
	}
	l.data[i - 1] = e;
	l.length++;
	return true;
}
//删除操作
bool list_delete(list& l, int i, int& e) {
	if (i<1 || i>l.length)
		return false;
	e = l.data[i - 1];
	for (int j = i; j < l.length; j++) {
		l.data[j - 1] = l.data[j];
	}
	l.length--;
	return true;
}
//按位置查找
int list_locate(list l, int i) {
	if (i<1 || i>l.length)
		return -1;
	return l.data[i - 1];
}
//按数值查找
int list_search(list l, int e) {
	for (int j = 0; j < l.length; j++) {
		if (l.data[j] == e)
			return j + 1;
	}
	return 0;
}
//打印顺序表
void list_print(list l) {
	for (int i = 0; i < l.length; i++) {
		cout << l.data[i] << endl;
	}
}
int main() {
	list l;
	Init_list(l);
	add_list_size(l, 1); 
	list_insert(l, 1, 1);
	list_insert(l, 1, 2);
	list_insert(l, 1, 3);
	list_insert(l, 1, 4);
	list_insert(l, 1, 5);
	list_insert(l, 1, 6);
	list_insert(l, 1, 7);
	list_insert(l, 1, 8);
	list_insert(l, 1, 9);
	list_insert(l, 1, 10);
	list_insert(l, 1, 11);
	list_print(l);
	cout << l.Maxsize<<endl;
	list_insert(l, 12, 12);
	cout << l.Maxsize << endl;
	return 0;
}

2.链表

单链表
//单链表
#include <iostream>
using namespace std;
typedef struct LNode {
	int data;
	LNode* next;
}LNode,*LinkList;
//头插法建立单链表(有头结点)
LinkList List_HeadInsert(LinkList &l) {
	int x;
	LNode* s;
	l = new LNode();
	//l = (LinkList)malloc(sizeof(LNode));
	l->next = NULL;
	cin >> x;
	while (x != 9999) {
		s = new LNode();
		s->data = x;
		s->next = l->next;
		l->next = s;
		cin >> x;
	}
	return l;
}

//头插法建立单链表(没有头结点)
LinkList List_HeadInsert_2(LinkList& l) {
	int x;
	LNode* s;
	cin >> x;
	while (x != 9999) {
		s = new LNode();
		s->data = x;
		s->next = l;
		l = s;
		cin >> x;
	}
	return l;
}

//尾插法建立单链表(有头结点)
LinkList List_TailInsert(LinkList& l) {
	int x;
	l = new LNode();
	LNode* s;
	LNode* r = l;
	cin >> x;
	while (x != 9999) {
		s =new LNode();
		s->data = x;
		r->next = s;
		r = s;
		cin >> x;
	}
	r->next = NULL;
	return l;
}

//尾插法建立单链表(没有头结点)
LinkList List_TailInsert_2(LinkList& l) {
	int x;
	l = new LNode();
	LNode* s;
	LNode* r = l;
	cin >> x;
	if (x != 9999) {
		l = new LNode();
		l->data = x;
	}
	cin >> x;
	while (x != 9999) {
		s = new LNode();
		s->data = x;
		r->next = s;
		r = s;
		cin >> x;
	}
	r->next = NULL;
	return l;
}
//按序号查找结点(有头结点)
LNode* Get_Elem(LinkList l, int i) {
	int j = 1;
	LNode* temp = l->next;  //temp指向第一个结点
	if (i == 0) return l;
	if (i < 0) return nullptr;
	while (j<i&&temp!=nullptr) {
		temp = temp->next;
		j++;
	}
	return temp;  //如果i大于单链表长度,返回nullptr
}
//按结点值查找结点(有头结点)
LNode* Locate_Elem(LinkList l, int e) {
	LNode* temp = l->next;
	while (temp->data != e&&temp!=nullptr) {
		temp = temp->next;
	}
	return temp;
}
//插入结点操作(有头结点)
//另一种方法:使用Get_Elem找到第i个结点,
//           然后在此节点之后插入新节点,
//           最后将此节点与新节点的数据域调换
bool List_Insert(LinkList& l, int i, int e) {
	if (i < 1) return false;
	LNode* re = Get_Elem(l,i - 1);
	LNode* s = new LNode();
	s->data = e;
	s->next = re->next;
	re->next = s;
	return true;
}

//删除结点操作(有头结点)
bool List_Delete(LinkList& l, int i) {
	if (i < 1) return false;
	LNode* re = Get_Elem(l, i - 1);
	LNode* node_i = re->next;
	re->next = node_i->next;
	delete node_i;
	return true;
}

//求单链表表长(有头结点)
int List_length(LinkList l) {
	int len = 0;
	LNode* temp = l->next;
	while (temp != nullptr) {
		temp = temp->next;
		len++;
	}
	return len;
}

//打印单链表
void print(LinkList l) {
	LNode* temp = l->next;
	while (temp != nullptr) {
		cout << temp->data << endl;
		temp = temp->next;
	}
}

int main() {
	LinkList l;
	List_HeadInsert(l);
	// cout << l->next->data<<endl;
	// cout << Get_Elem(l, 2)->data << endl;
	// cout << Locate_Elem(l, 1)->data << endl;
	cout<<List_length(l)<<endl;
	// List_Insert(l, 1, 10);
	// print(l);

	// List_HeadInsert_2(l);
	// cout << l->next->data << endl;

	LinkList t;

	// List_TailInsert(t);
	// cout << t->data << endl;

	// List_TailInsert_2(t);
	// cout << t->data << endl;

	return 0;
}
双链表
#include <iostream>
using namespace std;
typedef struct DLNode {
	int data;
	DLNode* pre;
	DLNode* next;
}LNode, * DLinkList;

//初始化
bool InitDLinkList(DLinkList& l) {
	l = new LNode();
	if (l == nullptr) return false;
	l->pre = nullptr;
	l->next = nullptr;
	return true;
}

//创建双链表
bool DLinkList_Create(DLinkList& l) {
	int x;
	DLNode* s;
	cin >> x;
	while (x != 9999) {
		s = new DLNode();
		s->data = x;
		s->next = l->next;
		if (l->next != nullptr) {
			l->next->pre = s;
		}
		s->pre = l;
		l->next = s;
		cin >> x;
	}
	return true;
}

//插入操作(在p结点之后插入s结点)
bool DLinkList_Insert(LNode* p, LNode* s) {
	if (p == nullptr || s == nullptr) return false;
	s->next = p->next;
	if (p->next != nullptr) {
		p->next->pre = s;
	}
	s->pre = p;
	p->next = s;
	return true;
}

//删除操作(删除p结点的后继结点)
bool DLinkList_Delete(LNode* p) {
	if (p == nullptr) return false;
	LNode* s = p->next;
	if (s == nullptr) return false;
	p->next = s->next;
	s->next->pre = p;
	delete s;
	return true;
}

// 打印双链表
void DLinkList_Print(DLinkList l) {
	DLNode* pri = l->next;
	while (pri != nullptr) {
		cout << pri->data << endl;
		pri = pri->next;
	}
}

int main() {
	DLinkList l;
	InitDLinkList(l);
	DLinkList_Create(l);
	DLinkList_Print(l);
	cout << "----" << endl;
	DLNode* temp = new DLNode();
	temp->data = 10;
	DLinkList_Insert(l->next, temp);
	DLinkList_Print(l);
	cout << "----" << endl;

	/*DLinkList_Delete(l->next);
	DLinkList_Print(l);*/
	return 0;
}
循环单链表
#include <iostream>
using namespace std;
typedef struct CLNode {
	int data;
	CLNode* next;
}CLNode, * CLinkList;

//初始化
bool Init_CLinkList(CLinkList& l) {
	l = new CLNode();
	if (l == nullptr) return false;
	l->next = l;
	return true;
}

//头插法建立循环单链表(有头结点)
CLinkList List_HeadInsert(CLinkList& l) {
	int x;
	CLNode* s;
	l = new CLNode();
	//l = (LinkList)malloc(sizeof(LNode));
	l->next = NULL;
	cin >> x;
	if (x != 9999) {
		s = new CLNode();
		s->data = x;
		s->next = l->next;
		l->next = s;
		s->next = l;
	}
	cin >> x;
	while (x != 9999) {
		s = new CLNode();
		s->data = x;
		s->next = l->next;
		l->next = s;
		cin >> x;
	}
	return l;
}

//尾插法建立循环单链表(有头结点)
CLinkList List_TailInsert(CLinkList& l) {
	int x;
	l = new CLNode();
	CLNode* s;
	CLNode* r = l;
	cin >> x;
	while (x != 9999) {
		s = new CLNode();
		s->data = x;
		r->next = s;
		r = s;
		r->next = l;
		cin >> x;
	}
	r->next = NULL;
	return l;
}

//按序号查找结点(有头结点)
CLNode* Get_Elem(CLinkList l, int i) {
	int j = 1;
	CLNode* temp = l->next;  //temp指向第一个结点
	if (i == 0) return l;
	if (i < 0) return nullptr;
	while (j < i && temp != l && temp != nullptr) {
		temp = temp->next;
		j++;
	}
	return temp;  //如果i大于单链表长度,返回nullptr
}
//按结点值查找结点(有头结点)
CLNode* Locate_Elem(CLinkList l, int e) {
	CLNode* temp = l->next;
	while (temp->data != e && temp != l&&temp!=nullptr) {
		temp = temp->next;
	}
	return temp;
}
//插入结点操作(有头结点)
bool List_Insert(CLinkList& l, int i, int e) {
	if (i < 1) return false;
	CLNode* re = Get_Elem(l, i - 1);
	CLNode* s = new CLNode();
	s->data = e;
	s->next = re->next;
	re->next = s;
	return true;
}

//删除结点操作(有头结点)
bool List_Delete(CLinkList& l, int i) {
	if (i < 1) return false;
	CLNode* re = Get_Elem(l, i - 1);
	CLNode* node_i = re->next;
	re->next = node_i->next;
	delete node_i;
	return true;
}

//求循环单链表表长(有头结点)
int List_length(CLinkList l) {
	int len = 0;
	CLNode* temp = l->next;
	while (temp != l&&temp!=nullptr) {
		temp = temp->next;
		len++;
	}
	return len;
}

//打印循环单链表
void print(CLinkList l) {
	CLNode* temp = l->next;
	while (temp != nullptr) {
		cout << temp->data << endl;
		temp = temp->next;
	}
}

//判断链表是否为空
bool Empty(CLinkList l) {
	if(l->next == l){
		return true;
	}else{
		return false;
	}
}

int main() {
	CLinkList l;
	Init_CLinkList(l);
	List_TailInsert(l);

	cout << Get_Elem(l, 1)->data << endl;
	cout << "---" << endl;
	cout << Locate_Elem(l, 1)->data << endl;
	cout << "---" << endl;
	cout << List_length(l) << endl;
	cout << "---" << endl;
	List_Insert(l, 2, 10);
	cout << "---" << endl;
	List_Delete(l, 1);
	cout << "--" << endl;
	cout << List_length(l) << endl;
	cout << "---" << endl;
	print(l);
	return 0;
}
循环双链表
#include <iostream>
using namespace std;
typedef struct CDLNode {
	int data;
	CDLNode* pre;
	CDLNode* next;
}CDLNode, * CDLinkList;

//初始化
bool InitDLinkList(CDLinkList& l) {
	l = new CDLNode();
	if (l == nullptr) return false;
	l->pre = l;
	l->next = l;
	return true;
}

//创建循环双链表
bool DLinkList_Create(CDLinkList& l) {
	int x;
	CDLNode* s;
	cin >> x;
	while (x != 9999) {
		s = new CDLNode();
		s->data = x;
		s->next = l->next;
		l->next->pre = s;
		s->pre = l;
		l->next = s;
		cin >> x;
	}
	return true;
}

//插入操作(在p结点之后插入s结点)
bool DLinkList_Insert(CDLNode* p, CDLNode* s) {
	if (p == nullptr || s == nullptr) return false;
	s->next = p->next;
	p->next->pre = s;
	s->pre = p;
	p->next = s;
	return true;
}

//删除操作(删除p结点的后继结点)
bool DLinkList_Delete(CDLinkList l, CDLNode* p) {
	if (p == nullptr) return false;
	CDLNode* s = p->next;
	if (s == nullptr||s == l) return false;
	p->next = s->next;
	s->next->pre = p;
	delete s;
	return true;
}

// 打印循环双链表
void DLinkList_Print(CDLinkList l) {
	CDLNode* pri = l->next;
	while (pri != l) {
		cout << pri->data << endl;
		pri = pri->next;
	}
}
//判断循环双链表是否为空
bool Empty(CDLinkList l) {
	if(l->next == l&&l->pre == l) {
		return true;
	}else {
		return false;
	}
}

int main() {
	CDLinkList l;
	InitDLinkList(l);
	DLinkList_Create(l);
	DLinkList_Print(l);
	cout << "----" << endl;
	CDLNode* temp = new CDLNode();
	temp->data = 10;
	DLinkList_Insert(l->next, temp);
	DLinkList_Print(l);
	cout << "----" << endl;
	DLinkList_Delete(l,l->next);
	DLinkList_Print(l);
	return 0;
}
静态链表省略
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值