把list倒序排列,不申请空间。
2005-12-15 09:21
435 查看
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct node{
int num;
node *next;
}node_t;
void reverse_list(node_t **pheadmain);
void print_list(node_t *pheadmain);
void free_list(node_t *pheadmain);
void main()
{
node_t *phead = NULL;
node_t *pheadtemp = NULL;
node_t *temp = NULL;
int i;
phead = (node_t *)malloc(sizeof(node_t));
phead->num = 0;
phead->next = NULL;
pheadtemp = phead;
for(i=1; i<5; i++)
{
temp = (node_t *)malloc(sizeof(node_t));
temp->num = i;
temp->next = NULL;
pheadtemp->next = temp;
pheadtemp = pheadtemp->next;
}
print_list(phead);
reverse_list(&phead);
print_list(phead);
free_list(phead);
}
void free_list(node_t *pheadmain)
{
node_t *phead = pheadmain;
node_t *pheadtemp = NULL;
while(phead != NULL)
{
pheadtemp = phead->next;
free(phead);
phead = pheadtemp;
}
}
void print_list(node_t *pheadmain)
{
node_t *phead = pheadmain;
while(phead != NULL)
{
cout<<phead->num<<" ";
phead = phead->next;
}
cout<<endl;
}
void reverse_list(node_t **pheadmain)
{
node_t *phead = *pheadmain;
node_t *pheadPPP = NULL;
node_t *pheadQQQ = NULL;
pheadPPP = phead;
phead = phead->next;
pheadPPP->next = NULL;/*init*/
while(phead != NULL)
{
pheadQQQ = phead->next;
phead->next = pheadPPP;
pheadPPP = phead;
phead = pheadQQQ;
}
*pheadmain = pheadPPP;
}
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct node{
int num;
node *next;
}node_t;
void reverse_list(node_t **pheadmain);
void print_list(node_t *pheadmain);
void free_list(node_t *pheadmain);
void main()
{
node_t *phead = NULL;
node_t *pheadtemp = NULL;
node_t *temp = NULL;
int i;
phead = (node_t *)malloc(sizeof(node_t));
phead->num = 0;
phead->next = NULL;
pheadtemp = phead;
for(i=1; i<5; i++)
{
temp = (node_t *)malloc(sizeof(node_t));
temp->num = i;
temp->next = NULL;
pheadtemp->next = temp;
pheadtemp = pheadtemp->next;
}
print_list(phead);
reverse_list(&phead);
print_list(phead);
free_list(phead);
}
void free_list(node_t *pheadmain)
{
node_t *phead = pheadmain;
node_t *pheadtemp = NULL;
while(phead != NULL)
{
pheadtemp = phead->next;
free(phead);
phead = pheadtemp;
}
}
void print_list(node_t *pheadmain)
{
node_t *phead = pheadmain;
while(phead != NULL)
{
cout<<phead->num<<" ";
phead = phead->next;
}
cout<<endl;
}
void reverse_list(node_t **pheadmain)
{
node_t *phead = *pheadmain;
node_t *pheadPPP = NULL;
node_t *pheadQQQ = NULL;
pheadPPP = phead;
phead = phead->next;
pheadPPP->next = NULL;/*init*/
while(phead != NULL)
{
pheadQQQ = phead->next;
phead->next = pheadPPP;
pheadPPP = phead;
phead = pheadQQQ;
}
*pheadmain = pheadPPP;
}
相关文章推荐
- java List Collections.sort 排序 支持 对象属性 普通数据类型 升序 降序 正序 倒序 排列
- sql语句面试题(查询按照申请日期倒序排列的前10个记录)
- List的正序和倒序排列
- sql语句面试题(查询按照申请日期倒序排列的前10个记录)
- python list倒序排列
- hdu 1671 Phone List && POJ 3630 Phone List(静态申请空间)
- 将list集合的元素按照添加顺序的倒序进行排列取出
- (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn rever
- Python面试题 之 Uniquify a list 不使用set去除一个列表中的重复项,并且保持原有的排列顺序
- 把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
- javascript select列表内容按字母倒序排序与按列表倒序排列
- 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。 你能帮帮小Q吗?
- c++中在堆和栈中申请空间的差别
- 给出1..n的一个排列,其中缺少2个元素,用O(1)的空间找到那2个缺失的元素
- 全排列(List)实现
- C++测试申请最大内存空间
- JAVA版List<Map>排序,按字符或数字类型排序,支持正序倒序
- 重新排列链表Reorder List
- 网上申请免费空间,先保存下来。。
- memcpy函数导致申请的其他堆栈空间被修改,执行程序出错