#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct list
{
int data;
struct list *next;
}list;
int main()
{
int data;
int count;
list *head;
printf("b1");
list* addList1(list*,int,int*);
void printlist(list*);
count = 0;
head = NULL;
printlist(head);
printf("Enter Node data\n");
scanf("%d",&data);
printf("Node data is %d\n",data); /*<--- this works and prints to screen*/
while(data >= 0)
{
printf("yy");/*<---this doesn't print to screen so seg fault somewhere between these two?*/
head = addList1(head, data,&count);
printlist(head);
scanf("%d",&data);
}
return(0);
}
list* addList1(list *head,int data,int* count)
{
list *current,*front,*rear;
int i,j;
printf("b2");
current =malloc(sizeof(list*));
printf("b3");
assert(current!=NULL);
printf("b4");
if(head==NULL)
{
printf("a");
head=current;
printf("b");
current->data=data;
printf("c");
current->next=NULL;
printf("d");
front = current;
printf("e");
*count++;
printf("f");
}
else
{
current->data=data;
current->next=NULL;
front->next=current;
front=current;
*count++;
}
/*sorts after data is added*/
front = NULL;
rear = NULL;
current = head;
front = head;
rear = head->next;
for(i=0;i < *count - 1; i++)
{
for(j=0;j < (*count - i - 1); j++)
{
if(current->data > rear->data)
{
current->next = rear->next;
rear->next = current;
if(current == head)
{
head = rear;
front = rear;
}
else
{
front->next = rear;
front = rear;
}
if(rear != NULL)
{
rear = current->next;
}
}
else
{
front = current;
current = rear;
rear = current->next;
}
}
current = head;
front = head;
rear = current->next;
}
return(head);
}
void printlist(list *head)
{
list *current;
printf("list:\n");
for(current=head;current!=NULL;current = current->next)
{
printf("%d ",current->data);
}
printf("\n\n");
}
A lot of code Yeah I know. For some reason it seems to be seg faulting after printing what the user input and before the while loop. I don't get it as it was working fine before. Any insight to what may be the problem feel free to share and I would appreciate a lot.
This post has been edited by McSick: 22 October 2010 - 04:06 PM

New Topic/Question
Reply




MultiQuote






|