//函数声明部分:"CirLinkList.h"
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}node;
void judgement_NULL(node * p); //判断动态内存是否开辟成功
node * creat_order(); //建立循环链表
void insert(node *head, int i, ElemType x); //在i位置上插入一个值为x的结点
void delete(node *head, int i); //删除i位置的结点
void find(node *head, ElemType x); //寻找只为x的结点位置
void length_list(node *head);
void output(node * head); //打印整个链表
void inits_linklist(node *head); //释放整个链表
void sortrank(node *head); //对整个链表进行排序
//函数实现:"CirLinkList.c"
#include"CirLinkList.h"
void judgement_NULL(node * p) //判断动态内存是否开辟成功,如果失败,程序直接结束
{
if (p == NULL)
{
perror("out of memory\n");
exit(EXIT_FAILURE);
}
}
node * creat_order() //创建一个循环单链表,以0作为结束标志
{
printf("请创建循环链表:");
node *head, *p, *r;
ElemType x;
head = (node *)malloc(sizeof(node));
judgement_NULL(head);
r = head;
head->next = NULL;
while (1)
{
scanf("%d", &x);
if (x != 0)
{
p = (node *)malloc(sizeof(node));
judgement_NULL(p);
p->data = x;
r->next = p;
r = p;
}
else
break;
}
r->next = head; //让最后一个结点的指针域指向头结点
printf("创建成功\n");
return head;
}
void insert(node *head, int i, ElemType x) //在i位置上插入一个元素x
{
node *p, *r, *q;
p = (node *)malloc(sizeof(node));
judgement_NULL(p);
p->data = x;
r = head;
while (1) //因为是循环链表,所以输入的位置只要i>1,就一定会有一个位置可以插入
{
if (i<=1) //顺着链表循环,直到i--到1
break;
r = r->next;
i--;
}
if (i < 1)
printf("没有这个结点\n");
else
{
q = r->next; //从头结点之后开始插入
r->next = p;
p->next = q;
printf("插入成功\n");
}
}
void delete(node *head, int i) //删除链表中i位置的结点
{
node *p, *q;
p = head;
while (1) //循环整个链表,直到i--到1
{
if (i <= 1)
break;
p = p->next;
i--;
}
if (i<1)
printf("没有该结点\n");
else
{
if (p->next== head) //如果找到要删除的结点是头结点,则跳过删除下一个结点
{
p = head->next;
head->next = p->next;
free(p);
}
else
{
q = p->next;
p->next = q->next;
free(q);
q = NULL;
}
printf("删除成功\n");
}
}
void find(node *head, ElemType x) //查找值为x的元素所在结点位置
{
head = head->next;
node *p;
int count = 1;
if (head->data == x) //先判断是不是第一个
printf("结点位置:1");
else
{
p = head->next;
while (p != head)
{
count++;
if (p->data == x)
break;
else
p = p->next;
}
if (p == head)
printf("没有这个结点\n");
else
printf("结点位置:%d\n", count);
}
}
void length_list(node *head) //求取循环链表的结点个数
{
int len = 0;
if (head->next == NULL)
{
printf("链表为NULL\n");
}
else
{
node *p = head->next;
while (p != head)
{
len++;
p = p->next;
}
printf("链表长度:%d\n", len);
}
}
void output(node * head) //打印循环链表
{
node *p;
if (head == NULL)
printf("链表为空\n");
else
{
printf("打印链表:");
p = head->next;
while (p != head)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
void inits_linklist(node *head) //初始化链表
{
node *p,*r;
p = head->next;
while (p !=head)
{
r = p;
p = p->next;
free(r);
r = NULL;
}
free(head);
head = NULL;
printf("初始化成功\n");
}
void sortrank(node *head) //对整个链表进行排序
{
node *p, *r;
ElemType tmp;
p = head->next;
while (p->next != head) //冒泡排序
{
r = head->next;
while (r->next != head)
{
if ((r->data) >(r->next->data))
{
tmp = r->data;
r->data = r->next->data;
r->next->data = tmp;
}
r = r->next;
}
p = p->next;
}
printf("排序成功\n");
}
//函数测试部分:
#include"CirLinkList.h"
int main()
{
printf("******************************************** \n");
printf("******************************************** \n");
printf("**1.Creat_LinkList 2.Insert_Element** \n");
printf("**3.Find 4.Delete_Element** \n");
printf("**5.Length_LinkList 6.Output_LinkList**\n");
printf("*7.InitsLinkLinst 8.Sortrank **\n");
printf("*0.Exit *******************\n\n\n");
node *ret = NULL;
ElemType x;
int i = 0;
int n = 0;
while (1) //循环起来,直到选择0结束
{
printf("请选择功能:");
scanf("%d", &n);
if (n == 0) //当选择功能时,选择0的优先级最高,无条件退出
{
free(ret);
exit(1);
}
if (n == 1 && ret == NULL) //只有当前没创建链表,而且ret为空时,n==1时,创建链表才有效
{
ret = creat_order();
}
else if (n != 1 && ret == NULL)
printf("\n链表为NULL,请建立链表\n\n");
else if (ret != NULL) //当创建链表之后这些语句才有效
{
switch (n)
{
case 2: //链表的末尾也可以插入元素
printf("请输入要插入的位置和要插入的元素\n");
scanf("%d", &i);
scanf("%d", &x);
insert(ret,i,x);
break;
case 3:
printf("请输入要查找的元素:");
scanf("%d", &x);
find(ret,x);
break;
case 4:
printf("请输入要删除的位置:");
scanf("%d", &i);
delete(ret, i);
break;
case 5:
length_list(ret);
break;
case 6: //打印链表
output(ret);
break;
case 7:
inits_linklist(ret);
ret = NULL;
break;
case 8:
sortrank(ret);
break;
default:
printf("选择无效,请重新选择\n");
break;
} //switch()语句结束
}
}
system("pause");
return 0;
}