循环链表初始化、随机20个数值、打印一圈、10圈、指定插入、指定删除、结束
源程序:#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 20
typedef struct like
{
int T;
struct like *next;
}like, *likes;
void explain();
void InitList(likes *G);
void GetList(likes *G);
void PutList(like *G);
void PutTwoList(like *G);
void AppointList(likes *G);
void DeleList(likes *G);
void DeleList(likes *G)
{
like *temp;
like *temps = (*G)->next;
int A;
scanf("%d", &A);
getchar();
printf("删除数值%d", A);
while (1)
{
if (temps->T == A)
{
if ((*G)->next == temps)
{
temp = temps;
do
{
temp = temp->next;
} while (temp->next !=(*G)->next);
(*G)->next = temps->next; //头部数据调整
temp->next = temps->next; //尾部数据调整
}
else
{
temp->next = temps->next;
}
free(temps);
break;
}
temp = temps; //保留上一个链表的地址
temps = temps->next;
}
}
void AppointList(likes *G)
{
like *temp;
like *temps = (*G)->next;
int A;
scanf("%d", &A);
getchar();
printf("在%d插入数值:", A);
while (1)
{
if (temps->T == A)
{
temp = (likes )malloc(sizeof(like ));
scanf("%d", &temp->T);
getchar();
temp->next = temps->next;
temps->next = temp;
break;
}
temps = temps->next;
}
}
void PutTwoList(like *G)
{
int i;
like *temp = G->next;
for (i = 0; i < 10; i++ )
{
printf("%d、", i + 1);
do
{
printf("<%d>", temp->T);
temp = temp->next;
} while (G->next != temp );
putchar('\n');
}
}
void PutList(like *G)
{
like *temp = G->next;
do
{
printf("<%d>", temp->T);
temp = temp->next;
} while (G->next != temp);
}
void GetList(likes *G)
{
like *temp, *tail;
int i;
//随机创建20个数值
srand((unsigned)time(NULL));
for (i = 0; i < MAXSIZE; i++)
{
temp = (likes )malloc(sizeof(like ));
temp->T = (rand() % 100);
if (*G == (*G)->next)
{
temp->next = temp;
(*G)->next = temp;
}
else
{
tail->next = temp;
temp->next = (*G)->next;
}
tail = temp;
}
}
void InitList(likes *G) //初始化链表
{
(*G)->next = *G;
}
void explain()
{
printf("1、初始化循环链表\n");
printf("2、输入数据\n");
printf("3、打印一圈循环链表\n");
printf("4、无限制打印循环链表\n");
printf("5、指定位置插入数据\n");
printf("6、指定位置删除数据\n");
printf("0、结束本程序\n");
}
int main()
{
int L;
like *G = (likes )malloc(sizeof(like ));
explain();
while (1)
{
printf("输入选项:");
scanf("%d", &L);
getchar();
switch (L)
{
case 1:{
InitList(&G);
}
break;
case 2:{
GetList(&G);
}
break;
case 3:{
PutList(G);
putchar('\n');
}
break;
case 4:{
PutTwoList(G);
}
break;
case 5:{
AppointList(&G);
}
break;
case 6:{
DeleList(&G);
putchar('\n');
}
break;
case 0:{
return 0;
}
break;
}
}
return 0;
} {:5_90:}
页:
[1]