关于 单链表 的一些程序
来源:互联网 发布:linux安装tortoisesvn 编辑:程序博客网 时间:2024/05/22 13:47
#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;
}
阅读全文
0 0
- 关于 单链表 的一些程序
- 关于反射的一些程序
- 关于程序的一些零碎思考
- 关于写程序的一些笑话
- 关于人工智能程序的一些设想
- C++ 关于友元的一些程序
- 关于程序方面的一些心得
- 关于控制台程序的一些想法
- 关于写程序的一些理解
- 一些关于程序猿的视频
- 关于AD12的一些程序注释
- 关于程序调式的一些心得!
- 关于发布程序上的一些小事
- 关于Android程序崩溃的一些记录
- opencv2.4.9 关于findcontours的一些程序
- 关于微信小程序自己的一些看法
- 关于单片机程序运行的一些理解
- 关于微信小程序的一些看法
- Android 加入 Kotlin 编程语言支持
- bash常见命令和vi编辑器
- windows(无GPU)+VS2013,caffe安装测试mnist问题及解决
- JavaScript引用类型
- Linux系统调度简介
- 关于 单链表 的一些程序
- [leetcode]: 83. Remove Duplicates from Sorted List
- Composer切换中国镜像
- python都能做什么
- 单链表的功能使用:初始化,赋值,输出,删除,按值查找,按地址查找,数据修改
- 【第七届蓝桥杯大赛个人赛(软件类)决赛B组 棋子换位】
- 2017XMUCTF水题解答
- java多线程三种实现
- 图(2)——有向图