用C++实现单链表逆序

guang_5678 2010-08-21 10:53:46
用C++实现单链表逆序,方法有两三种的,但是具体能运行的程序写不好,哪位可以来试试。
...全文
1762 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhonylee 2010-08-21
  • 打赏
  • 举报
回复
void reserve_list(node* &head)
{
if((head == NULL) || (head->next == NULL))
{
return -1;
}
node* p1;
node* p2;
node* p3;
p1 = head;
p2 = head->next;
while(p2 != NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}

head->next = NULL;
head = p1;
}
大拙男 2010-08-21
  • 打赏
  • 举报
回复
这个在面试一家大公司的时候做过
牵扯到进程和线程
用代码好像也可以写
yshuise 2010-08-21
  • 打赏
  • 举报
回复
解決問題的時候能簡單就簡單,研究問題的時候能深就深。
Happy0403 2010-08-21
  • 打赏
  • 举报
回复
哇。。。有这种东西啊




我没学过C++.....
qq120848369 2010-08-21
  • 打赏
  • 举报
回复
能省事就省事
yshuise 2010-08-21
  • 打赏
  • 举报
回复
std::list不合胃口?
Happy0403 2010-08-21
  • 打赏
  • 举报
回复
for(p = temp; p->Next;p = p->Next)
{
pn = p->Next;
if(pn)
pn->Next = p;
else
break;
}


循环体严重错误。。。
Happy0403 2010-08-21
  • 打赏
  • 举报
回复
typedef struct List
{
struct List *Next;
}LIST;

void unList(LIST *List)
{
struct List *temp = List->Next;
List->Next = 0;
temp->Next = List;

struct List *p=temp, *pn, *pnn;
for(p = temp; p->Next;p = p->Next)
{
pn = p->Next;
if(pn)
pn->Next = temp;
else
break;
}
}

不知道会不会有错。。。
这个好麻烦。。。
云瑀 2010-08-21
  • 打赏
  • 举报
回复

# #include "stdafx.h"
# #include <iostream>
# using namespace std;
#
# template <typename T>
# struct MyNode
# {
# T id;
# MyNode * next;
# MyNode(T _id)
# {
# id = _id;
# next = NULL;
# }
# };
#
# template <typename T>
# class MyList
# {
# MyNode<T> * head;
# MyNode<T> * current;
# public:
# MyList() : head(NULL) {}
# void add(T value)
# {
# MyNode<T>* node = new MyNode<T>(value);
# if(head == NULL)
# {
# head = node;
# current = head;
# }
# else
# {
# current->next = node;
# current = node;
# }
# }
# void showAll()
# {
# MyNode<T> *go = head;
# while(go != NULL)
# {
# cout<<go->id<<endl;
# go = go->next;
# }
#
# }
# void reverse()
# {
# MyNode<T> *preNode = NULL;
# MyNode<T> *currentNode = head;
# MyNode<T> *nextNode = head->next;
# while(true)
# {
# nextNode = currentNode->next;
# currentNode->next = preNode;
# preNode = currentNode;
# currentNode = nextNode;
# if(currentNode->next == NULL)
# {
# head = currentNode;
# head->next = preNode;
# break;
# }
# }
# }
# };
#
#
# int _tmain(int argc, _TCHAR* argv[])
# {
# MyList<int> *list = new MyList<int>();
# list->add(123);
# list->add(456);
# list->add(789);
# list->showAll();
# list->reverse();
# cout<<"after reverse:"<<endl;
# list->showAll();
# system("pause");
# return 0;
# }
fight_flight 2010-08-21
  • 打赏
  • 举报
回复
一个模板链表

#include<iostream>
#include<conio.h>
#include<list>
using namespace std;

template<class T>
struct Node{
T value;
struct Node<T>* next;
Node(const T& v):value(v),next(NULL){}
~Node(){
delete next;
next=NULL;
cout<<"~Node()"<<endl;
}
};

template<class T,class N>
class Iterator{
public:
Iterator(T v=NULL):t(v){}
T& operator++()const{
t=t->next;
return t;
}
const N& operator*()const{
return t->value;
}
N&operator*(){
return t->value;
}
const T& getT()const{
return t;
}
Iterator& operator=(Iterator& other){
t=other.getT();
return *this;
}
const Iterator& operator=(Iterator& other)const{
t=other.getT();
return *this;
}
private:
mutable T t;
};

