#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F -1
struct Node
{
int value;
struct Node *next;
};
int init(struct Node **head);
int insert_tail(struct Node *head, int value);
int insert_head(struct Node *head, int value);
int insert_index(struct Node *head, int index, int value);
int delete_index(struct Node *head, int index);
int delete_date(struct Node *head, int date);
int update_index(struct Node *head, int index, int value);
void update_date(struct Node *head, int date, int value);
int find_date(struct Node *head, int date);
int find_index(struct Node *head, int index);
int delete_date(struct Node *head, int date);
int count(struct Node *head);
void print(struct Node *head);
int main()
{
int i, ret;
struct Node *head;
ret = init(&head);
if(F == ret)
{
return 1;
}
for(i = 0; i < 10; i++)
{
insert_tail(head, i);
}
print(head);
printf("\n");
for(i = 0; i < 10; i++)
{
insert_head(head, i);
}
print(head);
ret = count(head);
printf("%d\n", ret);
delete_index(head, 0);
print(head);
delete_index(head, count(head) - 1);
print(head);
delete_index(head, 2);
print(head);
insert_index(head, 3, 100);
print(head);
insert_index(head, 0, 100);
print(head);
insert_index(head, count(head), 100);
print(head);
delete_date(head, 0);
print(head);
delete_date(head, 100);
print(head);
find_date(head, 4);
update_index(head, 14, 200);
print(head);
update_date(head, 4, 300);
print(head);
find_index(head, 1);
return 0;
}
int init(struct Node **head)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = 0;
newnode->next = NULL;
(*head) = newnode;
}
int insert_tail(struct Node *head, int value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->next = NULL;
newnode->value = value;
while(head->next != NULL)
{
while(head->next != NULL)
{
head = head->next;
}
head->next = newnode;
return T;
}
int insert_head(struct Node *head, int value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
void print(struct Node *head)
{
while(head->next != NULL)
{
printf("%d ", head->next->value);
head = head->next;
}
printf("\n");
}
int delete_index(struct Node *head, int index)
{
int i;
if(index < 0 || index >= count(head))
{
return F;
printf("out of the range");
}
for(i = 0; i < index; i++)
{
head = head->next;
}
struct Node *temp = head->next->next;
free(head->next);
head->next = temp;
return T;
}
int count(struct Node *head)
{
int count = 0;
while(head->next != NULL)
{
count++;
head = head->next;
}
return count;
}
int insert_index(struct Node *head, int index, int value)
{
if(index < 0 || index > count(head))
{
printf("out of the range");
return F;
}
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
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 delete_date(struct Node *head, int date)
{
int count = 0;
while(head->next != NULL)
{
if(head->next->value == date)
{
struct Node *temp = head->next->next;
free(head->next);
head->next = temp;
count++;
}
if(head->next != NULL && head->next->value != date)
{
head = head->next;
}
}
}
int update_index(struct Node *head, int index, int value)
{
int i;
if(index < 0 || index >= count(head))
{
return F;
}
for(i = 0; i < index; i++)
{
head = head->next;
}
head->next->value = value;
return T;
}
void update_date(struct Node *head, int date, int value)
{
while(head->next != NULL)
{
if(head->next->value == date)
{
head->next->value = value;
}
head = head->next;
}
}
int find_date(struct Node *head, int date)
{
int index = 0, count = 0;
while(head->next !=NULL)
{
if(head->next->value == date)
{
printf("index = %d\n", index);
count++;
}
index++;
head = head->next;
}
if(count != 0)
{
return T;
}
else
{
printf("not fount");
return F;
}
}
int find_index(struct Node *head, int index)
{
int i;
if(index < 0 || index >= count(head))
{
printf("out of range");
return F;
}
for(i = 0; i < index; i++)
{
head = head->next;
}
printf("%d\n", head->next->value);
return T;
}
#include <stdlib.h>
#define T 1
#define F -1
struct Node
{
int value;
struct Node *next;
};
int init(struct Node **head);
int insert_tail(struct Node *head, int value);
int insert_head(struct Node *head, int value);
int insert_index(struct Node *head, int index, int value);
int delete_index(struct Node *head, int index);
int delete_date(struct Node *head, int date);
int update_index(struct Node *head, int index, int value);
void update_date(struct Node *head, int date, int value);
int find_date(struct Node *head, int date);
int find_index(struct Node *head, int index);
int delete_date(struct Node *head, int date);
int count(struct Node *head);
void print(struct Node *head);
int main()
{
int i, ret;
struct Node *head;
ret = init(&head);
if(F == ret)
{
return 1;
}
for(i = 0; i < 10; i++)
{
insert_tail(head, i);
}
print(head);
printf("\n");
for(i = 0; i < 10; i++)
{
insert_head(head, i);
}
print(head);
ret = count(head);
printf("%d\n", ret);
delete_index(head, 0);
print(head);
delete_index(head, count(head) - 1);
print(head);
delete_index(head, 2);
print(head);
insert_index(head, 3, 100);
print(head);
insert_index(head, 0, 100);
print(head);
insert_index(head, count(head), 100);
print(head);
delete_date(head, 0);
print(head);
delete_date(head, 100);
print(head);
find_date(head, 4);
update_index(head, 14, 200);
print(head);
update_date(head, 4, 300);
print(head);
find_index(head, 1);
return 0;
}
int init(struct Node **head)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = 0;
newnode->next = NULL;
(*head) = newnode;
}
int insert_tail(struct Node *head, int value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->next = NULL;
newnode->value = value;
while(head->next != NULL)
{
while(head->next != NULL)
{
head = head->next;
}
head->next = newnode;
return T;
}
int insert_head(struct Node *head, int value)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
void print(struct Node *head)
{
while(head->next != NULL)
{
printf("%d ", head->next->value);
head = head->next;
}
printf("\n");
}
int delete_index(struct Node *head, int index)
{
int i;
if(index < 0 || index >= count(head))
{
return F;
printf("out of the range");
}
for(i = 0; i < index; i++)
{
head = head->next;
}
struct Node *temp = head->next->next;
free(head->next);
head->next = temp;
return T;
}
int count(struct Node *head)
{
int count = 0;
while(head->next != NULL)
{
count++;
head = head->next;
}
return count;
}
int insert_index(struct Node *head, int index, int value)
{
if(index < 0 || index > count(head))
{
printf("out of the range");
return F;
}
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if(NULL == newnode)
{
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 delete_date(struct Node *head, int date)
{
int count = 0;
while(head->next != NULL)
{
if(head->next->value == date)
{
struct Node *temp = head->next->next;
free(head->next);
head->next = temp;
count++;
}
if(head->next != NULL && head->next->value != date)
{
head = head->next;
}
}
}
int update_index(struct Node *head, int index, int value)
{
int i;
if(index < 0 || index >= count(head))
{
return F;
}
for(i = 0; i < index; i++)
{
head = head->next;
}
head->next->value = value;
return T;
}
void update_date(struct Node *head, int date, int value)
{
while(head->next != NULL)
{
if(head->next->value == date)
{
head->next->value = value;
}
head = head->next;
}
}
int find_date(struct Node *head, int date)
{
int index = 0, count = 0;
while(head->next !=NULL)
{
if(head->next->value == date)
{
printf("index = %d\n", index);
count++;
}
index++;
head = head->next;
}
if(count != 0)
{
return T;
}
else
{
printf("not fount");
return F;
}
}
int find_index(struct Node *head, int index)
{
int i;
if(index < 0 || index >= count(head))
{
printf("out of range");
return F;
}
for(i = 0; i < index; i++)
{
head = head->next;
}
printf("%d\n", head->next->value);
return T;
}