Bài toán đảo ngược danh sách liên kết đơn.
Forum CNPM06 :: Khác
Trang 1 trong tổng số 1 trang
Bài toán đảo ngược danh sách liên kết đơn.
Buổi học đầu tiên SE101.E11 thầy Hoan có đưa ra một bài toán: đảo ngược danh sách liên kết đơn không dùng thêm bộ nhớ phụ. dưới là code bài này( tuy chưa đúng 100% với yêu cầu đề bài).
- Code:
#include<iostream>
#include<conio.h>
using namespace std;
struct Node{
int tuoi;
Node *next;
};
void In(Node *phead){
Node *p=phead;
while (p!=0){
cout<<p->tuoi<<endl;
p=p->next;
}
};
// ham dao nguoc danh sach lien ket
void Reserve(Node *&phead, Node *&ptail){
Node *pre=0;
while (phead!=0)
{
Node *pnext=phead->next;
phead->next=pre;
pre=phead;
phead=pnext;
}
phead=pre;
ptail=phead;
while(ptail->next!=0)
ptail=ptail->next;
};
int main()
{
Node *n1=new Node; n1->tuoi=1;
Node *n2=new Node; n2->tuoi=2;
Node *n3=new Node; n3->tuoi=3;
Node *n4=new Node; n4->tuoi=4;
n1->next=n2; n2->next=n3; n3->next=n4; n4->next=0; // 0 =NULL;
Node *phead;
Node *ptail;
phead=n1;
ptail=n4;
In(phead);
Reserve(phead, ptail);
In(phead);
_getch();
return 0;
}
Forum CNPM06 :: Khác
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|