@【数据结构】(链表)

@【数据结构】(链表)

链表基本操作函数,完成带头结点的链表函数的定义
链表长度,链表元素输出,查找第i个节点的指针,指定位置插入元素, 查找某元素的链表结点位置

#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct list 
{
	int data;
	struct list *next;
}*LIST, LNode;
void InitList(LIST *L)	/* 初始化链表 */
{
	*L = (LNode *)malloc(sizeof(LNode));   //带头结点的链表初始化
	(*L)->next = NULL;
}
int Length(LIST L)    //带头结点的单链表长度
{
	LNode *p = L;   //p指向头结点
	int j = 0;
	while (p->next!=NULL)
	{
		p = p->next; j++;
	}
	return j;
}
void OutputList(LIST L)
{
	LNode *p;
	p = L->next;
	while (p)
	{
		cout << p->data << "  ";
		p = p->next;
	}
}

//*查找第i个节点的指针
LNode *Find(LIST L, int i)
{
	LNode *p = L;  
	int j = 0;
	while ((p->next != NULL) && (j < i))
	{
		p = p->next; j++;
	}
	if (i == j) return(p);
	else return NULL;
}
void InsertList1(LIST L, int item, int rc)
/* 向链表指定位置[rc]插入元素[item] */
{
	LNode *p,*s;
	p = Find(L, rc- 1);
	if (!p)
		cout << "error:without" << endl;   //参数不合法
	else
	{
		s= (LNode*)malloc(sizeof(LNode));
		s->data= item;   //创建新元素的结点
		s->next = p->next;
		p->next = s;   //修改指针
	}
}
void order(LIST L)    //排序,链表变为升序
{
	LNode *pre, *q, *l1, *l2;
	l1 = L->next;
	q = l1->next;
	l1->next = NULL;  //将原来的链L断开
	while (q)
	{
		l2 = q;
		q = q->next;
		pre = L;     //pre是待插入位置的前驱
		l1 = L->next; //l1是待插入位置的后继
		while (l1 != NULL && l1->data < l2->data) //寻找合适的插入点
		{
			pre = l1;
			l1 = l1->next;
		}
		//插入合适位置
		l2->next = l1;
		pre->next = l2;
	}
}
void InsertList2(LIST L, int item)
/* 向有序链表L插入键值为[item]的结点 */
{
	order(L);
	LNode *p,*q,*s;
	p = L; q= p->next;
	while (q&&q->data < item)  //找到插入的地方
	{
		p = p->next; q = q->next;
	}
	s = (LNode*)malloc(sizeof(LNode));
	s->data = item;
	s->next = q;
	p->next = s;
	cout << "将L变为有序表,在有序表中插入元素" << item << "后为:" << endl;
	OutputList(L);
}

/* 查找键值为[item]的链表结点位置, 返回>=1: 找到  -1: 没找到 */
int FindList(LIST L, int item)
{
	LNode *p = L;
	int j = 0;
	while ((p->next) && (p->data != item))
	{
		p = p->next; j++;
	}
	if (p->data == item) return j;
	else return -1;

}

/* 删除键值为[item]的链表结点, 返回0: 删除成功 1: 没找到 */
int DeleteList(LIST L, int item)
{
	LIST p,s;
	int i = FindList(L, item);   //查找键值为[item]的链表结点位置
	p= Find(L, i - 1);          //键值为item的结点前一个结点的指针
	if ((p != NULL) && (p->next != NULL))
	{
		s = p->next;    //指向第i个节点
	   p->next = s->next;
	   free(s);
	   return 0;
	}
	else return 1;
}

//输出链表元素 
void main()
{
	LIST L;
	
	LNode *p;
	InitList(&L); 
	p = L;
	int item;
	
	cout << "插入元素后,单链表为:" << endl;
	InsertList1(L, 24, 1);
	InsertList1(L, 47, 2);
	InsertList1(L, 15, 3);
	InsertList1(L, 33, 4);
	InsertList1(L, 62, 5);
	InsertList1(L, 41, 6);
	InsertList1(L, 22, 7);
	OutputList(L);
	cout << endl;

	item = 33;
	cout << "元素33位于第" << FindList(L, 33) << "位" << endl;

	InsertList2(L, 11);
	cout << "插入键值为11的结点后" << endl;
	OutputList(L);
	cout << endl;

	DeleteList(L, 47);
	cout << "删除元素47:" << endl;
	OutputList(L);
	cout << endl;
	system("pause");
}

注:链表中的元素是InsertList1(L, X, i)逐个插入的,链表中的元素已预定义好。
测试示例:
在这里插入图片描述

发布了20 篇原创文章 · 获赞 1 · 访问量 83

猜你喜欢

转载自blog.csdn.net/gsgs1234/article/details/104843870