Problem B: 逆置线性表(线性表)

Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 940  

Solved: 545

[

Submit][

Status][

Web Board]


Description


(线性表)请写一个算法将顺序存储结构的线性表(a1...an)逆置为(an...a1)。


Input


输入长度n:5

输入数据:1 2 3 4 5


Output


5 4 3 2 1


Sample Input

5
7 8 9 10 11

Sample Output

11 10 9 8 7

HINT


逆置是数据结构中的题,类似数组中的逆置,即头尾互换。这里大家可以先用逆序,即输出时倒序输出,以后再用逆置做。

[ Submit][

Status][

Web Board]


한국어 中文 فارسی English ไทย
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Help Maunal

代码

#include <stdio.h>
#include <malloc.h>
struct Num
{
	int n;
	struct Num *next;
}num;
struct Num *createlist(struct Num *head,int n);
void print(struct Num *head);
int main()
{
	struct Num *head=NULL;
	int n ;
	scanf("%d",&n);
	head=createlist(head,n);       //建立
	print(head);             //输出
	return 0;
}
struct Num *createlist(struct Num *head ,int n)                //头插法建立链表
{
	struct Num *p;
	int i ;
	p=head=(struct Num*)malloc(sizeof(struct Num));
	head=NULL;                                     
	p=(struct Num*)malloc(sizeof(struct Num));            //p建立新结点
	                      //将新结点插到开头的位置
	  for(i= 0; i<n ;i++)
	  {
			scanf("%d",&p->n);			                      
		if(head==NULL)
     	{
     		 head = p ;
     		 head->next =NULL;
		 }
		
		 else
		 {
		 	p->next =head;
		}   
        head= p;
		p=(struct Num*)malloc(sizeof(struct Num));         //p每次建立新结点
	}
	return head;
}

void print(struct Num *head)
{
	struct Num *current=head;
	while(current!=NULL)
	{
		printf("%d ",current->n);
		current=current->next;
	}
}