template<class T,class N>
bool operator!=(Iterator<T,N> i1,Iterator<T,N> i2){
return i1.getT()!=i2.getT();
}


template<class T>
class NList{
public:
typedef Iterator<Node<T>*,T> iterator;
typedef const Iterator<Node<T>*,T> const_iterator;
NList(const T& value){
head=tail=new Node<T>(value);
}
void addNode(const T& value){
tail->next=new Node<T>(value);
tail=tail->next;
}
iterator begin(){
return iterator(head);
}

iterator end(){
return iterator(tail->next);
}
void reverse(){
if(head==tail)
return ;
Node<T>* end=head,*begin=tail;
Node<T>* p1=head,*p2,*p3;
p2=p1->next;
p1->next=NULL;
while(p2!=tail){
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
p2->next=p1;
head=begin,tail=end;
}
~NList(){
delete head;
head=NULL;
tail=NULL;
cout<<"~NList()"<<endl;
}
private:
Node<T>* head;
Node<T>* tail;
};


int main(){
NList<int> nlist(1);
nlist.addNode(2);
nlist.addNode(3);
NList<int>::const_iterator p=nlist.begin(),end=nlist.end();
for(p=nlist.begin();p!=end;++p)
cout<<*p<<endl;
nlist.reverse();
end=nlist.end();
for(p=nlist.begin();p!=end;++p)
cout<<*p<<endl;
getch();
return 0;
}
pro_To_Life 2010-08-21
  • 打赏
  • 举报
回复
// List.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<malloc.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;
struct Node
{
int data;
struct Node * pnext;
};
/*********************************创建链表***************************************/
struct Node * CreateList()
{
struct Node * nd = (struct Node *)malloc(sizeof(struct Node));
nd->pnext = NULL;
return nd;
}
/*********************************添加节点***************************************/
void AddNode(struct Node * pNode,int data)
{
while(true)
{
if(pNode->pnext==NULL)
break;
pNode = pNode->pnext;
}
struct Node * nd = (struct Node *)malloc(sizeof(struct Node));
nd->data = data;
nd->pnext = NULL;
pNode->pnext = nd;
}
/*********************************输出链表数据***********************************/
void OutList(struct Node * pNode)
{
while(pNode)
{
if(pNode->pnext == NULL)
break;
pNode = pNode->pnext;
cout<<pNode->data<<" ";

}
}
/*********************************反向输出链表数据***********************************/
void ROutList(struct Node * pNode)
{
while(pNode)
{
if(pNode == NULL)
break;
cout<<pNode->data<<" ";
pNode = pNode->pnext;
}
}

/*********************************链表反向构建*******************************/
void ReverseList(struct Node * &pNode)
{
if((pNode == NULL)||(pNode->pnext == NULL))
{
cout<<"链表只有一个元素,无法反向!";
exit(1);
}
struct Node * p1;
struct Node * p2;
struct Node * p3;
p1 = pNode;
p2 = pNode->pnext;
while(p2 != NULL)
{
p3 = p2->pnext;
p2->pnext = p1;
p1 = p2;
p2 = p3;
}
pNode->pnext = NULL;
pNode = p1;
}
/*********************************删除链表占用空间*******************************/
void DeleteList(struct Node * pNode)
{
struct Node * p;
while(pNode)
{
p = pNode;
free(pNode);
pNode = p->pnext;
}
cout<<"释放链表占用内存空间成功!";
}

int _tmain(int argc, _TCHAR* argv[])
{
struct Node * list;
struct Node * rlist;
list = CreateList();
AddNode(list,1);
AddNode(list,2);
AddNode(list,3);
cout<<"新创建的链表输出:"<<endl;
OutList(list);
cout<<endl;
cout<<"反向构建后输出:"<<endl;
ReverseList(list);
ROutList(list);
//DeleteList(list);
system("pause");
return 0;
}



完整运行,稍有点问题,还没看出,但基本ok
samson_fan 2010-08-21
  • 打赏
  • 举报
回复

node* reserve_list(node* head)
{
node* newHead, newLast, p1, p2;
newHead = newLast = p1 = head;
while( NULL != p1->next )
{
p2 = p1->next->next;
newHead = p1->next;
newHead->next = p1;
p1 = p2;
}

if ( NULL != newLast )
{
newLast->next = NULL;
}

return newHead;
}
guang_5678 2010-08-21
  • 打赏
  • 举报
回复
谁有可以完整运行出来的?

64,632

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