循环链表
#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F 0
typedef int Elementype;
typedef int Status;
struct node
{
Elementype value;
struct node* next;
};
typedef struct node* Node;
Status init(Node* p);
Status print(Node p);
int length(Node p);
Status insert_head(Node p,Elementype value); //头插
Status insert_tail(Node p,Elementype value); //尾插
Status insert_index(Node p,int index,Elementype value); //指定位置插入
Status delete_index(Node p,int index); //删除指定位置的值
Status delete_value(Node p,Elementype value); //删除指定的值
Status update_index(Node p,int index,Elementype value); //更新指定位置的值
Status update_value(Node p,Elementype old_value,Elementype new_value); //更新指定的值
Status query_index(Node p,int index); //查询指定位置的值
Status query_value(Node p,Elementype value); //查询指定值的位置
int main()
{
Node head;
init(&head);
int i;
for(i = 0;i < 10;i++)
{
insert_head(head,i + 1);
}
print(head);
for(i = 0;i < 10;i++)
{
insert_tail(head,i + 1);
}
print(head);
insert_index(head,2,99);
print(head);
delete_index(head,5);
print(head);
delete_value(head,1);
print(head);
update_index(head,5,55);
print(head);
update_value(head,8,100);
print(head);
query_index(head,15);
query_value(head,99);
query_value(head,100);
return 0;
}
Status init(Node* p)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->next = newnode;//自己指向自己
*p = newnode
return T;
}
Status print(Node p)
{
Node head = p;
while(head != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}
int length(Node p)
{
Node head = p;
int len = 0;
while(head != p->next)
{
len++;
p = p->next;
}
return len;
}
Status insert_head(Node p,Elementype value)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
return T;
}
Status insert_tail(Node p,Elementype value)
{
Node head = p;
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
while(head != p->next)
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
return T;
}
Status insert_index(Node p,int index,Elementype value)
{
if(index < 0 || index > length(p) - 1) //0 <= index <= length - 1
{
printf("index ie error\n");
return F;
}
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
int i;
for(i = 0;i < index;i++)
{
p = p->next;
} //p指向了index的前一个结点
newnode->next = p->next;
p->next = newnode;
return T;
}
Status delete_index(Node p,int index)
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
Node temp = p->next;
p->next = temp->next; //p->next->next;
free(temp);
return T;
}
Status delete_value(Node p,Elementype value)
{
int i = 0;
Node head = p; //记录链表的头结点
while(head != p->next)
{
if(value == p->next->value)
{
delete_index(head,i);
}
else
{
i++;
p = p->next;
}
}
return T;
}
Status update_index(Node p,int index,Elementype value)
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
p->next->value = value;
return T;
}
Status update_value(Node p,Elementype old_value,Elementype new_value)
{
Node head = p;
while(p->next != head)
{
if(old_value == p->next->value)
{
p->next->value = new_value;
}
p = p->next;
}
}
Status query_index(Node p,int index)
{
if(index < 0 || index > length(p) - 1)
{
printf("index is error\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
p = p->next;
}
printf("query_index(%d) : %d\n",index,p->next->value);
return T;
}
Status query_value(Node p,Elementype value)
{
printf("the index of query_value(%d) are : ",value);
int i = 0;
int flag = 0;
Node head = p;
while(p->next != head)
{
if(p->next->value == value)
{
flag++;
printf("%d ",i);
}
else
{
i++;
p=p->next;
}
}
if(flag == 0)
{
printf("NOT FOUND\n");
return F;
}
printf("\n");
return T;
}
双向循环链表
#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F 0
typedef int Elementype;
typedef int Status;
struct node//双方向循环链表结点结构体
{
struct node* prior;
Elementype value;
struct node* next;
};
typedef struct node* Node;
Status init(Node* p);
Status insert_head(Node p,Elementype value);
void printN(Node p);
void printP(Node p);
int length(Node p);
Status insert_tail(Node p,Elementype value);
Status insert_index(Node p,int index,Elementype value);
Status delete_index(Node p,int index);
Status delte_value(Node p,Elementype value);
int main()
{
Node head;
init(&head);
int i;
for(i = 0;i < 10;i++)
{
insert_head(head,i+1);
}
printN(head);
printP(head);
for(i = 0;i < 10;i++)
{
insert_tail(head,i+1);
}
printN(head);
printP(head);
return 0;
}
Status init(Node* p)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->next = newnode;
newnode->prior = newnode;
*p = newnode;
return T;
}
Status insert_head(Node p,Elementype value)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p->next;
p->next = newnode;
newnode->prior = p;
p->next->next->prior = newnode;
return T;
}
void printN(Node p)
{
Node head = p;
while(head != p->next)
{
printf("%d ",p->next->value);
p = p->next;
}
printf("\n");
}
void printP(Node p)
{
Node head = p;
while(head != p->prior)
{
printf("%d ",p->prior->value);
p = p->prior;
}
printf("\n");
}
int length(Node p)
{
Node head = p;
int len = 0;
while(head != p->next)
{
p = p->next;
len++;
}
return len;
}
Status insert_tail(Node p,Elementype value)
{
Node newnode = (Node)malloc(sizeof(struct node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = p;
p->prior->next = newnode;
newnode->prior = p->prior;
p->prior = newnode;
return T;
}