Hallo World的奇葩写法

Eric 阿蕾奇诺 2023-07-19 10:35:48 2023-07-24 14:22:33 6
#include <bits/stdc++.h>

using namespace std;

queue<char> h;

struct Node {
	char data; 

	Node* next;
};

Node* createLinkedList(char arr[], int length) {

	Node* head = nullptr;

	Node* tail = nullptr;
	
	for (int i = 0; i < length; i++) {
		Node* newNode = new Node;

		newNode->data = arr[i];

		newNode->next = nullptr;

		if (head == nullptr) {

			head = newNode;

			tail = newNode;

		} else {

			tail->next = newNode;

			tail = newNode;
		}
	}
	
	return head;
}

void printLinkedList(Node* head) {

	Node* current = head;
	
	while (current != nullptr) {

		std::cout << current->data;

			current = current->next;

	}
}

int main() {
	
        char arr[] = "Hello World!";

	int length = sizeof(arr) / sizeof(arr[0]);

	Node* head = createLinkedList(arr, length);

	printLinkedList(head);

	cout << "     ----->>链表"<<endl;

	h.push('H');

	h.push('e');

	h.push('l');

	h.push('l');

	h.push('o');

	h.push(' ');

	h.push('w');

	h.push('o');

	h.push('r');

	h.push('l');

	h.push('d');

	h.push('!');

	for(int i = 1;i <= 12;i++)
	{

		char t = h.front();

		h.pop();

		cout << t;

	}

	cout << "      ----->>queue(队列)"<<endl;

	return 0;

}

供参考的两种写法:链表和队列。 还不赶快去试试吗!

{{ vote && vote.total.up }}

共 1 条回复

CPP 刷题王

写错了……