程序设计综合实践 1.10

编写程序选用顺序存储结构和链式存储结构实现抽象数据类型栈和队列,再利用栈和队列,输入若干个整数,将输入后的正整数和负整数分别保存起来,输入完成后,首先将以输入相反的次序输出所有保存的正整数,再以输入相同次序输出所有保存的负整数,。

输入格式:
若干非0整数。

输出格式:
正整数和负整数输出各占一行,每个数占5位。

输入样例:100 2 3 -2 -8 -6 -9 -10 50 2 -1
输出样例:2 50 3 2 100
-2 -8 -6 -9 -10 -1
作者李卫明
单位杭州电子科技大学
代码长度限制16 KB
时间限制400 ms
内存限制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");
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值