单链表逆置算法(c++)
来源:互联网 发布:网购省钱软件 编辑:程序博客网 时间:2024/06/02 00:20
在VC环境中可调试:
#include<stdio.h>
#include<malloc.h>
typedef int elemtype;
typedef struct linknode
...{ elemtype data;
struct linknode *next;
}nodetype;
nodetype * createlist()
...{ elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
printf("Create a single Link list. ");
while(1)
...{
printf("Pls Enter the elements of the list(0:Exit)");
scanf("%d",&d);
if(d==0)
break;
if(i==1)
...{
h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
...{
s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}
void disp(nodetype *h)
...{
nodetype *p=h;
printf("Print the single link list: ");
if(p==NULL)
printf("The list has no elements! ");
while(p!=NULL)
...{
printf("%d ",p->data);
p=p->next;
}
printf(" ");
}
nodetype *reverse(nodetype *h)
...{
nodetype *p,*q,*r;
p=h;
if(p->next=NULL)
...{
printf("The reversed link list is: ");
return NULL;
}
else
...{
p=h;
q=p->next;
while(q!=NULL)
...{
r=q->next;
q->next=p;
p=q;
q=r;
}
h->next=NULL;
h=p;
return h;
}
}
void main()
...{
nodetype *head;
head=createlist();
disp(head);
head=reverse(head);
disp(head);
}
#include<malloc.h>
typedef int elemtype;
typedef struct linknode
...{ elemtype data;
struct linknode *next;
}nodetype;
nodetype * createlist()
...{ elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
printf("Create a single Link list. ");
while(1)
...{
printf("Pls Enter the elements of the list(0:Exit)");
scanf("%d",&d);
if(d==0)
break;
if(i==1)
...{
h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
...{
s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}
void disp(nodetype *h)
...{
nodetype *p=h;
printf("Print the single link list: ");
if(p==NULL)
printf("The list has no elements! ");
while(p!=NULL)
...{
printf("%d ",p->data);
p=p->next;
}
printf(" ");
}
nodetype *reverse(nodetype *h)
...{
nodetype *p,*q,*r;
p=h;
if(p->next=NULL)
...{
printf("The reversed link list is: ");
return NULL;
}
else
...{
p=h;
q=p->next;
while(q!=NULL)
...{
r=q->next;
q->next=p;
p=q;
q=r;
}
h->next=NULL;
h=p;
return h;
}
}
void main()
...{
nodetype *head;
head=createlist();
disp(head);
head=reverse(head);
disp(head);
}
第二个,未编译:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node...{
int data;
struct node *next;
}Node,*LinkList;
void CreateList(LinkList *head)
...{ LinkList p,q;
int x;
if((*head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
(*head)->next=NULL;
q=(*head);
printf("please input data: ");
scanf("%d",&x);
do...{
if((p=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
p->data=x;
p->next=NULL;
q->next=p;
q=p;
p=NULL;
printf("please input number: ");
fflush(stdin);
scanf("%d",&x);
}while(x!=-1);
} void Reverse(LinkList head)
...{LinkList p,q,s=NULL,Head;
int count=0,i;
p=head->next;
while(p)
...{ count++;
p=p->next;
}
p=head->next;
if((Head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
Head->next=p;
head->next=NULL;
for(i=0;i<count;i++)
...{ p=Head->next;
Head->next=p->next;
p->next=NULL;
if(s==NULL)
s=p;
else
...{ p->next=s;
s=p;
}
}
head->next=s;
free(Head);
} void Print(LinkList head)
...{ LinkList p;
p=head->next;
while(p)
...{ printf("%d ",p->data);
p=p->next;
}
} main()
...{ LinkList head;
CreateList(&head);
Print(head);
Reverse(head);
printf(" ");
Print(head);
}
#include<stdlib.h>
#include<malloc.h>
typedef struct node...{
int data;
struct node *next;
}Node,*LinkList;
void CreateList(LinkList *head)
...{ LinkList p,q;
int x;
if((*head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
(*head)->next=NULL;
q=(*head);
printf("please input data: ");
scanf("%d",&x);
do...{
if((p=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
p->data=x;
p->next=NULL;
q->next=p;
q=p;
p=NULL;
printf("please input number: ");
fflush(stdin);
scanf("%d",&x);
}while(x!=-1);
} void Reverse(LinkList head)
...{LinkList p,q,s=NULL,Head;
int count=0,i;
p=head->next;
while(p)
...{ count++;
p=p->next;
}
p=head->next;
if((Head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
Head->next=p;
head->next=NULL;
for(i=0;i<count;i++)
...{ p=Head->next;
Head->next=p->next;
p->next=NULL;
if(s==NULL)
s=p;
else
...{ p->next=s;
s=p;
}
}
head->next=s;
free(Head);
} void Print(LinkList head)
...{ LinkList p;
p=head->next;
while(p)
...{ printf("%d ",p->data);
p=p->next;
}
} main()
...{ LinkList head;
CreateList(&head);
Print(head);
Reverse(head);
printf(" ");
Print(head);
}
- 单链表逆置算法(c++)
- 单链表逆置递归算法(C语言)
- C算法
- C算法
- 算法 C
- C 算法
- C++--算法
- [C++] [算法] KMP算法
- [C/C++]洗牌算法
- [C/C++] 常用算法
- c语言经典算法算法
- 算法:二分查找算法(c++)
- 【C++】【啊哈!算法】Dijkstra算法
- 算法大全(c,c++)
- 算法大全(C,C++)
- 懂c/c++/java/算法
- 排序算法实现(C/C++)
- C/C++“大数相加算法”
- 处理顶点数据
- VS2005 在WinForm上是否有新特点?
- DirectX设备状态
- “形式语言与自动机”中的基本概念与部分结论:文法-语言-自动机
- google adsense 在firefox中
- 单链表逆置算法(c++)
- WINDOWS SHARE POINT 3 INSTALLATION
- 《贞观长歌》
- 避免使用隐式类型转换
- 一个经理人从月薪300元到15000元的职场经历
- MFC中CAsyncSocket及其派生类对象跨线程使用方法
- 求职空间ujobspace
- socket UDP 代码
- 人是如何变聪明的