1
逆序输出(10分)
题目内容:
你的程序会读入一系列的正整数,预先不知道正整数的数量,一旦读到-1,就表示输入结束。然后,按照和输入相反的顺序输出所读到的数字,不包括最后标识结束的-1。
输入格式:
一系列正整数,输入-1表示结束,-1不是输入的数据的一部分。
输出格式:
按照与输入相反的顺序输出所有的整数,每个整数后面跟一个空格以与后面的整数区分,最后的整数后面也有空格。
输入样例:
1 2 3 4 -1
输出样例:
4 3 2 1
时间限制:500ms内存限制:32000kb
#include<stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node *next;
struct node *pre;
}Node;
typedef struct _list{
Node* head;
Node* tail;
}List;
void add(List *pList,int number);
void print(List *pList);
void reprint(List *pList);
int main(int argc, char const *argv[])
{
List list;
list.head = NULL;
list.tail = NULL;
int number;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
reprint(&list);
return 0;
}
void add(List *pList,int number)
{
Node *p =(Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
if(pList->head!=NULL){
p->pre = pList->tail;
pList->tail->next=p;
pList->tail=p;
}else{
pList->head = p;
pList->tail = p;
pList->tail->pre = NULL;
}
}
void print(List *pList){
Node *p;
for(p=pList->head;p;p=p->next){
printf("%d ",p->value);
}
printf("\n");
}
void reprint(List *pList){
Node *p;
for(p=pList->tail;p;p=p->pre){
printf("%d ",p->value);
}
printf("\n");
}
课程总结
#include<stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node *next;
struct node *pre;
}Node;
typedef struct _list{
Node* head;
Node* tail;
}List;
void add(List *pList,int number);
void add_head(List *pList,int number);
void print(List *pList);
void reprint(List *pList);
void find(List *pList,int number);
void mydelete(List *pList,int number);
void myfree(List *pList);
int main(int argc, char const *argv[])
{
List list;
list.head = NULL;
list.tail = NULL;
int number;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
reprint(&list);
// scanf("%d",&number);
// find(&list,number);
// mydelete(&list,number);
// myfree(&list);
// add_head(&list,number);
// print(&list);
return 0;
}
void add(List *pList,int number)
{
Node *p =(Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
if(pList->head!=NULL){
p->pre = pList->tail;
pList->tail->next=p;
pList->tail=p;
}else{
pList->head = p;
pList->tail = p;
pList->tail->pre = NULL;
}
}
void add_head(List *pList,int number)
{
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = pList->head;
pList->head = p;
}
void print(List *pList){
Node *p;
for(p=pList->head;p;p=p->next){
printf("%d ",p->value);
}
printf("\n");
}
void reprint(List *pList){
Node *p;
for(p=pList->tail;p;p=p->pre){
printf("%d ",p->value);
}
printf("\n");
}
void find(List *pList,int number){
Node* p;
int isFound =0;
int ret = 0;
for( p= pList->head;p;p=p->next)
{
ret++;
if(p->value==number)
{
printf("找到了,在第%d个\n",ret);
isFound = 1;
break;
}
}
if(!isFound){printf("没找到\n");}
}
void mydelete(List *pList,int number){
Node* p;
Node* q;
int isFound =0;
for(q=NULL,p= pList->head;p;q=p,p=p->next)
{
if(p->value==number){
if(q){
q->next = p->next;
}else {
pList->head = p->next;
}
free(p);
printf("删除成功\n");
print(pList);
isFound = 1;
break;
}
}
if(!isFound){printf("没找到\n");}
}
void myfree(List *pList){
Node*p;
Node*q;
for(p=pList->head;p;p=q)
q=p->next;
free(p);
}