|
这是带头结点的,根据这程序修改,自己另写也行,但结构体必须是:
[code:1]
typedef struct node
{
int data;
struct node *next:
} cyclklist;
cyclklist *rear;
[/code:1]
下面为带头结点的循环链表:
[code:1]
/*带头结点的单循环链表*/
#include <stdio.h>
/********表的定义如下***********/
typedef struct node
{
int data;
struct node *next;
} cyclklist;
cyclklist* initiate_cyclklist(void);
cyclklist* create_cyclklist(cyclklist *head);
cyclklist* find_cyclklist(cyclklist *head, int i);
cyclklist* insert_cyclklist(cyclklist *head, int i, int x);
cyclklist* delete_cyclklist(cyclklist *head, int i);
int main (void)
{
cyclklist *head, *p;
int count, input;
head = initiate_cyclklist();
printf("请输入一些数字(0结束):");
head = create_cyclklist(head);
print_cyclklist(head);
printf("请输入要查找的序号:");
scanf("%d", &count);
p = find_cyclklist(head, count);
printf("第%d号的值为%d\n", count, *p);
printf("请输入要查找的值:");
scanf("%d", &input);
count = locate_cyclklist(head, input);
printf("该值%d的位置是%d\n", input, count);
printf("请输入要插入的位置和数字:");
scanf("%d %d", &count, &input);
head = insert_cyclklist(head, count, input);
print_cyclklist(head);
printf("请输入要删除的位置:");
scanf("%d", &count);
head = delete_cyclklist (head, count);
print_cyclklist(head);
count = length_cyclklist(head);
printf("\n线性链表的带头结点的单循环链表长为%d\n", count);
return 0;
}
/*************初始化运算***************/
cyclklist* initiate_cyclklist (void)
{
cyclklist *head;
head = (cyclklist *) malloc (sizeof(cyclklist));
return (head);
}
/***************创建表运算*******************/
cyclklist* create_cyclklist (cyclklist *head)
{
cyclklist *new, *end;
int x;
end = head;
scanf("%d", &x);
while (x != 0)
{
new = (cyclklist *) malloc (sizeof(cyclklist));
new -> data = x;
end -> next = new;
end = new;
scanf("%d", &x);
}
end -> next = head;
return (head);
}
/**********显示列表运算*************/
int print_cyclklist (cyclklist *head)
{
cyclklist *p;
int j = 1;
p =head;
printf("最新的排列:\n");
if (head != null)
{
p = p -> next;
while (p != head)
{
printf("(%d):", j);
j++;
printf("%d ", p -> data);
p = p -> next;
}
}
printf("\n");
}
/***********查找运算(读表元)*******************/
cyclklist* find_cyclklist (cyclklist *head, int i)
{
cyclklist *p;
int j = 0;
p = head;
while ((p -> next != head) && (j < i))
{
p = p -> next;
j++;
}
if (i == j)
return (p);
else
{
printf("\n该位置不存在!\n");
return 0;
}
}
/**************定位运算(按值查找)************/
int locate_cyclklist (cyclklist *head, int x)
{
cyclklist *p;
int j = 0;
p = head;
while ((p -> next != head) && (p -> data != x))
{
p = p -> next;
j++;
}
if (p -> data == x)
return (j);
else
{
printf("\n查无此值!\n");
return 0;
}
}
/************************插入运算**************************/
cyclklist* insert_cyclklist (cyclklist *head, int i, int x)
{
cyclklist *p, *s;
p = find_cyclklist(head, i - 1);
if (p == null)
{
printf("不存在第%d个位置!\n", i);
return 0;
}
else
{
s = (cyclklist *) malloc (sizeof(cyclklist));
s -> data = x;
s -> next = p -> next;
p -> next = s;
}
return (head);
}
/*********************删除运算***********************/
cyclklist* delete_cyclklist (cyclklist *head, int i)
{
cyclklist *p, *s;
p = find_cyclklist(head, i - 1);
if ((p != null) && (p -> next != null))
{
s = p -> next;
p -> next = s -> next;
free (s);
}
else
{
printf("\n不存在第%d个位置!\n", i);
return 0;
}
return (head);
}
/***********求表长运算***************/
int length_cyclklist (cyclklist *head)
{
int j = 0;
cyclklist *p;
p = head;
while (p -> next != head)
{
p = p -> next;
j++;
}
return (j);
}
[/code:1] |
|