
【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;
} 展开
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;
} 展开
展开全部
#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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询