还是自己写的代码看着顺眼。
听波波说开学他讲数据结构都是用面向对象的方法讲,C++实现。对于刁肥宅而言,既然立志搞底层开发,就一定要熟练掌握C语言。C语言是世界上最精炼、最快的语言,没有之一!
猜猜哪一个是刁肥宅!哈哈哈!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <windows.h>
typedef int ElementType;
typedef struct Node
{
ElementType Data;
struct Node *Next;
}CircleListNode,*CircleListLink;
CircleListLink InitiateCircleList(CircleListLink L);/*初始化单向循环链表*/
void CreateRearCircleList(CircleListLink L,int n);/*尾插创建单向循环链表*/
void CreateFrontcircleList(CircleListLink L,int n);/*头插创建单向循环链表*/
bool CircleListEmpty(CircleListLink L);/*判断单向循环链表是否为空*/
void TraverseCircleList(CircleListLink L);/*遍历单向循环链表*/
void ClearCircleList(CircleListLink L);/*销毁单向循环线性表*/
bool GetElement(CircleListLink L,int position,ElementType *e);/*将第position个位置的元素返回
参数 ElementType *e 的设置与运用相当巧妙,灵活使用了C语言的指针*/
int GetLocation(CircleListLink L,ElementType e);/*查找元素e的位置*/
bool InsertNode(CircleListLink L,int position,ElementType e);/*在第position个位置插入元素e
*/
bool DeleteNode(CircleListLink L,int position,ElementType *e);/*删除第position个位置的元素
参数 ElementType *e 的设置与运用相当巧妙,灵活使用了C语言的指针*/
int GetCircleListLenght(CircleListLink L);/*返回单向循环链表的表长*/
void SortCircleList(CircleListLink L);/*对单项循化链表元素排序,未实现*/
int main()
{
srand(time(NULL));
CircleListLink L;
int n, i,RandomNumber,x;
ElementType *e;
L=InitiateCircleList(L);
n = rand() % 200 + 1;
printf("n = %d\n",n);
CreateRearCircleList(L,n);
TraverseCircleList(L);
RandomNumber = rand() % 200 + 1;
if(DeleteNode(L,RandomNumber,&e))
{
printf("%d has been deleted from the list.\n",e);
TraverseCircleList(L);
}
else
;
printf("Length = %d.\n",GetCircleListLenght(L));
RandomNumber = rand() % 200 + 1;
if(GetElement(L,RandomNumber,&e))
{
printf("GetElement success.RandomPosition = %d,e = %d.\n",RandomNumber,e);
TraverseCircleList(L);
}
else
;
if(InsertNode(L,5,100))
TraverseCircleList(L);
else
printf("Insert failed.\n");
RandomNumber = rand() % 200 + 1;
x = GetLocation(L,RandomNumber);
if(x == -1)
printf("%d is not located in the list.\n",RandomNumber);
else
printf("%d is located in %d.\n",RandomNumber,x);
printf("Length = %d.\n",GetCircleListLenght(L));
ClearCircleList(L);
printf("Length = %d.\n",GetCircleListLenght(L));
Sleep(1000*60);
return 0;
}
/*初始化单向循环链表*/
CircleListLink InitiateCircleList(CircleListLink L)
{
L = (CircleListNode *)malloc(sizeof(CircleListNode));
if(!L)
{
printf("Error.Allocate Space Failed.\n");
exit(-1);
}
L->Next = L;
}
/*尾插创建单向循环链表*/
void CreateRearCircleList(CircleListLink L,int n)
{
srand(time(NULL));
CircleListNode *PNew,*PTmp = L;
int i,value;
if(n < 1)
{
printf("Error.The size of n must be greater than 0.\n");
exit(EXIT_FAILURE);
}
else
{
for(i = 1;i <= n;i++)
{
PNew = (CircleListNode *)malloc(sizeof(CircleListNode));
value = rand() % 100 + 1;
PNew->Data = value;
PNew->Next = L;
PTmp->Next = PNew;
PTmp = PNew;
}
}
}
/*头插创建单向循环链表*/
void CreateFrontcircleList(CircleListLink L,int n)
{
srand(time(NULL));
CircleListNode *PNew,*PTmp = L;
int i,value;
if(n < 1)
{
printf("Error.The size of n must be greater than 0.\n");
exit(EXIT_FAILURE);
}
else
{
PNew = (CircleListNode *)malloc(sizeof(CircleListNode));
value = rand() % 100 + 1;
PNew->Data = value;
PNew->Next = PTmp->Next;
PTmp->Next = PNew;
}
}
/*判断单向循环链表是否为空*/
bool CircleListEmpty(CircleListLink L)
{
if(L->Next == L)
return true;
return false;
}
/*遍历单向循环链表*/
void TraverseCircleList(CircleListLink L)
{
CircleListNode *PCur = L->Next;
int Line = 0;
while(PCur != L)
{
printf("%-4d ",PCur->Data);
Line ++;
if(Line % 10 == 0)
printf("\n");
PCur = PCur->Next;
}
printf("\n");
}
/*销毁单向循环线性表*/
void ClearCircleList(CircleListLink L)
{
CircleListNode *PCur = L->Next,*PTmp;
while(PCur->Next != L)
{
PTmp = PCur->Next;
free(PCur);
PCur = PTmp;
}
L->Next = L;
}
/*将第position个位置的元素返回
参数 ElementType *e 的设置与运用相当巧妙,灵活使用了C语言的指针*/
bool GetElement(CircleListLink L,int position,ElementType *e)
{
CircleListNode *PCur = L->Next;
int ID = 1;
if((position < 1)||CircleListEmpty(L))
{
printf("GetElement Error.\n");
return false;
}
while(PCur != L && ID < position)
{
PCur = PCur->Next;
++ID;
}
if(PCur->Next == L && ID != position)
return false;
e = PCur->Data;
return true;
}
/*查找元素e的位置*/
int GetLocation(CircleListLink L,ElementType e)
{
CircleListNode *PCur = L->Next;
int ID = 1;
if(CircleListEmpty(L))
{
printf("Empty list.\n");
return -1;
}
while(PCur->Next != L)
{
if(PCur->Data == e)
break;
PCur = PCur->Next;
++ID;
}
if(PCur->Next == L && PCur->Data != e)
return -1;
return ID;
}
/*在第position个位置插入元素e*/
bool InsertNode(CircleListLink L,int position,ElementType e)
{
CircleListNode *PTmp = L,*PNew,*PRear = L->Next;
int ID = 1;
if((position < 1)||CircleListEmpty(L))
{
printf("Error.\n");
return false;
}
else
{
while(PRear != PTmp && ID < position)
{
PRear = PRear->Next;
PTmp = PTmp->Next;
++ID;
}
if((PRear == PTmp) && ID == position)
return false;
PNew = (CircleListNode *)malloc(sizeof(CircleListNode));
PNew->Data = e;
PNew->Next = PRear;
PTmp->Next = PNew; /*dangerous code line*/
return true;
}
}
/*删除第position个位置的元素
参数 ElementType *e 的设置与运用相当巧妙,灵活使用了C语言的指针*/
bool DeleteNode(CircleListLink L,int position,ElementType *e)
{
CircleListNode *PCur = L->Next,*PTmp = L;
int ID = 1;
if(CircleListEmpty(L) || position < 1)
{
printf("Error.Delete Failed.\n");
return false;
}
if(position == 1)
L->Next = L;
while(PCur->Next != L && ID < position)
{
PCur = PCur->Next;
PTmp = PTmp->Next;
++ID;
}
if((PCur->Next == L) && ID != position)
return false;
PTmp->Next = PCur->Next;
*e = PCur->Data;
PCur->Next = NULL;/*防止产生野指针*/
free(PCur);
return true;
}
/*返回单向循环链表的表长*/
int GetCircleListLenght(CircleListLink L)
{
CircleListNode *PCur = L->Next;
int ID = 1;
if(CircleListEmpty(L))
{
printf("Empty list.\n");
return -1;
}
while(PCur->Next != L)
{
PCur = PCur->Next;
++ID;
}
return ID;
}
/*对单项循化链表元素排序,未实现*/
void SortCircleList(CircleListLink L)
{
}