程序設(shè)計綜合實踐 1.10

編寫程序選用順序存儲結(jié)構(gòu)和鏈?zhǔn)酱鎯Y(jié)構(gòu)實現(xiàn)抽象數(shù)據(jù)類型棧和隊列,再利用棧和隊列,輸入若干個整數(shù),將輸入后的正整數(shù)和負(fù)整數(shù)分別保存起來,輸入完成后,首先將以輸入相反的次序輸出所有保存的正整數(shù),再以輸入相同次序輸出所有保存的負(fù)整數(shù),。

輸入格式:
若干非0整數(shù)。

輸出格式:
正整數(shù)和負(fù)整數(shù)輸出各占一行,每個數(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é)
代碼長度限制 16 KB
時間限制 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");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容