#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 200
typedef struct Node {
long int data;
struct Node* next;
}LNode,*LinkList;
LinkList CreatList(LinkList L,char* str);
LinkList Bubble_Sort(LinkList SL);
int main(int argc, char** argv)
{
char* str=NULL;
LinkList head=NULL;
#if 0
head = (LinkList)malloc(sizeof(LNode));
if (NULL == head)
{
printf("malloc size error!\n");
return 1;
}
#endif
str = (char*)malloc(MAX_SIZE*sizeof(char));
if (NULL == str)
{
printf("malloc size error!\n");
return 1;
}
printf("Please input number(q/Q is quit):\n");
head = CreatList(head,str);
printf("%d\n",head->data);
// Bubble_Sort(head);
printf("The result is:\n");
#if 0
for(;head!=NULL;head=head->next)
printf("%d ",head->data);
#endif
while(head != NULL)
{
printf("%d ",head->data);
head=head->next;
}
free(head);
head = NULL;
return 0;
}
LinkList CreatList(LinkList L,char* str)
{
LinkList p,q;
// L -> next = NULL;
p=q=(LinkList)malloc(sizeof(LNode));
if ( p== NULL || q ==NULL )
{
free(p);
free(q);
return NULL;
}
// p=L;
while(1)
{
scanf("%s",str);
if(*str == 'q' || *str == 'Q')
{
break;
}
q -> data = strtol(str,0,0);
if(NULL == L)
{
L = q;
}
else
{
p -> next = q;
}
p = q;
q = (LinkList)malloc(sizeof(LNode));
q->next = NULL;
}
// free(p);
// free(q);
// p = q = NULL;
for(;L!=NULL;L=L->next)
printf("%d ",L->data);
return L;
}
LinkList Bubble_Sort(LinkList SL)
{
LinkList p,q;
long temp;
for(p=SL;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->data > q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
}
}
return SL;
}