如何倒序输出一个数组(违规紫衫)
  • 板块灌水区
  • 楼主zhangjize_qwqjr
  • 当前回复7
  • 已保存回复7
  • 发布时间2022/10/26 16:52
  • 上次更新2023/10/27 05:47:38
查看原帖
如何倒序输出一个数组(违规紫衫)
608570
zhangjize_qwqjr楼主2022/10/26 16:52
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int data;
	node *next;	
};
void insert_head(node *head, int val)
{
	node *tmp = new node;
	tmp -> data = val;
	tmp -> next = NULL;
	tmp -> next = head -> next;
	head -> next = tmp;
	return;
}
void showLink(node *head)
{
	node *p = head;
	while(p -> next != NULL)
	{
		cout << p -> next -> data << " ";
		p = p->next;
	}
	return ;
}
signed main()
{
	node L;
	L.data = 0;
	L.next = NULL;
	int n, x;
	node *head = &L;
	node *tail = &L;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> x;
		insert_head(head,x);
	}
	showLink(head);
	return 0;
}

链表

2022/10/26 16:52
加载中...