显示反向顺序,但不显示非反向顺序 c++
Displays reverse order but not the non-reverse order c++
它不会以非反向顺序 (cde( 显示字符串字符数组,但会以相反顺序 (edc( 显示数组。你能帮忙解释为什么它不显示 cde 吗?
我尝试更改变量名称,但我是一名新程序员,所以我真的不知道该怎么做。
#include <iostream>
using namespace std;
// of linked list in reverse order
// Structure of a node
template<class T>
struct listrec2
{
T value; //corresponds to data
listrec2 *next; //points to next node
listrec2 *prev; //points to previous nod
};
template<class T>
void downwardSearch(listrec2<T> *head)
//traverse from start of linked list to end of linked list
//print out value of each node along way
{
char s[] = { 'c', 'd', 'e' };
// if you wanted to make it in the function listrec2<T> *tail;
listrec2<T> *current;
listrec2<T> *tail;
listrec2<T> value;
head = tail = new listrec2<T>; // make a new node
head->value = s[0];
tail = NULL;
head = NULL;
for (int i = 1; i < strlen(s); i++)
{
current = new listrec2<T>; //makes new node
current->value = s[i];
current->next = NULL;
current->prev = tail;
tail->next = current;
tail = current;
}
listrec2<T> *ptr;
ptr = head;
cout << "The array in non-reverse order: " << endl;
while (ptr != NULL)
{
cout << ptr->value;
ptr = ptr->next;
}
}
template<class T>
void upwardSearch(listrec2<T> *tail)
{
char s[] = { 'c', 'd', 'e' };
// if you wanted to make it in the function listrec2<T> *tail;
// listrec2<T> *temp;
listrec2<T> *current2;
listrec2<T> *tail2;
listrec2<T> *value;
listrec2<T> *head2;
head2 = tail = new listrec2<T>; // make a new node
head2->value = s[0];
tail2 = NULL;
head2 = NULL;
for (int i = 1; i < strlen(s); i++)
{
current2 = new listrec2<T>;
current2->value = s[i];
current2->next = NULL;
current2->prev = tail;
tail->next = current2;
tail = current2;
}
listrec2<T> *ptr2;
ptr2 = tail;
cout << "The array in reverse order or backwards: " << endl;
while (ptr2 != NULL)
{
cout << ptr2->value;
ptr2 = ptr2->prev;
}
cout << endl;
}
int main()
{
//missing info here
listrec2<char> *head;
listrec2<char> *tail;
upwardSearch(head);
downwardSearch(tail);
return 0;
}
预期结果为:反转前的数组:cde反转后的数组:EDC。
这是使用 create_list、search_up、search_down 和 destory_list 函数来实现的一种方法。 我尝试使用更具描述性的变量名称。 我不喜欢listrec2
因为它非常令人困惑。 这让我想到了第二个节点,但事实并非如此。 它是一种节点类型。
此外,大写您的类型(例如 Node(是一个很好的兔子。 然后,您可以为对象使用小写版本(例如节点节点;
#include <iostream>
using namespace std;
// of linked list in reverse order
// Structure of a Node
template<class T>
struct Node {
T value; //corresponds to data
Node *next; //points to next Node
Node *prev; //points to previous nod
};
template<typename T>
void CreateList(Node<T> *&head, Node<T> *&tail, T value_array[], int array_size)
{
head = nullptr;
tail = nullptr;
for (int i = 0; i < array_size; i++) {
// Create new node and add node to the end of the list
Node<T> *node = new Node<T>();
node->next = nullptr;
node->prev = tail;
if (head == nullptr) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
node->value = value_array[i];
}
}
template<class T>
void downwardSearch(Node<T> *head)
//traverse from start of linked list to end of linked list
//print out value of each Node along way
{
Node<T> *ptr = head;
cout << "The array in forward order: " << endl;
while (ptr != nullptr) {
cout << ptr->value;
ptr = ptr->next;
}
}
template<class T>
void DestroyList(Node<T> *head)
{
Node<T> *ptr;
while (head != nullptr) {
ptr = head->next;
delete head;
head = ptr;
}
}
template<class T>
void upwardSearch(Node<T> *tail)
{
Node<T> *ptr = tail;
cout << "The array in reverse order or backwards: " << endl;
while (ptr != nullptr) {
cout << ptr->value;
ptr = ptr->prev;
}
cout << endl;
}
int main()
{
char s[] = {'c', 'd', 'e'};
Node<char> *head;
Node<char> *tail;
CreateList<char>(head, tail, s, 3);
upwardSearch(tail);
downwardSearch(head);
DestroyList(head);
head = tail = nullptr;
return 0;
}
相关文章:
- 无法弄清楚如何使用 c++ 中的链表按从 a 到 z 的顺序显示字母
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 使用 while 循环显示顺序变量的值
- 以相反的顺序显示内容
- 编写一个递归功能,该功能采用数组并以相反顺序显示元素,而无需在末尾启动数组的索引
- 如何创建跟踪以显示存在递归的调用顺序
- 显示反向顺序,但不显示非反向顺序 c++
- 从桌子状的顺序中显示从循环的值显示值,以使其更清晰和清洁
- 使用 "For Loop" 按以下顺序显示数字
- 链接列表:在C++中按物理和逻辑顺序显示列表
- 隐藏/显示所有窗口并恢复 Z 顺序
- 二进制搜索树顺序树显示
- 如何显示顺序文件的内容
- 为什么该函数以相反的顺序显示十六进制代码
- 为什么 bool 显示正确的位顺序,而直接输出移位运算结果却不显示?
- 按字母顺序显示名称
- 显示程序中静态C++对象的初始化顺序
- 显示链接列表时颠倒顺序
- 在c++中以随机顺序显示5个图像
- C++ 使用指针以相反的顺序显示句子