#include
#include
#include
struct A {// 结构体A
char c[10];//数据
};
struct B { //结构体B
int number;//链表节点号
struct A a; //包含结构体A
struct B* next;//形成链表
};
//链表头尾指针
struct B* R_head = NULL;
struct B* R_end = NULL;
void deleteListHead()//删除链表头节点
{
if (R_head == NULL)//链表为空
{
return;
}
//记住旧的头
struct B* d = R_head;
//头的下一个,变成新的头
R_head = R_head->next;
//释放旧的头
free(d);
}
void deleteListTail()
//删除链表尾节点
{
struct B* p = R_head;
if (R_head == NULL)//空链
{
return;
}
else if (R_head == R_end)//只有一个数据的链
{
free(R_head);
R_head = NULL;
R_end = NULL;
}
else
{ while (p->next != R_end)
{ p = p->next;
}
}
//找到了,删尾巴
free(R_end);
R_end = p;
R_end->next = NULL;
}
void DeleteList()//删除链表节点
{
int a;// a是节点号
printf("输入节点号:\n");
scanf("%d",&a); struct B* ptemp = R_head; while (ptemp != NULL)
{
if (ptemp->number ==a)//找到节点号
{ if (R_head == R_end) { deleteListHead(); printf("删除成功\n"); return; } //有两个节点 else if (R_head->next == R_end) { if (ptemp->number == a) { deleteListHead(); printf("删除成功\n"); return; } else { deleteListTail(); printf("删除成功\n"); return; } } else if (R_head->next != R_end)//多个节点 { struct B* d = R_head; while (d->number != a)//找节点 { d = d->next; } //找到后 struct B* del = R_head;//再找节点的前一个 while (del->next != d) { del = del->next; } del->next = d->next;//关键操作 free(d); printf("删除成功\n"); return; }
} ptemp = ptemp->next;
}
}
void addLIST(int number,struct A *dic)//按照number(节点号)排序插入链表
{
struct B* ptemp = R_head;
struct B* pt = R_head;
/*尾添加,无需排序
struct B* NewRoom = (struct B*)malloc(sizeof(struct B));
NewRoom->number = number;
//连接
if (R_head == NULL || R_end == NULL) //尾插法
{
//链表为空时 R_head = NewRoom;
R_end = NewRoom;
}
else
{
//链表不为空 R_end->next = NewRoom;
R_end = NewRoom;
R_end=NULL;
}
*/
//按节点号插
if (R_head == NULL || R_end == NULL)//空链
{
printf("空链插 \n");
struct B* NewRoom = (struct B*)malloc(sizeof(struct B));
NewRoom->number = number;
NewRoom->a = *dic;
NewRoom->next = NULL;
R_head = NewRoom;
R_end = NewRoom;
}
else if (R_end->number < number)//尾插
{
printf("尾插 \n");
struct B* NewRoom = (struct B*)malloc(sizeof(struct B));
NewRoom->number = number;
NewRoom->a = *dic;
R_end->next = NewRoom;
R_end = NewRoom;
R_end->next = NULL;
}
else if (R_head->number > number)//头插
{
printf("头插\n");
struct B* NewRoom = (struct B*)malloc(sizeof(struct B));
NewRoom->number = number;
NewRoom->a = *dic;
NewRoom->next = R_head;
R_head = NewRoom;
}
else//中间插
{
printf("中间插\n");
while (ptemp->number < number)
{ pt = ptemp; ptemp = ptemp->next;
}
struct B* NewRoom = (struct B*)malloc(sizeof(struct B));
NewRoom->number = number;
NewRoom->a = *dic;
NewRoom->next = ptemp;
pt->next = NewRoom;
}
}
void FreeList()
//释放链表
{
printf("谢谢使用按任意键关闭程序!!\n");
struct B* ptemp = R_head;
while (ptemp != NULL)
{
struct B* pt = ptemp;
ptemp = ptemp->next;
free(pt);
}
R_head = NULL;
R_end = NULL;
} void printlist()//打印所有信息
{
struct B* ptemp = R_head;
int j, sum = 0;
while (ptemp != NULL)
{
if (ptemp->number)
{ printf("\t节点号int--%d\n", ptemp->number); printf("\t节点char--%s\n", ptemp->a.c);
}
printf("\n");
ptemp = ptemp->next;
}
printf("\t要返回首页吗?(1:Y,0:N)");
scanf("%d", &j);
if (j == 0)
{
printlist();
}
system("cls");
}
void Login() //登记,注册函数
{
struct A* info = (struct A*)malloc(sizeof(struct A));
int i, j;
system("cls");
printf("写入新的char\n");
scanf("%s", &info->c); printf("请输入int:\n");
scanf("%d", &i);
addLIST(i,info);
printf("恭喜,写入成功!!\n要返回首页吗?(1:Y,0:N)");
scanf("%d", &j);
if (j == 0)
{
Login();
}
system("cls");
}
void show() {
printf("| 1.--写入|\n");
printf("| 2.--删除|\n");
printf("| 3.--排序输出|\n");
printf("| 4.--退出|\n");
} int main()
{
int i;
do //菜单栏
{
printf("\n");
show();
printf("输入需要选择的功能:");
scanf("%d", &i); switch (i) //通过输入相应数字调用不同函数进行相应工作
{
case 1: Login(); break;
case 2:DeleteList(); break;
case 3:printlist(); break;
case 4:FreeList(); break;
default: printf("\n 输入错误,请从新选择\n"); break;
}
} while (i != 4);
system("pause");
return 0;
}