今天主要学习的是 链表,单链,循环链表和双向链表,总体感觉还可以,还是有点懵懵的,需要勤加练习。
2017-05-21 22:52
337 查看
此程序主要是实现删,查,插,其中分为按位置和按值域两种方式,还有其他的方式。
以下程序可能细节不是太完善。
#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");
}
以下程序可能细节不是太完善。
#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");
}
相关文章推荐
- 今天学习了一下,CS代码按我们的要求加载前台代码需要的JS,感觉很不错【值可以按我们的需要的加载】
- 数据结构学习笔记 --- 线性表 (双向链表、循环链表)
- 今天发现,原来yahoo也不是不可取,还是有点东西可以搜搜的。Research on Location-Sensitive Search Engine
- 昨晚开始了为期3个月的初级德语课,课上大家跟老师咿咿呀呀,仿佛回到了蒙学时代,感觉还是不错的!在blog里增加一个GERMAN随笔分类主要是方便自己随时学习,勿怪!Vielen Dank!
- 郁闷,今天学习数据结构的链表,结果被模板给郁闷住了,动手能力还是要加强
- 感觉今天一天没做什么事情啊QAQ,学习了一下java的基本语法,编写了几个小程序,还是总结一下吧
- 数据结构学习日志之四--循环链表和双向链表
- 数据结构学习笔记 --- 线性表 (双向链表、循环链表)
- 数据结构学习笔记 --- 线性表 (双向链表、循环链表)
- 新手学习数据结构与算法---单链表(C++版, 跟C还是有点差别的)
- 数据结构学习笔记三 循环链表及双向链表
- 循环链表及双向链表的c语言实现
- 今天完成了一个主要框架了,可以歇歇了
- 因工作需要,从0开始学习.NET,以C#为主,有兴趣的可以慢慢看
- 今天主要练习定点转.
- 今天有些堕落,看得很少 没有静下心来学习 以后绝对不可以
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】详解Linux内核之双向循环链表
- 《Delphi 算法与数据结构》学习与感悟[10]: 双向链表
- 研究了两套经典源码,翻译了其中的注释,有学习需要的朋友可以下载
- 有点困惑了,不知道是从smartClient入手还是从做网站web入手学习.net技术