数据结构试验二-循环链表的基本操作

#include "stdafx.h"
#include "Function.h"

CLList createCLList(void)
{
	CLList list;
	list=(CLList)malloc(sizeof(struct clList));
	if(list==NULL)
	{
		return NULL;
	}
	list->head=(PNode)malloc(sizeof(struct CLNode));
	if(list->head==NULL)
	{
		free(list);
		return NULL;
	}
	list->head->link=list->head;
	list->head->info=0;
	list->rear=list->head;
	list->length=0;
	return list;
}
void printCLList(CLList list)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head;
	if(list->head==NULL||list->length==0)
	{
		return ;
	}
	
	while(temp->link!=list->rear&&temp!=NULL)
	{			
		temp=temp->link;
		printf("%d   ",temp->info);
	}
}

int appendElemCLList(CLList list,int x)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode element=(PNode)malloc(sizeof(struct CLNode));
	if(temp==NULL)
	{
		printf("Out of Space \n");
		return NULL;
	}
	if(element==NULL)
	{
		printf("Out of Space \n");
		return NULL;
	}
	element->info=x;
	temp=list->head;
	while(temp->link!=list->rear)
	{
		temp=temp->link;
	}
	temp->link=element;
	element->link=list->rear;
	list->length++;
	return 0;
}


//后插法,在插入到p位置的后面
int insertPostCLList(CLList list, int p, int x)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode element=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head;
	if(list->length==0)
	{
		appendElemCLList(list,x);
		return 1;
	}
	if(element==NULL)
	{
		printf("Out of Space \n");
		return NULL;
	}
	int i=1;
	if(p>=list->length)
	{
		printf("插入的位置不正确");
		return NULL;
	}
	while(i<=p&&temp->link!=list->rear)
	{
		temp=temp->link;
		i++;
	}
	element->info=x;
	element->link=temp->link;
	temp->link=element;
	list->length++;
	return 1;	
}
//前插法,在插入到p位置的前面
int insertPreCLList(CLList list, int p, int x)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode element=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head;
	if(list->length==0)
	{
		appendElemCLList(list,x);
		return 1;
	}
	if(element==NULL)
	{
		printf("Out of Space \n");
		return NULL;
	}
	int i=1;
	if(p>=list->length)
	{
		printf("插入的位置不正确");
		return NULL;
	}
	while(i<p&&temp->link!=list->rear)
	{
		temp=temp->link;
		i++;
	}
	element->info=x;
	element->link=temp->link;
	temp->link=element;
	list->length++;
	return 1;	
}


int deleteVertexCLList(CLList list, int p)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode start=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head->link;
	start=list->head;

	if(p<=0||p>list->length)
	{
		printf("Not Exist!");
		free(temp);
		free(start);
		return -1;
	}
	int i=1;
	while(temp->link!=list->rear&&i<=p)
	{
		temp=temp->link;
		i++;
	}
	int j=1;
	while(j<=i-1)
	{
		start=start->link;
		j++;
	}
	start->link=temp->link;
	free(temp);
	return 0;

}

int deleteElemCLList(CLList list, int x)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode start=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head->link;
	start=list->head;

	//找到要查找元素x的位置
	int i=1;
	while(temp->link!=list->rear&&temp->info!=x)
	{
		temp=temp->link;
		i++;
	}
	//printf("%d  \n ",temp->info);
	
	//找到要查找元素x的位置的前一个
	int j=1;
	while(j<=i-1)
	{
		start=start->link;
		j++;
	}
	//	printf("%d  \n ",start->info);
	start->link=temp->link;
	free(temp);
	return 0;
}

int locateVertexCLList(CLList list, int x)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	temp=list->head;
	int i=0;
	while(temp->link!=list->rear&&temp->info!=x)
	{
			temp=temp->link;
			i++;
	}
	return i;
}

int lengthCLList(CLList list)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	int i=0;
	while(temp->link!=list->rear)
	{		
			temp=temp->link;
			i++;
	}
	return i;
}

void destroyCLList(CLList list)
{
	PNode temp=(PNode)malloc(sizeof(struct CLNode));
	PNode start=(PNode)malloc(sizeof(struct CLNode));
	start=list->head->link;
	while(start!=list->rear)
	{
		temp=start->link;
		delete start;
		start=temp;
	}
	list->head=NULL;
	list->rear=NULL;
	list->length=0;
}


#include "stdafx.h"
#include "malloc.h"
	
	struct CLNode;
	typedef struct CLNode * PNode;

	struct CLNode {
		int info;
		PNode link;
	};
	struct clList {
		PNode head;
		PNode rear;
		int length;
	};
	typedef struct clList * CLList;

	CLList createCLList(void);
	int insertPostCLList(CLList list, int p, int x);
	int insertPreCLList(CLList list, int p, int x);
	int deleteElemCLList(CLList list, int x);
	int deleteVertexCLList(CLList list, int p);
	int locateVertexCLList(CLList list, int x);
	void printCLList(CLList list);
	void destroyCLList(CLList list);
	int appendElemCLList(CLList list, int x);
	int lengthCLList(CLList list);



// LoopList.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Function.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int N=0;
	cin>>N;
	static int i =0;
	char op='0';
	CLList list=createCLList();
	while(i<N)
	{
		cin>>op;
		switch(op)
		{
		case 'C':
			{
				i++;
				break;
			}
		case 'W':
			{
				destroyCLList(list);
				i++;
				break;
			}
		case 'P':
			{
				printCLList(list);
				i++;
				break;
			}
		case 'L':
			{
				printf("%d \n",lengthCLList(list));
				i++;
				break;
			}
		case 'F':
			{
				int x=0;
				cin>>x;
				printf("%d \n",locateVertexCLList(list,x));
				i++;
				break;
			}
		case 'B':
			{
				int p=0;
				int x=0;
				cin>>p;
				cin>>x;
				insertPreCLList(list,p,x);
				i++;
				break;
			}
		case 'A':
			{
				int p=0;
				int x=0;
				cin>>p;
				cin>>x;
				insertPostCLList(list,p,x);
				i++;
				break;
			}
		case 'D':
			{
				int x=0;
     		cin>>x;
				deleteElemCLList(list,x);
				i++;
				break;
			}
		case 'S':
			{
				int p=0;
     		cin>>p;
				deleteVertexCLList(list,p);
				i++;
				break;
			}
		case 'R':
			{
				int x=0;
     			cin>>x;
				appendElemCLList(list,x);
				i++;
				break;
			}
			
		}
	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值