单向循环链表(C实现)

单向循环链表(C实现)

#include <iostream>
#include <cstdlib>

using namespace std;
typedef int ElemType;

typedef struct CLNode{
	ElemType data;
	struct CLNode *next;
}CLNode,*CLinkList;

bool InitCreateList(CLinkList *list);
bool InsertList(CLinkList list,int insloc);
bool DeleteList(CLinkList list,int delloc);
bool SearchList(CLinkList list,int searloc);
bool ShowList(CLinkList list);
int LengthList(CLinkList list);
void ShowMenu();

int main()
{
	CLNode *list=NULL,*target=NULL;
	CLNode *head=(CLinkList)malloc(sizeof(CLNode));
	cout.width(31);
	cout << "Create_Circular_LinkList.\n";

	ShowMenu();
	char choice;
	int delloc,insloc,count,searloc;
	while(cin >> choice){
		if(!cin)
		{
			cout << "Please do that menu:)\n";
			exit(EXIT_FAILURE);
		}
		switch(choice)
		{
			case '1':	cout << "Please enter numbers to create the list(0 to quit).\n";
						InitCreateList(&list);break;
			case '2':	count=LengthList(list);
						cout << "Your List length: " << count << endl;
						break;
			case '3':	cout << "Please enter a number you want to delete(1~20): ";
						cin >> delloc;
						DeleteList(list,delloc);break;
			case '4':	cout << "Please enter a place&number you want to insert(place:1~20): ";
						cin >> insloc;
						InsertList(list,insloc);break;
			case '5':	cout << "Your list is: ";
						ShowList(list);break;
			case '6':	cout << "Please enter a locate you want to search: ";
						cin >> searloc;
						SearchList(list,searloc);break;
			default :	return 0;
		}
		cout << "\nPlease enter a number which you want to do: ";
	}
	return 0;
}

void ShowMenu()
{
	cout << "Press 1 to create a circularlist.\n";
	cout << "Press 2 to check your list length.\n";
	cout << "Press 3 to delete.\n";
	cout << "Press 4 to insert.\n";
	cout << "Press 5 to check your list.\n";
	cout << "Press 6 to search node.\n";
	cout << "Press q to quit.";
	cout << "\nPlease enter a number which you wang to do: ";
}

bool InitCreateList(CLinkList *list)
{
	
	ElemType item;
	CLNode *head,*target=NULL;
	while(cin >> item){
		if(item==0)
			return true;
		if(*list == NULL)
		{
			head = (CLinkList)malloc(sizeof(CLNode));
			if(head==NULL)
				return false;
			*list = head;
			CLNode *temp = (CLinkList)malloc(sizeof(CLNode));
			temp->data = item;
			temp->next = head;
			head->next = temp;
		}
		else
		{
			for(target=*list; target->next != *list; target=target->next);
			head = *list;
			CLNode *temp=(CLinkList)malloc(sizeof(CLNode));
			temp->data = item;
			target->next = temp;
			temp->next = head;
		}
	} 
	return true;
} 

bool InsertList(CLinkList list,int insloc)
{
	
	int insitem;
	cout << "please enter a number: ";
	cin >> insitem;
	if(list == NULL)
		return false;
	CLNode *pins,*pinstemp;
	int ans=1;
	pins=list;
	while(pins->next!=list && ans<insloc){
		pins=pins->next;
		++ans;
	}
	if(ans==insloc)
	{
		pinstemp=(CLinkList)malloc(sizeof(CLNode));
		pinstemp->data = insitem;
		pinstemp->next = pins->next;
		pins->next = pinstemp;
	}
	else
	{
		cout << "Error.\n";
		return false;
	}
	return true;
}

bool DeleteList(CLinkList list,int delloc)
{

	if(list==NULL)
		return false;
	CLNode *pdel,*pdeltemp;
	int ans=1;
	pdel=list;
	while(pdel->next!=list && ans<delloc){
		pdel=pdel->next;
		++ans;
	}
	if(ans==delloc)
	{
		pdeltemp=pdel->next;
		pdel->next = pdeltemp->next;
		delete(pdeltemp);
	}
	else
	{
		cout << "Error.\n";
		return false;
	}
	return true;
}

bool SearchList(CLinkList list,int searloc)
{
	
	if(list==NULL)
		return false;
	CLNode *psear=list;
	int ans=1;
	while(psear->next!=list && ans<searloc){
		psear=psear->next;
		++ans;
	}
	if(ans==searloc)
	{
		cout << "Your searched the " << searloc << " is " << psear->next->data; 
	}
	else
	{
		cout << "Not found.\n";
		return false;
	}
	return true;
}

int LengthList(CLinkList list)
{
	if(list==NULL)
		return false;
	CLNode *plen=list->next;
	int count = 0;
	while(plen->next!=list->next){
		plen=plen->next;
		count++;
	}
	return count;
}

bool ShowList(CLinkList list)
{
	if(list==NULL)
	{
		cout << "ShowList_Error.\n";
		return 0;
	}
	CLNode *pshow=list->next;
	while(pshow->next!=list->next){
		cout << pshow->data << ' ';
		pshow=pshow->next;
	}
}



单向循环链表简易实现,一位链表初学者

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值