C/C++顺序表的链式表示

单链表

#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct LNode{
	int data;
	struct LNode* next;
}LNode,*LinkList;
const int MAXN = 12;
int A[MAXN];
int N = 12;
void declare(int);
LinkList List_HeadInsert(LinkList& L) {		//头插法
	for (int i = 0; i < N; i++) {
		LNode* p = (LNode*)malloc(sizeof(LNode));
		p->data = A[i];
		p->next = L->next;
		L->next = p;
	}
	return L;
}
LinkList List_TailInsert(LinkList& L) {	//尾插法
	LNode* q = L;
	for (int i = 0; i < N; i++) {
		LNode* p = (LNode*)malloc(sizeof(LNode));
		p->data = A[i];
		p->next = NULL;
		q->next = p;
		q = p;
	}
	return L;
}
LNode* GetElem(LinkList L, int i) {	//按序号查找
	if (i < 0)	return NULL;
	if (i == 0)	return L;
	int j = 1;
	LNode* p = L->next;
	while (p != NULL && j < i) {
		p = p->next;
		j++;
	}
	if (j != i)	return NULL;
	return p;
}
LNode* LocateElem(LinkList L, int e) {	//按值查找
	LNode* p = L->next;
	while (p != NULL && p->data != e)	p = p->next;
	return p;
}
void Insert(LinkList& L,int i,int e) {	//在i号位置插入data为e的节点
	LNode* q = GetElem(L, i - 1);	//插入位置的前驱节点
	LNode* p = (LNode*)malloc(sizeof(LNode));
	p->data = e;
	p->next = q->next;
	q->next = p;
}
void Delete(LinkList& L, int i) {	//删除i号节点
	LNode* q = GetElem(L, i-1);		//删除节点的前驱节点		
	LNode* p = q->next;
	q->next = p->next;
	free(p);
}
int Length(LinkList L) {			//求表长
	int len = 0;
	LNode* p = L->next;
	while (p) {
		p = p->next;
		len++;
	}
	return len;
}
void Print(LinkList L) {	//打印链表
	LNode* p = L->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
int main() {
	//declare(N);
	for (int i = 0; i < N; i++) {
		A[i] = i * 100;
	}
	LNode* head = new LNode;
	head->next = NULL;
	head = List_TailInsert(head);
	Print(head);
	Insert(head, 11, 199);
	Print(head);
	cout << Length(head) << endl;
	Delete(head, 11);
	Print(head);
	cout << Length(head) << endl;
	return 0;
}
void declare(int N) {
	cout << "插入链表的数组数据为:" << endl;
	for (int i = 0; i < N; i++) {
		A[i] = rand();
		cout << A[i];
		if (i != N - 1)	cout << " ";
	}
	cout << endl;
}

双链表

#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct DNode{
	int data;
	struct DNode* next;
	struct DNode* prior;
}DNode,*DLinkList;
DNode* GetElem(DLinkList L, int i) {		//按序号查找
	if (i < 0)	return NULL;
	if (i == 0)	return L;
	int j = 1;
	DNode* p = L->next;
	while (p != NULL and j < i) {
		p = p->next;
		j++;
	}
	return p;
}
void Insert(DLinkList& L, int i, int e) {
	DNode* q = GetElem(L, i - 1);
	DNode* p = (DNode*)malloc(sizeof(DNode));
	p->next = q->next;
	p->prior = q;
	p->data = e;
	q->next->prior = p;
	q->next = p;
}
void Delete(DLinkList& L, int i) {
	DNode* q = GetElem(L, i - 1);
	DNode* p = q->next;
	if (p->next == NULL) {
		q->next = NULL;
		free(p);
		return;
	}
	q->next = p->next;
	p->next->prior = q;
	free(p);
}
int N = 12;
int A[12];
DLinkList List_TailInsert(DLinkList& L) {	//尾插法
	DNode* q = L;
	for (int i = 0; i < N; i++) {
		DNode* p = (DNode*)malloc(sizeof(DNode));
		p->data = A[i];
		p->next = NULL;
		p->prior = q;
		q->next = p;
		q = p;
	}
	return L;
}
void Print(DLinkList L) {	//打印链表
	DNode* p = L->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
int main() {
	for (int i = 0; i < N; i++) {
		A[i] = i * 100;
	}
	DNode* head = new DNode;
	head=List_TailInsert(head);		//先用尾插法建立链表,方便后续进行测试
	Print(head);
	Insert(head, 10, 777);
	Print(head);
	Delete(head, 10);
	Print(head);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值