The realization of a simple single linked list【1.8】

I added the menu to the previous version.

#include <iostream>
using namespace std;
struct Node
{
	int data;
	struct Node* next;
};
class LinkList
{
	public:
		LinkList();
		LinkList(int a[],int n);
		~LinkList();
		int ListLength();
		void Get(int pos);
		int Locate(int item);
		void PrintLinkList();
		void Insert(int pos,int item);
		void Delete(int i);
	
	private:
		Node *head;
		Node *rear;
		Node *next;	
		Node *p;
	    Node *q;
	    Node *s;
};
LinkList::LinkList()
{
	head = new Node;
	head->next = NULL;
}
LinkList::~LinkList()
{
	p = head->next;
	while(p)
	{
		q = p;
		p = p->next;
		delete q;
	}
	head = NULL;
}
LinkList::LinkList(int a[],int n)
{
	head = new Node;
	p = head;
	for(int i=0;i<n;++i)
	{
		s = new Node;
		s->data = a[i];
		p->next = s;
		p = s;
	}
	p->next = NULL; 
}

//here we can't use the int return type ,it will have error
//also can't use exit(1) either,it will exit the program,
//resulting in code that will not be executed

//get the pos'th number 
void LinkList::Get(int pos)
{
	int count =1;
	p = head->next;
	
	while(p&&count<pos)
	{
		count++;
		p = p->next;	
	}
	if(!p||count>pos)
	{
		cout<<"Location illegal"<<endl;
	}
	else
		cout<<p->data<<endl;
}

//get the number position in the list 
int LinkList::Locate(int item)
{
	p = head->next;
	int count = 1;
	while(p&&p->data!=item)
	{
		p = p->next;
		count++;
	}
	if(p)
		return count;
	else 
		return 0;
}

int LinkList::ListLength()
{
	p = head->next;
	int length = 0;
	while(p)
	{
		p = p->next;
		length++;
	}
	return length;
}

void LinkList::PrintLinkList()
{
	p = head->next;
	while(p)
	{
		cout<<p->data<<endl;
		p = p->next;
	}
}

//the return type argument is the same as the Get()
void LinkList::Delete(int pos)
{
	p = head;
	int count = 1;
	while(p&&count<pos)
	{
		count++;
		p = p->next;	
	}
	if(!p||count>pos)
	{
		cout<<"Invalid location"<<endl;
	}
	else
	{
		q = p->next;
		int m = q->data;
		p->next = q->next;
		delete q;
		cout<<m<<endl;
	}
}

void LinkList::Insert(int pos,int item)
{
	p = head;
	int count = 1;
	while(p&&count<pos)
	{
		count++;
		p = p->next;	
	}
	if(!p||count>pos)
	{
		cout<<"Illegal insertion location"<<endl;
	}
	else
	{
		s = new Node;
		s->data = item;
		s->next = p->next;
		p->next = s;
	}
	 
}
void menu(LinkList list)
{
	int index;
	int pos,num;
	
	while(1)
	{
	
		cout<<"-------1.Print the list-------"<<endl;
		cout<<"-------2.Insert elements--------"<<endl;
		cout<<"-------3.Look at the data of your desired location--------"<<endl;
		cout<<"-------4.View the location of the data in the list-------"<<endl;
		cout<<"-------5.Remove elements-------"<<endl;
		cout<<"-------6.Output list length-------"<<endl;
		cout<<"-------7.Pull out-------"<<endl;
		cin>>index;
		cout<<endl;
		switch(index)
		{
			case 1:
				list.PrintLinkList();
				break;
			case 2:
				cout<<"The position you want to insert:";
				cin>>pos;
				cout<<"You want to insert the data:";
				cin>>num;
				list.Insert(pos,num);
				break;
			case 3:
				cout<<"You want to find the location:";
				cin>>pos;
				list.Get(pos);
				break;
			case 4:
				cout<<"Please enter the data you want to find:";
				cin>>num;
				cout<<list.Locate(num)<<endl;
				break;
			case 5:
				cout<<"Please enter the location of the data you want to delete:";
				cin>>num;
				list.Delete(num);
				break;
			case 6:
				cout<<list.ListLength()<<endl;
				break;
			case 7:
				exit(1);

		}
	}
}

int main()
{
	int n;
	cout<<"How much data do you want to enter:";
	cin>>n;
	
	int *a = new int [10];
	for(int i = 0;i<n;++i)
	{
		cout<<"Please enter the "<<i+1<<" numeber:";
		cin>>a[i];
	}
	LinkList list(a,n);
	menu(list);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值