实现单向循环链表的创建、测长、打印、插入、删除及逆置

实例实现单向循环链表的创建、测长、打印、插入、删除及逆置

//circularList.h文件

#ifndef CIRCULAR_H_H_H
#define CIRCULAR_H_H_H

typedef struct student
{
	int data;
	struct student *next;
}cnode;

typedef cnode * cirList;
typedef cnode * cirPosition;

//建立循环链表
cirList CirCreate();

//循环链表测长
int CirLength (cirList L);

//循环链表打印
void CirPrint(cirList L);

//循环链表删除结点
cirList CirDelete(cirList L, int num);

//循环链表在值为val的结点前插入结点,值为insertvalue
cirList CirInsert(cirList L, int val, int insertvalue);

//循环链表的逆置
cirList CirReverse(cirList &L);

#endif


//circularList.cpp源文件


#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>

using namespace std;

#include "circularList.h"


/************************************************************************/
/* 功能:编程实现一个循环链表的建立(只有头指针,没有头结点)           */
/* 参数:无                                                             */
/* 返回:链表的头指针                                                   */
/************************************************************************/
cirList CirCreate()
{
	cirList head,p,s;      //head用于实现指向头结点,p指向当前结点,s指向新创建结点,p、s实现循环创建
	int x,cycle = 1;
	head = (cirList)malloc(sizeof(cnode));  //这个结点是个没用的结点,后面应该释放掉
	p = head;
	while(cycle)
	{
		printf("\nPlease input the data: ");
		scanf("%d",&x);
		if(x != 0)
		{
			s = (cirList)malloc(sizeof(cnode));
			s->data = x;
			printf("\n      %d",s->data);
			p->next = s;
			p = s;
		}
		else
			cycle = 0;
	}
	head = head->next;
	p->next = head;

	return head;
}

/************************************************************************/
/* 功能:编程实现一个循环链表的测长(只有头指针,没有头结点)           */
/* 参数:链表的头指针                                                   */
/* 返回:循环链表的长度(结点数目)                                     */
/************************************************************************/
int CirLength( cirList L )
{
	cirList p;
	int n = 0;
	p = L;
	while(p)
	{
		n++;
		p = p->next;
		if(p == L)
			break;
	}
	return n;
}

/************************************************************************/
/* 功能:编程实现一个循环链表的打印                                     */
/* 参数:双链表的头指针                                                 */
/* 返回:无                                                             */
/************************************************************************/
void CirPrint( cirList L )
{
	cirList p;
	int n = CirLength(L);
	printf("\nNow,These %d Records are ",n);
	p = L;
	while(p)
	{
		printf("\n         %d \n",p->data);
		p = p->next;
		if(p == L) //说明已循环一周
			break;
	}
}

/************************************************************************/
/* 功能:编程实现一个循环链表的删除                                     */
/* 参数:L:双链表的头指针     num:删除该值多对应的结点                  */
/* 返回:双链表的头指针                                                 */
/************************************************************************/
cirList CirDelete( cirList L, int num )
{
	cirList p,temp;
	p = temp = L;
	while(p)        //头指针不为NULL
	{
		if(p->data == num)
		{
			while(temp->next != p)
				temp = temp->next;     //找到p的上一个结点
			temp->next = p->next;      //删除p元素
			free(p);                   //释放p所指的内存空间
			if(p == L)                 //虽然p所指的内存被释放,但是其指针任指向这里,因此无影响
			{
				return temp->next;      //当删除的是头结点
			}
			return L;
		}
		p = p->next;
		if(p == L)              //说明已循环了一周
		{
			printf("\n%d could not been found \n",num);
			break;
		}
	}
	return L;
}

/****************************************************************************************/
/* 功能:编程实现循环链表在值为val的结点前插入结点,值为insertvalue                     */
/* 参数:L:循环链表的头指针     val:插入的位置    insertvalue:插入的值                  */
/* 返回:循环链表的头指针                                                               */
/****************************************************************************************/
cirList CirInsert( cirList L, int val, int insertvalue )
{
	cirList p1,p2,p3;
	p1 = p2 = L;
	while(p1)
	{
		if(p1->data == val)
		{
			while(p2->next != p1)
				p2 = p2->next;
			p3 = (cirList)malloc(sizeof(cnode));
			p3->data = insertvalue;
			p3->next = p2->next;
			p2->next = p3;
			if(p1 == L)      //如果插入的是第一个结点
				return p3;
			return L;
		}
		p1 = p1->next;
		if(p1 == L)         //循环链表循环了一圈没有找到
		{
			printf("\nNo such value can be insert\n");
			break;
		}
	}
	return L;
}

/********************************************************************/
/* 功能:编程实现循环链表的逆置                                     */
/* 参数:L:循环链表的头指针                                         */
/* 返回:循环链表的头指针                                           */
/********************************************************************/
cirList CirReverse( cirList &L )
{
	cirList Pre = L;
	cirList Cur = L->next;
	cirList Next;
	while(Cur)
	{
		Next = Cur->next;
		Cur->next = Pre;
		Pre = Cur;
		Cur = Next;
		if(Cur == L)
			break;
	}
	L->next = Pre;
	L = Pre;
	return L;
}


int main()
{
	cirList L;
	L = CirCreate();
	CirPrint(L);
	CirDelete(L,5);
	CirPrint(L);
	CirInsert(L,4,10);
	CirPrint(L);
	CirReverse(L);
	CirPrint(L);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/dby3579/article/details/52045468