目录
C语言文件更新
目录链表尝试写双头链表1.双头链表12.双向列表3.双向添加数据4.双向环链表(程序运行有点小问题)5.双向循环列表实验目的
结束
链表
list.h
#ifndef _LIST_H_
#define _LIST_H_
typedef struct _node_
{
int value;
struct _node_ *next;
}Node;
typedef struct _list_
{
Node *head;
}List;
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list(List list)
{
Node *p;
for(p=list.head;p;p=p->next)
{
printf("%d\t",p->value);
}
}
void add_list_head(List *list,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->value=number;
Node *last=list->head;
if (last)
{
while(last->next)
{
last=last->next;
}
last->next=p;
}else
{
list->head=p;
}
}
void list_delete_number(List *list,int number)
{
Node *p;
Node *q;
for(q=NULL,p=list->head;p;q=p,p=p->next)
{
if (p->value==number)
{
if (q)
{
q->next=p->next;
}else
{
list->head=p->next;
}
free(p);
break;
}
}
}
void delete_list (List *list)
{
Node *p;
Node *q;
for(p=list->head;p;p=q)
{
q=p->next;
free(p);
list->head=NULL;
}
}
int main()
{
int number;
List list;
list.head=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=-1)
{
printf("%d\n",number);
add_list_head(&list,number);//链表尾部插入数据
}else
{
printf("输入结束\n");
}
}while(number!=-1);
print_list(list);//遍历显示链表
printf("\n请输入要删除的数值");
scanf("%d",&number);
list_delete_number(&list,number);
print_list(list);//遍历显示链表
delete_list (&list);//删除整个链表
print_list(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
尝试写双头链表
1.双头链表1
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list_head(List list)
{
Node *p;
for(p=list.head;p;p=p->next)
{
printf("%d\t",p->value);
}
}
void print_list_tail(List list)
{
Node *p;
for(p=list.tail;p;p=p->previous)
{
printf("%d\t",p->value);
}
}
int main()
{
int number;
List list;
list.head=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=0)
{
printf("%d\n",number);
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->previous=NULL;
p->value=number;
Node *last=list.head;
if (last)
{
while(last->next)
{
last=last->next;
}
last->next=p;
p->previous=last;
list.tail=p;
}else
{
list.head=p;
}
}
}while(number!=0);
print_list_head(list);//遍历显示链表
printf("\n倒叙输出\n");
print_list_tail(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
2.双向列表
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list(List list)
{
printf("\n正向输出\n");
Node *p;
for(p=list.head;p;p=p->next)
{
printf("%d\t",p->value);
}
printf("\n反向输出\n");
for(p=list.tail;p;p=p->previous)
{
printf("%d\t",p->value);
}
}
int main()
{
int number;
List list;
list.head=NULL;
list.tail=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=0)
{
printf("%d\n",number);
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->previous=NULL;
p->value=number;
Node *last=list.head;
if (last)
{
while(last->next)
{
last=last->next;
}
last->next=p;
p->previous=last;
list.tail=p;
}else
{
list.head=p;//头没有就是默认尾巴也没有
list.tail=p;//尾巴没有就是默认头也没有
}
}
}while(number!=0);
print_list(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
3.双向添加数据
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list(List list)
{
printf("\n正向输出\n");
Node *p;
for(p=list.head;p;p=p->next)
{
printf("%d\t",p->value);
}
printf("\n反向输出\n");
for(p=list.tail;p;p=p->previous)
{
printf("%d\t",p->value);
}
}
void add_list (List *list,int number,int mode)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->previous=NULL;
p->value=number;
if (list->head)
{
if (mode==1)
{
(list->tail)->next=p;
p->previous=list->tail;
list->tail=p;
}else if (mode==-1)
{
(list->head)->previous=p;
p->next=list->head;
list->head=p;
}
}else
{
list->head=p;
list->tail=p;
}
}
int main()
{
int number;
List list;
list.head=NULL;
list.tail=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=0)
{
printf("%d\n",number);
add_list(&list,number,1);
}
}while(number!=0);
print_list(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
4.双向环链表(程序运行有点小问题)
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list(List list)
{
printf("\n正向输出\n");
Node *p;
int index;
for(p=list.head,index=0;(p !=list.head || index ==0 );p=p->next)
{
printf("%d\t",p->value);
index++;
}
printf("\t一共有%d个数据\n",index);
}
void add_list (List *list,int number,int mode)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->previous=NULL;
p->value=number;
if (list->head)
{
if (mode==1)
{
(list->tail)->next=p;
p->previous=list->tail;
list->tail=p;
(list->tail)->next=list->head;
}else if (mode==-1)
{
(list->head)->previous=p;
p->next=list->head;
list->head=p;
(list->head)->previous=list->tail;
}
}else
{
p->next=p;
p->previous=p;
list->head=p;
list->tail=p;
}
}
int main()
{
int number;
List list;
list.head=NULL;
list.tail=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=0)
{
printf("%d\n",number);
add_list(&list,number,1);
}
}while(number!=0);
print_list(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
5.双向循环列表
双向循环列表
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_list(List list)
{
printf("\n正向输出\n");
Node *p;
int index;
for(p=list.head,index=0;((p !=list.head || index ==0 )&& p);p=p->next)
{
printf("%d\t",p->value);
index++;
}
printf("\t一共有%d个数据\n",index);
printf("\n反向输出\n");
for(p=list.tail,index=0;((p !=list.tail || index ==0 )&& p);p=p->previous)
{
printf("%d\t",p->value);
index++;
}
printf("\t一共有%d个数据\n",index);
}
void add_list (List *list,int number,int mode)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next=NULL;
p->previous=NULL;
p->value=number;
if (list->head)
{
if (mode==1)
{
(list->tail)->next=p;
p->previous=list->tail;
list->tail=p;
}else if (mode==-1)
{
(list->head)->previous=p;
p->next=list->head;
list->head=p;
}
}else
{
p->next=p;
p->previous=p;
list->head=p;
list->tail=p;
}
(list->tail)->next=list->head;
(list->head)->previous=list->tail;
}
int main()
{
int number;
List list;
list.head=NULL;
list.tail=NULL;
do{
printf("请输入数值");
scanf("%d",&number);
if (number!=0)
{
printf("%d\n",number);
if (number>0)
{
add_list(&list,number,1);
}else
{
add_list(&list,number,-1);
}
}
}while(number!=0);
print_list(list);//遍历显示链表
printf("\n程序正常运行");
return 0;
}
以零为分界线,进行分组。左边大于零右边小于零。
实验目的
输入 17936871 0结束 输出 1 7 9 1 7 3 6 8 9 1 7 3 6 7 8 9 1 7 3 6 1 7 8 9 最终输出为 1 7 3 6 1 7 8 9
目录链表尝试写双头链表1.双头链表12.双向列表3.双向添加数据4.双向环链表(程序运行有点小问题)5.双向循环列表实验目的
结束
结束