hi,
Here is my code:-
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next,*prev;
};
typedef struct node *nodeptr;
nodeptr getnode(void);
nodeptr create_list(nodeptr);
void display_list(nodeptr);
nodeptr sort_list(nodeptr);
int main(void)
{
nodeptr head=NULL;
head=create_list(head);
display_list(head);
getch();
head=sort_list(head);
display_list(head);
getch();
return 0;
}
nodeptr getnode(void)
{
nodeptr p;
p=(nodeptr)malloc(sizeof(nodeptr));
printf("\n#:");
scanf("%d",&p->info);
p->next=NULL;
p->prev=NULL;
return p;
}
nodeptr create_list(nodeptr head)
{
int nodes=0;
nodeptr p,curr;
printf("\nEnter how many nodes do you desire in the list:");
scanf("%d",&nodes);
for(;nodes>0;nodes--)
{
p=getnode();
if(!head)
{
p->next=head;
head=p;
}
else
{
for(curr=head;curr->next!=NULL;curr=curr->next)
;
p->prev=curr;
curr->next=p;
}
}
return head;
}
nodeptr sort_list(nodeptr head)
{
nodeptr p,q;
int swap=0;
for(p=head;p!=NULL;p=p->next)
{
for(q=head;q!=NULL;q=q->next)
{
if(p->info < q->info)
{
swap=p->info;
p->info=q->info;
q->info=swap;
}
}
}
return head;
}
void display_list(nodeptr head)
{
nodeptr curr;
for(curr=head;curr!=NULL;curr=curr->next)
printf("\n%d",curr->info);
}
I know it a bit long but since I dont know the problem area , I had to post the entire program.
The above program is a simple doubly linked list which gets sorted and then is printed on the screen.
When I compile the above code in MS VC++ 6.0 and in Turbo C++ 3.0, it runs perfectly, but when i use Dev-Cpp(4.9.9.2) which inherently uses Mingw32(3.4.2), my program accepts only 3 numbers in the list!. This is a very strange bug, if i try to accept more than 3 nodes, a segmentation fault occurs and the program crashes.
Dev-Cpp/Mingw32 users kindly help me cause i dont know wheather my code is at fault or if i am not compiling correctly or what the problem is.
By the way, I am using Windows XP SP1.
Thanks,
Sahil