单链表的实现及其一些简单功能

单链表的实现

#include<iostream>
using namespace std;

typedef struct LNode {
	int data;
	LNode* next;
}ListNode;

void CreatListF(ListNode*& L, int n, int a[]);//头插法建立链表
void CreatListR(ListNode*& L, int n, int a[]);//尾插法建立链表
void DestroyList(ListNode*& L);//销毁链表
int ListLength(ListNode* L);//求链表的长度
void DispList(ListNode* L);//输出线性表
bool GetElem(ListNode* L, int& e, int i);//按某个数据值查找
int LocateElem(ListNode*& L, int i, int e);//按元素值查找
bool ListInsert(ListNode*& L, int i, int e);//插入元素
bool ListDelete(ListNode*& L, int i, int& e);//删除数据元素
void UpOrder(ListNode*& L);//对链表进行递增排序
void Dorder(ListNode*& L);//对链表进行递减排序


int main() {
	int a[10] = { 1,3,2,4,6,5,8,9,7,0 };
	ListNode* L = NULL;
	//ListNode* R;
	CreatListF(L, 10, a);
	//CreatListR(R,10,a);
	DispList(L);
	//DispList(R);

	UpOrder(L);
	DispList(L);

	Dorder(L);
	DispList(L);

	DestroyList(L);
	return 0;
}

void CreatListF(ListNode*& L, int n, int a[]) {//头插
	ListNode* s;
	int i = 0;
	L = (ListNode*)malloc(sizeof(ListNode));
	L->next = NULL;
	while (i < n)
	{
		s = (ListNode*)malloc(sizeof(ListNode));
		s->data = a[i];
		s->next = L->next;
		L->next = s;
		i++;
	}
}

void CreatListR(ListNode*& L, int n, int a[])//尾插法 
{
	ListNode* s, * r;
	int i = 0;
	L = (ListNode*)malloc(sizeof(ListNode));
	r = L;
	while (i < n)
	{
		s = (ListNode*)malloc(sizeof(ListNode));
		s->data = a[i];
		r->next = s;
		r = s;
		i++;
	}
	r->next = NULL;
}

void DestroyList(ListNode*& L)//销毁链表
{
	ListNode* pre, * p;
	pre = L, p = L->next;
	while (p != NULL) {
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}

int ListLength(ListNode* L) {//统计链表的长度
	ListNode* p;
	int n = 0;
	p = L->next;
	while (p != NULL) {
		n++;
		p = p->next;
	}
	return (n);
}

void DispList(ListNode* L) {//输出线性表
	ListNode* p = L->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}

bool GetElem(ListNode* L, int& e, int i) {//按位置查找
	int j = 0;
	ListNode* p = L;
	if (i <= 0)
		return false;
	while (p != NULL && j < i) {
		j++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
		e = p->data;
	return true;
}

int LocateElem(ListNode*& L, int e)//按元素值查找
{
	ListNode* p = L->next;
	int i = 1;
	while (p->data != e && p != NULL) {
		i++;
		p = p->next;
	}
	if (p == NULL)
		return 0;
	else
		return i;
}

bool ListInsert(ListNode*& L, int i, int e)//插入元素
{
	int j = 0;
	ListNode* p = L, * s;
	if (i <= 0)
		return false;
	while (p != NULL && j < i - 1) {
		j++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		s = (ListNode*)malloc(sizeof(ListNode));
		s->data = e;
		s->next = p->next;
		p->next = s;
		return true;
	}
}

bool ListDelete(ListNode*& L, int i, int& e)//删除数据元素
{
	ListNode* p = L;
	int j = 0;
	if (i <= 0)
		return false;
	while (j < i - 1 && p != NULL) {
		j++;
		p = p->next;
	}
	if (p == NULL)
		return false;
	else
	{
		ListNode* q = p->next;
		if (q == NULL)
			return false;
		e = q->data;
		p->next = q->next;
		free(q);
		return true;
	}
}

void UpOrder(ListNode*& L) {//递增排序
	ListNode* p,*q,*pre;
	p = L->next->next;
	L->next->next= NULL;
	while (p!=NULL) 
	{
		q = p->next;
		pre = L;
		while(pre->next!=NULL&&pre->next->data<p->data) 
		{
			pre = pre->next;
		}
		p->next = pre->next;
		pre->next = p;
		p = q;
	}

}

void Dorder(ListNode*& L) {//递减排序
	ListNode* p, * pre, * q,*r;
	p=L->next->next;
	L->next->next = NULL;
	while (p != NULL ) {
		pre = L;
		q = p->next;
		while (pre->next!=NULL&&pre->next->data>p->data) {
			pre = pre->next;
		}
		p->next = pre->next;
		pre->next = p;
		p = q;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值