# 数据结构与算法—循环链表

175人阅读 评论(0)

/*

*/

#include <stdlib.h>
#include <stdio.h>

#define ElementType int

struct Node;
typedef struct Node *PtrNode;
typedef PtrNode List;

typedef struct Node
{
ElementType data;
struct Node *next;

}Node;
void printCirList(List L)
{
List temp;
for(temp = L; temp->next != L; temp = temp->next)
printf("  %d",temp->data);
printf("  %d\n",temp->data);
}
void InitCirList(List *list)//尾插法
{

ElementType itemValue;
List tempCirList,temp;
printf("输入结点的值，0表示结束:\n");
while(1)
{

scanf("%d",&itemValue);
//    fflush(stdin);

if( itemValue == 0)
return;
if((*list) == NULL)//只有一个结点
{
*list = malloc(sizeof(struct Node));
if(!(*list))
exit(EXIT_FAILURE);
(*list)->data = itemValue;
(*list)->next = (*list);
}
else
{
for(tempCirList = (*list); tempCirList->next != (*list); tempCirList=tempCirList->next)
;
temp = malloc(sizeof(struct Node));

temp->data = itemValue;

tempCirList->next = temp;

temp->next = (*list);

}
}
}
int CirListLength(List L)
{
List temp;
int sum = 1;
for(temp = L; temp->next != L; temp=temp->next)
sum++;
return sum;
}
List FindPreCirList(List L, int insertPosit)
{
List temp=NULL;
int i=1;
if(insertPosit <1 || insertPosit > CirListLength(L))
{
printf("\n插入/删除的位置越界！！！\n");
return temp;
}
temp = L;
while( (i + 1)<insertPosit )
{
temp =temp->next;
i++;
}
return temp;

}
//【特么的关键：2015-07-08
//一定要传*L进来才能在插入第一个位置的时候，改变头指针的地址】
int insertCirList(List *L, int insertPosition, ElementType insertValue)
{
List PreList,temp,target;

if(insertPosition == 1)//插入的是第一个结点
{
for(target = *L; target->next != *L; target = target->next)
;

temp = malloc(sizeof(struct Node));
temp->data = insertValue;

target->next = temp;
temp->next = *L;
*L = temp;
return 1;
}

PreList = FindPreCirList(*L,insertPosition);
if(!PreList)
return 0;
temp = malloc(sizeof(struct Node));
temp->data = insertValue;

temp->next = PreList->next;
PreList->next = temp;
return 1;

}

int deleteCirList(List *L, int deletePosition)
{
List temp,target;
if(deletePosition == 1)
{
for(target = *L; target->next != *L; target = target ->next)
;
temp = *L;

target->next = (*L)->next;
(*L) = (*L)->next;

free(temp);
return 1;
}

target = FindPreCirList(*L, deletePosition);
if(!target)
return 0;
temp = target->next;
target->next = temp->next;
free(temp);
return 1;

}
int main()
{
unsigned int item;
int insertPosition,deletePosition;
ElementType insertValue;

//【特么关键：2015-07-07
//注意一定要初始化为空，因为后面建立链表的第一个结点的时候有用到！！！】
List L = NULL;

printf("1.初始化链表\n");
printf("2.插入结点\n");
printf("3.删除结点\n");
printf("4.返回结点位置\n");
printf("5.遍历列表\n");
printf("0.退出\n");

while(1)
{
printf("请选择你的操作：");
scanf("%d",&item);
switch(item)
{
case 1:
InitCirList(&L);
printCirList(L);
break;
case 2:
while(1)
{
printf("输入需要插入的位置：");
scanf("%d",&insertPosition);
printf("输入需要插入的值：");
scanf("%d",&insertValue);
if(!insertCirList(&L,insertPosition,insertValue))
continue;
printCirList(L);
break;
}
break;
case 3:
while(1)
{
printf("输入要删除的结点位置：");
scanf("%d",&deletePosition);
if(!deleteCirList(&L,deletePosition))
continue;
printCirList(L);
break;
}

break;
case 4:
break;
case 5:
break;
case 0:
return 0;
break;
default:
break;

}
}

return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：5539次
• 积分：245
• 等级：
• 排名：千里之外
• 原创：20篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
评论排行