面试题5—从尾到头打印链表
2017-06-26 10:17
411 查看
题目:输入一个链表的头结点,从尾到头反过来打印每个节点的值。
代码示例(用栈也行):
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
Node *head;
public:
LinkList();
~LinkList();
bool CreateList(int a[], int n);
bool InsertListF(int data);
void Disp(void);
friend bool InverseList(const LinkList &src, LinkList &dst);
};
LinkList::LinkList()
{
head = new Node();
head->next = NULL;
}
LinkList::~LinkList()
{
Node *p = head->next;
Node *pre = head;
while (p != NULL)
{
delete pre;
pre = p;
p = p->next;
}
delete pre;
}
bool LinkList::CreateList(int a[], int n)
{
if (n <= 0)
return false;
Node *pre = head;
for (int i = 0; i < n; i++)
{
Node *p = new Node();
p->data = a[i];
pre->next = p;
pre = p;
}
pre->next = NULL;
return true;
}
bool LinkList::InsertListF(int data)
{
Node *p = new Node();
p->data = data;
p->next = head->next;
head->next = p;
return true;
}
void LinkList::Disp()
{
Node *p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
bool InverseList(const LinkList &src, LinkList &dst)
{
if (src.head->next == NULL)
return false;
Node *p = src.head->next;
while (p != NULL)
{
int temp = p->data;
dst.InsertListF(temp);
p = p->next;
}
}
void main()
{
LinkList L1, L2;
int a[4] = { 1,2,4,5 };
//源链表
L1.CreateList(a, 4);
L1.Disp();
//源链表反向输出
InverseList(L1, L2);
L2.Disp();
}
代码示例(用栈也行):
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
Node *head;
public:
LinkList();
~LinkList();
bool CreateList(int a[], int n);
bool InsertListF(int data);
void Disp(void);
friend bool InverseList(const LinkList &src, LinkList &dst);
};
LinkList::LinkList()
{
head = new Node();
head->next = NULL;
}
LinkList::~LinkList()
{
Node *p = head->next;
Node *pre = head;
while (p != NULL)
{
delete pre;
pre = p;
p = p->next;
}
delete pre;
}
bool LinkList::CreateList(int a[], int n)
{
if (n <= 0)
return false;
Node *pre = head;
for (int i = 0; i < n; i++)
{
Node *p = new Node();
p->data = a[i];
pre->next = p;
pre = p;
}
pre->next = NULL;
return true;
}
bool LinkList::InsertListF(int data)
{
Node *p = new Node();
p->data = data;
p->next = head->next;
head->next = p;
return true;
}
void LinkList::Disp()
{
Node *p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
bool InverseList(const LinkList &src, LinkList &dst)
{
if (src.head->next == NULL)
return false;
Node *p = src.head->next;
while (p != NULL)
{
int temp = p->data;
dst.InsertListF(temp);
p = p->next;
}
}
void main()
{
LinkList L1, L2;
int a[4] = { 1,2,4,5 };
//源链表
L1.CreateList(a, 4);
L1.Disp();
//源链表反向输出
InverseList(L1, L2);
L2.Disp();
}
相关文章推荐
- 【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)
- 面试题5-从尾到头打印链表
- <剑指offer 面试题6-2>从尾到头打印链表 Java
- 【面试题五】从尾到头打印链表
- P51、面试题5:从尾到头打印链表
- 剑指Offer: 面试题5 从尾到头打印链表
- 面试题(六)从尾到头打印链表
- 剑指Offer 面试题5 从尾到头打印链表
- 剑指offer|面试题5:从尾到头打印链表(Java代码)
- 剑指Offer:面试题5——从尾到头打印链表(java实现)
- 剑指offer面试题5:从尾到头打印链表
- 剑指offer--面试题5:从尾到头打印链表
- 【剑指offer】面试题5:从尾到头打印链表 java
- 面试题五: 从尾到头打印链表
- 面试题6:从尾到头打印链表(offer)
- 面试题5:从尾到头打印链表
- 剑指Offer---面试题5:从尾到头打印链表
- 剑指offer - 面试题5:从尾到头打印链表
- 面试题之【从尾到头打印链表】
- 剑指Offer_面试题05_从尾到头打印链表