马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>[free]
#include<string.h>
#include<stdlib.h>
#include "a.h"
//这是c语言链表3的第一个作业题
struct Num
{
int num;
struct Num* next;
};
void sortnum(struct Num **head,int a)
{
struct Num* c=*head;
struct Num* p=NULL;
struct Num* new;
new=(struct Num*)malloc(sizeof(struct Num));
new->num=a;
while(c!=NULL && c->num<a)
{
p=c;
c=c->next;
}
new->next=c;
if(p==NULL)
{
*head=new;
}
else{
p->next=new;
}
}
struct Num* returnnum(struct Num* head)
{
struct Num* c=head;
struct Num* p=NULL;
while(c)
{
struct Num *next = c->next;
c->next = p;
p = c;
c = next;
}
return p;
}
void printnum(struct Num* head)
{
struct Num* h=head;
while(h!=NULL)
{
printf("%d ",h->num);
h=h->next;
}
putchar('\n');
}
void freenum(struct Num* head)
{
struct Num* h=head;
while(h)
{
h=h->next;
free(h);
}
}
int main(void)
{
struct Num* head=NULL;
int num;
while(1)
{
printf("please input a (int)number : \n");
scanf("%d",&num);
if(num==-1)
{
break;
}
else[/free]
{
sortnum(&head,num);
printnum(head);
}
}
char* c;//不做判断只调用程序正常,做了后下面直接return 0
c=(char*)malloc(sizeof(char));
printf("是否要反转(y/n)\n");
scanf("%c",c);
if(*c=='y')
{
printf("反转:\n");
head=returnnum(head);
printnum(head);
}
free(c);
freenum(head);
return 0;
}
没问题
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *c;
c=(char*)malloc(sizeof(char));
printf("是否要反转(y/n)\n");
scanf("%c", c);
if(*c=='y')
{
printf("反转:\n");
}
return 0;
}
|