C++ realizes the basic operation of singly linked list

Comprehend the storage structure of singly linked list and master the design of various basic operation algorithms in singly linked list.

Realize various basic operations of singly linked list and overall table building algorithm (assuming that the element type of singly linked list is char).

Program requirements:

1. Initialize the singly linked list h;

2. Insert a, b, c, d, e elements in sequence by using the tail insertion method;

3. Output the singly linked list h;

4. Output the length of the singly linked list h;

5. Determine whether the singly linked list h is empty;

6. According to the position input by the user, output the corresponding element of the singly linked list h;

7. According to the element input by the user, output the corresponding position of the singly linked list h;

8. According to the position and element input by the user, insert a new element into the singly linked list h;

9. Output the singly linked list h;

10. According to the position input by the user, delete the corresponding element of the singly linked list h;

11. Output the singly linked list h;

12. Release the singly linked list h;

----------------------------------------------------------------------------------------------

Define the data structure of a singly linked list

#include <iostream.h>

typedef char ElemType;
typedef struct LNode  //定义单链表结点类型
{
	ElemType data;
	struct LNode *next;
} LinkList;

1. Initialize the singly linked list

void InitList(LinkList *&L)
{
	L=new LinkList;
	L->next=NULL;
}

2. Tail insertion method to build table

The head node does not contain data and has a guiding role, and the next node of the head node starts to save data.

void CreateListR(LinkList *&L,ElemType a[],int n)
{
	LinkList *s,*r;int i;
	L=new LinkList;  //产生一个新的头结点
	r=L;  //令r指针指向终端结点,刚开始的时候指向头结点
	for(i=0;i<n;i++)
	{
		s=new LinkList;
		s->data=a[i];
		r->next=s;  //将s插入r的后面
		r=s;     //r指针指向终端结点
	}
	r->next=NULL;
}

3. Destroy the singly linked list

When only the last node is left, p points to the last node, and q == NULL. So after the loop ends, p needs to be deleted again.

void DestroyList(LinkList *&L)
{
	LinkList *p=L,*q=p->next;
	while(q!=NULL)
	{
		delete p;
		p=q;
		q=p->next;
	}
	delete p;
}

4. Determine whether it is an empty table

If the singly linked list is an empty list, return true;

Returns false if the singly linked list is not empty.

bool ListEmpty(LinkList *L)
{
	return (NULL==L->next);
}

5. Find the length of the singly linked list

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

6. Output singly linked list

void DispList(LinkList *&L)
{
	LinkList *p=L->next;
	while(p!=NULL)
	{
		cout<<p->data<<"      ";
		p=p->next;
	}
	cout<<endl;
}

7. Find the value of an element at a specified position in the singly linked list

bool GetElem(LinkList *L,int i,ElemType &e)
{
	int j=0;
	LinkList *p=L;
	while (j<i  && p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;//不存在第i个数据结点
	else
	{
		e=p->data;
		return true;
	}
}

8. Find by element value

int LocateElem(LinkList *L,ElemType e)
{
	LinkList *p=L->next;int n=1;
	while(p!=NULL && p->data!=e)
	{
		p=p->next;
		n++;
	}
	if(p==NULL)
		return 0;
	else
		return n;
}

9. Insert data elements

A data element needs to be inserted before position i, so p should point to the node position of i-1.

bool ListInsert(LinkList *&L,int i,ElemType e)
{
	int j=0;
	LinkList *p=L,*s;
	while(j<i-1 && p!=NULL)    //该循环是查找第i-1个结点
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
		return false;  //找不到第i-1个结点
	else
	{
		s=new LinkList;
		s->data=e;
		s->next=p->next;
		p->next=s;
		return true;
	}
}

10. Delete data elements

ElemType e is used to save the deleted data element, p points to the node position of i-1, and q points to the node position of i.

bool ListDelete(LinkList *&L,int i,ElemType &e)
{
	int j=0;
	LinkList *p=L,*q;
	while (j<i-1 && p!=NULL)
	{
		j++;
		p=p->next;
	}

	if(p==NULL)
		return false;
	else
	{
		q=p->next;
		if(q==NULL)
			return false;
		e=q->data;
		p->next=q->next;
		delete q;
		return true;
	}
}

----------------------------------------------------------------------------------------------

main function 

void main()
{
	LinkList *L1;
	cout<<"1.初始化顺序表:"<<endl;  InitList(L1);

	cout<<"\n2.尾插法插入元素:"<<endl;
	ElemType a[5]={'A','B','C','D','E'};
	for(int i=0;i<5;i++)
		if(!ListInsert(L1,i+1,a[i]))
			cout<<a[i]<<"插入失败!";

	cout<<"\n3.顺序表的元素为:";
	DispList(L1);

	cout<<"\n4.该顺序表的长度为:"<<ListLength(L1)<<endl;

	cout<<"\n5.该顺序表";
	if(ListEmpty(L1))
		cout<<"为空表.";
	else
		cout<<"不为空表.";

	cout<<"\n\n6.取元素";
	ElemType temp;cout<<"请输入取的位置:";int k;cin>>k;
	if(GetElem(L1,k,temp))
		cout<<"取值成功,该顺序表的第"<<k<<"个元素的值为:"<<temp<<endl;
	else
		cout<<"取值失败,你输入的位置"<<k<<"越界!"<<endl;

	cout<<"\n7.查找元素:"<<endl;
	cout<<"请输入查找元素的值:";cin>>temp;
	if(LocateElem(L1,temp))
		cout<<"输出元素"<<temp<<"的位置为"<<LocateElem(L1,temp)<<endl;
	else
		cout<<"元素"<<temp<<"不存在"<<endl;

	cout<<"\n8.在顺序表指定位置插入元素:"<<endl;
	cout<<"请输入插入的位置:";cin>>k;
	cout<<"请输入插入元素的值:";cin>>temp;

	if(ListInsert(L1,k,temp))
		cout<<"插入成功"<<endl;
	else
		cout<<"插入失败"<<endl;

	cout<<"\n9.输出顺序表"<<endl;
	DispList(L1);

	cout<<"\n10.删除指定位置的元素"<<endl;
	cout<<"请输入删除的位置:";cin>>k;
	if(ListDelete(L1,k,temp))
		cout<<"删除成功,删除的元素为:"<<temp<<endl;
	else
		cout<<"删除失败!"<<endl;

	cout<<"\n11.输出顺序表"<<endl;
	DispList(L1);

	cout<<"\n12.释放顺序表"<<endl;
	DestroyList(L1);
}

Guess you like

Origin blog.csdn.net/henry594xiaoli/article/details/123820453