此程序主要是实现删,查,插,其中分为按位置和按值域两种方式,还有其他的方式。
以下程序可能细节不是太完善。
#include<stdio.h>
#include<stdlib.h>#define T 1
#define F -1
typedef int type;
struct Node
{
type value;
struct Node* next;
};
int init(struct Node** head);
int insert_tail(struct Node* head,type value);
int insert_index(struct Node* head,type value,int index);
int insert_head(struct Node* head,type value);
int print(struct Node* head);
int delete_index(struct Node* head,int index);
int delete_value(struct Node* head,type value);
int update_index(struct Node* head,int index,type newvalue);
int update_value(struct Node* head,type oldvalue,type newvalue);
int query_index(struct Node* head,int index);
int query_value(struct Node* head,type value);
int length(struct Node* head);
main()
{
struct Node* head;
init(&head);
int i;
for(i = 0;i < 10; i++)
{
insert_tail(head,i);
}
print(head);
for(i = 0;i < 10; i++)
{
insert_head(head,i);
}
print(head);
insert_index(head,100,0);
insert_index(head,100,length(head));
insert_index(head,100,11);
print(head);
delete_index(head,3);
delete_value(head,100);
delete_value(head,0);
print(head);
printf("length=%d\n",length(head));
update_index(head,5,89);
update_value(head,6,66);
print(head);
query_index(head,10);
query_value(head,66);
print(head);
return 0;
}
int insert_index(struct Node* head,type value,int index)
{
struct Node* newnode = (struct Node* )malloc(sizeof(struct Node));
if(NULL == newnode)
{
printf("error");
return F;
}
if(index < 0 || index >length(head))
{
printf("out of range\n");
return F;
}
int i;
for(i = 0;i < index;i++)
{
head = head->next;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int insert_head(struct Node* head,type value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
printf("error");
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int init(struct Node** head)
{
struct Node* newnode = (struct Node* )malloc(sizeof(struct Node));
if(NULL == newnode)
{
printf("error");
return F;
}
newnode->value = 0 ;
newnode->next = newnode;
*head = newnode;
return T;
}
int insert_tail(struct Node* head,type value)
{
struct Node* temp = head;
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
while(head->next != temp)
{
head = head->next;
}
newnode->value = value;
newnode->next = temp;
head->next = newnode;
return T;
}
int delete_index(struct Node* head,int index)
{
if(index < 0 || index > length(head))
{
printf("error");
return F;
}
int i;
for(i = 0;i < index ;i++)
{
head = head->next;
}
struct Node* temp = head->next;
head->next = head->next->next;
free(temp);
return T;
}
int delete_value(struct Node* head,type value)
{
int i ;
for(i = 0;i < length(head);i++)
{
if(head->next->value == value)
{
struct Node* temp2 = head->next;
head->next = head->next->next;
free(temp2);
}
head = head->next;
}
}
int update_index(struct Node* head,int index,type newvalue)
{
if(index < 0 || index > length(head))
{
return F;
}
int i;
for(i = 0; i <= index;i++ )
{
head = head->next;
}
head->value = newvalue;
}
int update_value(struct Node* head,type oldvalue,type newvalue)
{
int i;
for(i = 0;i < length(head);i++)
{
if(head->next->value == oldvalue)
{
head->next->value = newvalue;
}
head = head->next;
}
}
int query_index(struct Node* head,int index)
{
if(index < 0 || index > length(head))
{
return F;
}
int i;
for(i = 0; i <= index;i++ )
{
head = head->next;
}
printf("the value is=%d\n",head->value);
}
int query_value(struct Node* head,type value)
{
int i;
for(i = 0;i < length(head);i++)
{
if(head->next->value == value)
{
printf("the index is=%d",i);
}
}
}
int length(struct Node* head)
{
int count=0;
struct Node* temp = head;
while(head->next != temp)
{
count++;
head = head->next;
}
return count;
}
print(struct Node* head)
{
struct Node* temp = head;
while(temp->next!=head)
{
printf("%d ",temp->next->value);
temp = temp->next;
}
printf("\n");
}