这个是今天上课自己编写的链表的程序。
来源:互联网 发布:java object类常用方法 编辑:程序博客网 时间:2024/06/06 00:12
#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 range\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 range\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;
}
}
阅读全文
0 0
- 这个是今天上课自己编写的链表的程序。
- 今天的上课总结
- 今天编写了自己的第一个程序
- 今天实验室的上课心得
- 这个是我今天自己写的对缓冲区的个人理解
- 今天在CSDN注册了这个blog,同时开始自己的游戏程序学习的旅程
- 今天给同学们上课的感受
- 编写opencv程序的小心这个漏洞
- 编写自己的ls程序
- 利用Date对象编写程序,判断并输出今天是开学的第几周,星期几。
- 今天编写双线程程序的思路
- 今天这个是mybatis与spring的整合
- 这个程序是怎样运行的?
- 今天是第一天所以写两个,这个是总结以前学到的一些知识看看自己以后的学习方向
- 今天开始编写俺自己的技术博客
- 今天来开始自己的代码编写之路
- 今天我编写了自己的第一个病毒
- 自己感觉这个是最经典的FLASH~
- G-Catch That Cow
- LeetCode 83. Remove Duplicates from Sorted List ***
- 关于单链表的增、减、改、查函数
- JavaScript for of循环
- Architecting HBase Applications学习(一)——HBase简介和HBase原理
- 这个是今天上课自己编写的链表的程序。
- 初学者必看:VC++、Win32 SDK、MFC的区别
- js数组的简单操作一
- Java排序算法
- 百度IFE学习笔记(二)js 添加DOM子节点
- 电商秒杀抢购
- “面向对象"和"面向过程"到底有什么区别?通俗易懂举例说明
- 关于链式栈的相关操作
- 关系数据库中的几种设计范式