1“`
typedef int ElementType;
typedef int Boolean;
typedef struct node* Node;
struct node //定义双向的结构体
{
struct node* prior;
ElementType value;
struct node* next;
};
Boolean make(Node *head);//创建一个新的结点
Boolean init(Node *head);//创建一个双向循环的结点,并使之成为头结点
Boolean insert_tail(Node head, ElementType value);//实现尾插入
Boolean insert_index(Node head, int index, ElementType value);//实现按位置插入
Boolean insert_head(Node head, ElementType value);//实现头插入
Boolean delete_index(Node head, int index);//按位置删除
Boolean delete_value(Node head, ElementType value);//按值删除
Boolean update_index(Node head, int index, ElementType value);//按位置替换
Boolean update_value(Node head, ElementType old_value, ElementType new_value);//按值替换
Boolean query_index(Node head, int index);//查找所给位置的值
Boolean query_value(Node head, ElementType value);//查找所给值的位置
int length(Node head);//计算链表长度
void print_p(Node head);//前序遍历打印
void print_n(Node head);//后序遍历打印
int main()
{
Node head;
make(&head);
init(&head);
ElementType i;
for (i = 0; i < 10; i++) //按头插入0~9
{
insert_head(head, i);
}
for (i = 0; i < 10; i++) //按尾插入0~9
{
insert_tail(head, i);
}
insert_index(head, 3, 99); //在3位置插入99
insert_index(head, 0, 99); //在0位置插入99
insert_index(head, length(head), 99); //在链表最后位置插入99
print_p(head); //前序遍历打印
print_n(head); //后序遍历打印
delete_index(head, 4); //删除位置4
print_p(head);
delete_value(head, 0); //删除0值
print_p(head);
update_index(head, 0, 100); //将0位置的数改为100
update_index(head, length(head) - 1, 100); //将最后一个数改为100
print_p(head);
update_value(head, 100, 101); //将100改为101
print_p(head);
print_n(head);
query_index(head, 15); //查找第15位的值
query_value(head, 101); //查找值为101的位置
printf("%d\n", length(head)); //打印链表的长度
return 0;
}
//以下是实现各功能的调用函数的程序
Boolean query_value(Node head, ElementType value)
{
Node temp = head;
int index = 0;
while (temp->next != head)
{
if (value == temp->next->value)
{
printf(“%d is on %d\n”, value, index);
}
index++;
temp = temp->next;
}
return T;
}
Boolean query_index(Node head, int index)
{
if (index < 0 || index >= length(head))
{
printf(“out of range\n”);
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
printf(“%d is %d\n”, index, head->next->value);
return T;
}
Boolean update_value(Node head, ElementType old_value, ElementType new_value)
{
Node temp = head;
while (temp->next != head)
{
if (old_value == temp->next->value)
{
temp->next->value = new_value;
}
temp = temp->next;
}
return T;
}
Boolean update_index(Node head, int index, ElementType value)
{
if (index < 0 || index >= length(head))
{
printf(“out of range\n”);
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
head->next->value = value;
return T;
}
Boolean delete_value(Node head, ElementType value)
{
Node temp = head;
while (temp->next != head)
{
if (value == temp->next->value)
{
Node temp2 = temp->next;
temp->next = temp->next->next;
temp->next->prior = temp;
free(temp2);
}
else
{
temp = temp->next;
}
}
return T;
}
Boolean delete_index(Node head, int index)
{
if (index < 0 || index >= length(head))
{
printf(“out of range\n”);
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
Node temp = head->next;
head->next = head->next->next;
head->next->prior = head;
free(temp);
return T;
}
int length(Node head)
{
int count = 0;
Node temp = head;
while (temp->next != head)
{
count++;
temp = temp->next;
}
return count;
return T;
}
Boolean insert_index(Node head, int index, ElementType value)
{
if (index < 0 || index > length(head))
{
printf(“out of range\n”);
return F;
}
Node newnode;
make(&newnode);
newnode->value = value;
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
newnode->next->prior = newnode;
return T;
}
Boolean insert_head(Node head, ElementType value)
{
Node newnode;
make(&newnode);
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
newnode->next->prior = newnode;
return T;
}
void print_p(Node head)
{
Node temp = head;
while (temp->prior != head)
{
printf(“%d “, temp->prior->value);
temp = temp->prior;
}
printf(“\n”);
}
void print_n(Node head)
{
Node temp = head;
while (temp->next != head)
{
printf(“%d “, temp->next->value);
temp = temp->next;
}
printf(“\n”);
}
Boolean insert_tail(Node head, ElementType value)
{
Node newnode;
make(&newnode);
newnode->value = value;
head->prior->next = newnode;
newnode->next = head;
newnode->prior = head->prior;
head->prior = newnode;
return T;
}
Boolean init(Node *head)
{
Node newnode;
make(&newnode);
newnode->next = newnode;
newnode->prior = newnode;
*head = newnode;
return T;
}
Boolean make(Node *head)
{
Node newnode = (Node)malloc(sizeof(struct node));
if (NULL == newnode)
{
return F;
}
*head = newnode;
return T;
}