// 1181_建立鏈表.cpp : 定義控制檯應用程序的入口點。
//題目1181:遍歷鏈表
//時間限制:1 秒內存限制:32 兆特殊判題:否提交:3381解決:1425
//題目描述:
//建立一個升序鏈表並遍歷輸出。
//輸入:
//輸入的每個案例中第一行包括1個整數:n(1<=n<=1000),接下來的一行包括n個整數。
//輸出:
//可能有多組測試數據,對於每組數據,
//將n個整數建立升序鏈表,之後遍歷鏈表並輸出。
//樣例輸入:
//4
//3 5 7 9
//樣例輸出:
//3 5 7 9
//來源:
//2000年華中科技大學計算機研究生機試真題
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct link{
int num;
struct link * next;
}link;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
link *pre,*current;
link *head = (link *)malloc(sizeof(link));
link *node = (link *)malloc(sizeof(link));
scanf("%d",&(node->num));
node ->next =NULL;
head ->next = node;
for(int i= 0;i<n-1;i++)
{
pre = head;
current = head ->next;
link *node = (link *)malloc(sizeof(link));
scanf("%d",&(node->num));
node ->next = NULL;
while(node->num > current->num)
{
pre = current;
current = current ->next;
if(!current)
break;
}
node->next = current;
pre ->next = node;
}
link *q = head->next;
while(q->next!=NULL)
{
printf("%d ",q->num);
q=q->next;
}
printf("%d\n",q->num);
}
return 0;
}
1181_建立鏈表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.