这个是今天上课自己编写的链表的程序。
2017-05-20 22:33
447 查看
#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_head(struct Node* head,type value);
int insert_index(struct Node* head,type value,int index);
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 value);
int update_value(struct Node* head,type ole_value, type new_value);
int query_index(struct Node* head,int index);
int query_value(struct Node* head,type value);
int length(struct Node*head);
void print(struct Node* head);
main()
{
int ret;
int i;
struct Node* head;
ret = init(&head);
if(F == ret)
{
return -1;
}
for(i = 0;i < 10;i++)
{
insert_tail(head, i);
}
for(i = 0;i < 10;i++)
{
insert_head(head, i);
}
print(head);
printf("\n");
printf("length = %d \n",length(head));
delete_index(head,1);
delete_index(head,length(head)-2);
delete_index(head,5);
print(head);
printf("\n");
insert_index(head,99,0);
insert_index(head,99,7);
insert_index(head,99,length(head)-1);
print(head);
printf("\n");
delete_value(head,99);
print(head);
printf("\n");
update_index(head,8,-1);
print(head);
printf("\n");
update_value(head,8,100);
print(head);
printf("\n");
printf("the value is %d",query_index(head,16));
printf("\n");
query_value(head,0);
printf("\n");
return 0;
}
int init(struct Node** head)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(newnode == NULL)
{
return F;
}
newnode->value = 0;
newnode->next = NULL;
(*head) = newnode;
return T;
}
int insert_tail(struct Node* head,type value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = NULL;
while(head->next!= NULL)
{
head = 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(newnode == NULL)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int insert_index(struct Node* head,type value,int index)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
int i;
if(index < 0 || index > length(head))
{
printf("out of range");
return F;
}
for(i = 0;i < index;i++)
{
head = head->next;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int delete_index(struct Node* head,int index)
{
int i;
if(index < 0 || index >= length(head))
{
printf("out of r
a5df
ange\n");
return F;
}
for(i = 0;i < index;i++)
head = head->next;
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
return T;
}
int delete_value(struct Node* head,type value)
{
int index ,i;
int len;
len = length(head);
for(index = 0 ;index < len-1;index++)
{
if(head->next->value == value)
{
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
}
else
{
head = head->next;
}
}
return T;
}
int update_index(struct Node* head,int index,type value)
{
if(index < 0 || index > length(head))
{
printf("out of the range");
}
int i;
for(i = 0;i <= index;i++)
{
head = head->next;
}
head->value = value;
return T;
}
int update_value(struct Node* head,type old_value, type new_value)
{
int count=0,i;
int len;
len = length(head);
for(i = 0;i < len;i++)
{
if(head->value == old_value)
{
count++;
head->value = new_value;
}
head = head->next;
}
if(0 == count)
{
printf("no number\n");
}
return T;
}
int query_index(struct Node* head,int index)
{
int i;
for(i = 0;i <= index; i++)
{
head = head->next;
}
return head->value;
}
int query_value(struct Node* head,type value)
{
int i,count=0;
int len;
len = length(head);
for(i = 0;i < len-1; i++)
{
if(head->next->value == value)
{
printf("there are in the %d\n",i);
}
head = head->next;
}
}
int length(struct Node* head)
{
int count = 0 ;
while(head!= NULL)
{
count++;
head = head->next;
}
return count;
}
void print(struct Node* head)
{
while(head->next!=NULL)
{
printf("%d ",head->next->value);
head = head->next;
}
}
#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_head(struct Node* head,type value);
int insert_index(struct Node* head,type value,int index);
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 value);
int update_value(struct Node* head,type ole_value, type new_value);
int query_index(struct Node* head,int index);
int query_value(struct Node* head,type value);
int length(struct Node*head);
void print(struct Node* head);
main()
{
int ret;
int i;
struct Node* head;
ret = init(&head);
if(F == ret)
{
return -1;
}
for(i = 0;i < 10;i++)
{
insert_tail(head, i);
}
for(i = 0;i < 10;i++)
{
insert_head(head, i);
}
print(head);
printf("\n");
printf("length = %d \n",length(head));
delete_index(head,1);
delete_index(head,length(head)-2);
delete_index(head,5);
print(head);
printf("\n");
insert_index(head,99,0);
insert_index(head,99,7);
insert_index(head,99,length(head)-1);
print(head);
printf("\n");
delete_value(head,99);
print(head);
printf("\n");
update_index(head,8,-1);
print(head);
printf("\n");
update_value(head,8,100);
print(head);
printf("\n");
printf("the value is %d",query_index(head,16));
printf("\n");
query_value(head,0);
printf("\n");
return 0;
}
int init(struct Node** head)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(newnode == NULL)
{
return F;
}
newnode->value = 0;
newnode->next = NULL;
(*head) = newnode;
return T;
}
int insert_tail(struct Node* head,type value)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
newnode->value = value;
newnode->next = NULL;
while(head->next!= NULL)
{
head = 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(newnode == NULL)
{
return F;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int insert_index(struct Node* head,type value,int index)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
if(NULL == newnode)
{
return F;
}
int i;
if(index < 0 || index > length(head))
{
printf("out of range");
return F;
}
for(i = 0;i < index;i++)
{
head = head->next;
}
newnode->value = value;
newnode->next = head->next;
head->next = newnode;
return T;
}
int delete_index(struct Node* head,int index)
{
int i;
if(index < 0 || index >= length(head))
{
printf("out of r
a5df
ange\n");
return F;
}
for(i = 0;i < index;i++)
head = head->next;
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
return T;
}
int delete_value(struct Node* head,type value)
{
int index ,i;
int len;
len = length(head);
for(index = 0 ;index < len-1;index++)
{
if(head->next->value == value)
{
struct Node* temp = head->next->next;
free(head->next);
head->next = temp;
}
else
{
head = head->next;
}
}
return T;
}
int update_index(struct Node* head,int index,type value)
{
if(index < 0 || index > length(head))
{
printf("out of the range");
}
int i;
for(i = 0;i <= index;i++)
{
head = head->next;
}
head->value = value;
return T;
}
int update_value(struct Node* head,type old_value, type new_value)
{
int count=0,i;
int len;
len = length(head);
for(i = 0;i < len;i++)
{
if(head->value == old_value)
{
count++;
head->value = new_value;
}
head = head->next;
}
if(0 == count)
{
printf("no number\n");
}
return T;
}
int query_index(struct Node* head,int index)
{
int i;
for(i = 0;i <= index; i++)
{
head = head->next;
}
return head->value;
}
int query_value(struct Node* head,type value)
{
int i,count=0;
int len;
len = length(head);
for(i = 0;i < len-1; i++)
{
if(head->next->value == value)
{
printf("there are in the %d\n",i);
}
head = head->next;
}
}
int length(struct Node* head)
{
int count = 0 ;
while(head!= NULL)
{
count++;
head = head->next;
}
return count;
}
void print(struct Node* head)
{
while(head->next!=NULL)
{
printf("%d ",head->next->value);
head = head->next;
}
}
相关文章推荐
- 今天编写了自己的第一个程序
- 今天在CSDN注册了这个blog,同时开始自己的游戏程序学习的旅程
- 今天开始学Java 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。
- 哪位大神能帮帮我编写下双向链表这个程序
- 自己动手编写一个简单的U盘杀毒程序
- 利用Recognizer编写自己的开机自启动程序
- 今天终于搞清了Serializable ,虽然不是自己写的程序!(去马赛克版)
- 呵..今天开心..申请了这个weblog,在这里可以说一些自己事..
- 今天发现CASSINI这个程序正是我需要了解的,有空一定拜读,特立此文
- 如何编写自己的缓冲区溢出利用程序
- 半菱形,今天第二个自己写的程序
- 利用Recognizer编写自己的开机自启动程序
- 自己编写安装制作程序
- 如何编写自己的缓冲区overflow程序
- 今天开贴,立志开始学习编写程序,大家帮忙呀, 小生我先谢谢了 !!
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- 今天我也在这个计算机技术人员云集之处书写我的文章,感觉自己的成长和责任感,我将不负程序员的称呼!
- 自己编写的第一个程序!
- 在Delphi中编写自己的链表管理类。 推荐
- 自己动手编写devExpress组件自动安装程序