马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 41
typedef struct Like
{
int T;
struct Like *next;
}Like, *Likes;
void explain(); //本程序各个功能说明
void InitList(Likes *N);//清空数据并初始化程序
void GetList(Likes *N); //随机创建41人员个编号
void PutList(Like *N); //打印所有链表
void DeThList(Likes *N); //依次删除第三人并打印编号
void DeThTwList(Likes *N); //依次删除第三人直至剩余最后两人并打印
void InsertList(Likes *N); //指定位置插入
void DeApList(Likes *N); //指定位置删除
void DeApList(Likes *N)
{
int K;
Like *temp = (*N)->next, *temps;
printf("删除数据:");
scanf("%d", &K);
while (temp->T != K)
{
temps = temp;
temp = temp->next;
if (temp == (*N)->next)
{
printf("未找到匹配的数据请确认输入是否准确。\n");
return;
}
}
if ((*N)->next == temp)
{
do
{
temps = temp;
temp = temp->next;
} while (temp != (*N)->next);
(*N)->next = temp->next;
}
temps->next = temp->next;
free(temp);
}
void InsertList(Likes *N)
{
int K;
Like *temp = (*N)->next, *temps;
scanf("%d", &K);
getchar();
printf("在%d之后插入\n", K);
while (temp->T != K)
{
temp = temp->next;
if (temp == (*N)->next)
{
printf("未找到匹配的数据请确认输入是否准确。\n");
return;
}
}
temps = (Likes )malloc(sizeof(Like ));
printf("输入插入的值:");
scanf("%d", &temps->T);
getchar();
temps->next = temp->next;
temp->next = temps;
}
void DeThTwList(Likes *N)
{
int i, j;
Like *temp = (*N)->next, *temps, *tempss;
for (i = 0; i < MAXSIZE - 2; i++)
{
for (j = 0; j < 2; j++)
{
temps = temp;
temp = temp->next;
}
if ((*N)->next == temp)
{
(*N)->next = temp->next; //如果头文件数据域被删除,用temp->next更换头文件数据域中的数据
}
tempss = temp;
temp = temp->next;
temps->next = temp; //与前一个链表相连
free(tempss);
}
PutList(*N);
}
void DeThList(Likes *N)
{
int i, j;
Like *temp = (*N)->next, *temps, *tempss;
for (i = 0; i < MAXSIZE; i++)
{
for (j = 0; j < 2; j++)
{
temps = temp;
temp = temp->next;
}
if (temp == (*N)->next)
{
(*N)->next = temp->next;
}
printf("<%d>", temp->T);
tempss = temp;
temp = temp->next;
temps->next = temp;
free(tempss);
}
putchar('\n');
}
void PutList(Like *N)
{
Like *temp = N->next;
while (1)
{
printf("<%d>", temp->T);
temp = temp->next;
if (N->next == temp)
{
break;
}
}
putchar('\n');
}
void GetList(Likes *N)
{
int i;
Like *temp, *tail;
srand((unsigned)time(NULL)); //随机数按照时间归零
for (i = 0; i < MAXSIZE; i++)
{
temp = (Like *)malloc(sizeof(Like ));
temp->T = (rand() % 100);
if ((*N)->next == *N)
{
(*N)->next = temp;
}
else
{
tail->next = temp;
}
tail = temp;
tail->next = (*N)->next;
}
}
void InitList(Likes *N)
{
int i = 0;
Like *temp;
if (*N != NULL)
{
while (i <= MAXSIZE)
{
temp = *N;
*N = (*N)->next;
free(temp);
i++;
}
}
*N = (Likes )malloc(sizeof(Like ));
(*N)->next = *N;
}
void explain()
{
printf("1、清空数据并初始化程序\n");
printf("2、随机创建41人员个编号\n");
printf("3、打印所有人员编号\n");
printf("4、依次删除第三人并打印编号\n");
printf("5、依次删除第三人直至剩余最后两人\n");
printf("6、指定位置插入\n");
printf("7、删除指定人员\n");
printf("0、结束本程序\n");
}
int main()
{
int i;
Like *N = NULL;
explain();
while (1)
{
printf("输入选项:");
scanf("%d", &i);
getchar();
switch (i)
{
case 1:{
InitList(&N);
}
break;
case 2:{
GetList(&N);
}
break;
case 3:{
PutList(N);
}
break;
case 4:{
DeThList(&N);
}
break;
case 5:{
DeThTwList(&N);
}
break;
case 6:{
InsertList(&N);
}
break;
case 7:{
DeApList(&N);
}
break;
case 0:{
return 0;
}
break;
}
}
return 0;
}
|