編寫程序選用順序存儲(chǔ)結(jié)構(gòu)和鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)實(shí)現(xiàn)抽象數(shù)據(jù)類型棧和隊(duì)列赘艳,再利用棧和隊(duì)列,輸入若干個(gè)整數(shù)克握,將輸入后的正整數(shù)和負(fù)整數(shù)分別保存起來蕾管,輸入完成后,首先將以輸入相反的次序輸出所有保存的正整數(shù)菩暗,再以輸入相同次序輸出所有保存的負(fù)整數(shù)掰曾,。
輸入格式:
若干非0整數(shù)停团。
輸出格式:
正整數(shù)和負(fù)整數(shù)輸出各占一行旷坦,每個(gè)數(shù)占5位掏熬。
輸入樣例: | 100 2 3 -2 -8 -6 -9 -10 50 2 -1 |
---|---|
輸出樣例: | 2 50 3 2 100 -2 -8 -6 -9 -10 -1 |
作者 | 李衛(wèi)明 |
---|---|
單位 | 杭州電子科技大學(xué) |
代碼長(zhǎng)度限制 | 16 KB |
時(shí)間限制 | 400 ms |
內(nèi)存限制 | 64 MB |
C語言版本:
#include<stdio.h>
#include <string.h>
typedef struct Node{
int data;
struct Node *next;
} Node;
Node* input_LinkedList(){
char str[1024];
char *delim = " ";
scanf("%[^\n]", str);
Node* head = (Node*)malloc(sizeof(Node));
Node * end = head;
Node * node;
int count=0;
char *p = strtok(str, delim);
head->data = atoi(p);
count++;
while((p = strtok(NULL, delim))){
node = (Node*)malloc(sizeof(Node));
node->data = atoi(p);
head->next = node;
head = head->next;
count++;
}
return end;
}
int main(){
Node* head = input_LinkedList();
int positive[1024];
int pos_count=0;
int negative[1024];
int neg_count=0;
while(head!=NULL){
if(head->data>0){
positive[pos_count++]=head->data;
}else{
negative[neg_count++]=head->data;
}
head = head->next;
}
for(int i=pos_count-1;i>=0;i--){
printf("%5d",positive[i]);
}
printf("\n");
for(int i=0;i<neg_count;i++){
printf("%5d",negative[i]);
}
printf("\n");
}