【C++单链表就地逆置】程序无error错误,求指点TAT。

#include<iostream>usingnamespacestd;structNode{intdata;Node*next;};classLinkList{Node... #include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
Node *head;
public:
LinkList(){head=NULL;}
void OutputList();
void InsertList();
void Reverse();
Node *Gethead(){return head;}
private:
Node *first;
};
void LinkList::Reverse()
{
Node *p,*u;
p = first->next;
first->next = NULL;
while (p!=NULL)
{
u = p->next;
p->next = first->next;
first->next = p;
p = u;
}
}
void LinkList::InsertList()
{
int n;
Node *s;
int a[10]={1,2,3,4,5,6,7,8,9,10};
first=new Node; first->next=NULL;
for (int i=0;i<n;i++)
{
s=new Node; s->data=a[i];
s->next=first->next;first->next=s;
}
}
void LinkList::OutputList()
{
Node *p = head;
while (p!=NULL)
{
cout << p->data <<"";
p = p->next;
}
cout << endl;
}
int main()
{
LinkList A;
A.InsertList;
cout << "原链表";
A.OutputList();
cout << "现链表";
A.Reverse;
A.OutputList();
return 0;
}
展开
 我来答
百度网友cd8541ae4
2017-04-01 · TA获得超过264个赞
知道小有建树答主
回答量:126
采纳率:100%
帮助的人:118万
展开全部
#include <iostream>
using namespace std;
struct Node
{
 int data;
 Node *next;
};
class LinkList
{
 Node *head;
public:
 LinkList(){ head = NULL; }
 void OutputList();
 void InsertList();
 void Reverse();
 Node *Gethead(){ return head; }
};
void LinkList::Reverse()
{
 if (head == NULL || head->next == NULL)
  return;
 Node *curr = head;
 Node *aft = head->next;
 head->next = NULL;
 while (aft != NULL)
 {
  Node *temp = aft->next;
  aft->next = curr;
  curr = aft;
  aft = temp;
 }
 head = curr;
}
void LinkList::InsertList()
{
 int n = 10;
 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 for (int i = 0; i<n; i++)
 {
  Node * s = new Node;
  s->data = a[i];
  s->next = head;
  head = s;
 }
}
void LinkList::OutputList()
{
 Node *p = head;
 while (p != NULL)
 {
  cout << p->data << " ";
  p = p->next;
 }
 cout << endl;
}
int main()
{
 LinkList A;
 A.InsertList();
 cout << "原链表";
 A.OutputList();
 cout << "现链表";
 A.Reverse();
 A.OutputList();
 return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式