The realization of a simple single linked list【1.6】

Single linked list with initialization, analysis, insert, search by location, by numerical search, delete, traverse

#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);
		
		//void Invert();
		//friend void Merge(LinkList &L1,LinkList &L2);
	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<<"Search 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<<"Delete location illegal"<<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<<"Insert location illegal"<<endl;
	}
	else
	{
		s = new Node;
		s->data = item;
		s->next = p->next;
		p->next = s;
	}
	 
}
int main()
{
	int a[10]={16,46,413,4,64,13,1,4654,133,4661};
	LinkList list(a,10);
	cout<<"Print the list length :"<<list.ListLength()<<endl; 
	cout<<"Print the fifth element :"; list.Get(5);
	cout<<"Print the twelfth element :"; list.Get(12);
	cout<<"Print 46 position:"<<list.Locate(46)<<endl; 
	cout<<"Print the nonexistent number 2 in the list:"<<list.Locate(2)<<endl; 
	cout<<"Print the fifth number:";list.Delete(5); 
	cout<<"Print all element :"<<endl;	
	list.PrintLinkList();
	cout<<"Insert 666 into the fifth position"<<endl;
	list.Insert(5,666);
	cout<<"Print all element :"<<endl;	
	list.PrintLinkList();
	cout<<"Insert 777 into the eleventh position"<<endl;
	list.Insert(11,777);
	cout<<"Print all element :"<<endl;	
	list.PrintLinkList();
	//now the list length is 11
	cout<<"Insert 999 into the 13 position"<<endl;
	list.Insert(13,999);
	cout<<"Print all element :"<<endl;	
	list.PrintLinkList();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值