4 Replies - 741 Views - Last Post: 04 December 2012 - 11:44 AM Rate Topic: -----

#1 sagar1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-December 12

i got runtime error while executing strand sort

Posted 04 December 2012 - 06:01 AM

Strand sort is a sorting algorithm. It works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with a sorted sublist.
in output it doesn't show sorted list



#include<stdio.h>
#include<stdlib.h>
struct node
{
	int info;
	struct node *next;
};
void strand_sort(struct node **,struct node **,struct node **);
void insert(struct node **);
void merge(struct node **,struct node **);
void display(struct node **);
void main()
{
	struct node *first=NULL;
	struct node *strand=NULL;
	struct node *sort=NULL;
	struct node *te;
	printf("enter numbers\n");
	insert(&first);
	te=first;
	while(te!=NULL)
{
    printf("\n %d",te->info);
    te=te->next;
}
	strand_sort(&first,&strand,&sort);
	display(&sort);
}
void insert(struct node **f)
{
	struct node *p;
	struct node *temp;
	int n,k;
	do
	{
		p=*f;
		temp=(struct node *)malloc(sizeof(struct node));
		printf("enter elem\n");
		scanf("%d", &n);
		temp->info=n;
		temp->next=NULL;
		if(*f==NULL)
		*f=temp;
		else{
		while(p->next!=NULL)
		{
		p=p->next;
		}
		p->next=temp;
		}
		printf("enter 1 to cont\n");
		scanf("%d", &k);
		}
		while(k==1);

}
void strand_sort(struct node **f,struct node **g,struct node **h)
{
	struct node *p,*temp,*temp2,*temp1,*q,*r,*s;
	p=*f;
	q=*g;
	r=*h;
	s=NULL;

	if((*g)==NULL)
	{
   temp=(struct node *)malloc(sizeof(struct node));
	temp->next=NULL;
	temp->info=p->info;
		(*g)=temp;
		q=temp;
		(*f)=(*f)->next;
         p=*f;
         s=p;
printf("\n the 1st element in strand is %d",(*g)->info);
printf("\nfirst points to %d", (*f)->info);
printf("\ns point to %d",s->info);
	}
	else
	{
		while(p->next!=NULL)
		{
			if(p->info>q->info)
			{
			    if((*f)==p)
			    {

			        temp1=(struct node *)malloc (sizeof(struct node));
			        temp1->info=p->info;
			        temp1->next=NULL;
			        q->next=temp1;
			        q=temp1;
			        (*f)=(*f)->next;
			        p=*f;
			        s=p;
			        printf("\nfirst points to %d", (*f)->info);
			        printf("s point to %d",s->info);
			    }
			    else
                {
				temp2=(struct node *)malloc(sizeof(struct node));
				temp2->next=NULL;
				temp2->info=p->info;
				q->next=temp2;
				q=temp2;
				s->next=p->next;
				p=s;

				printf("\nthe first non first element intnserted %d",q->info);
				}

			}
			else
			{
            s=p;
			p=p->next;
			}
		}
	}

	printf("elemnet eneterd in d the first strand are");
r=*g;
while(r->next!=NULL)
{
    printf("%d",r->info);
    r=r->next;
}
	merge(g,h);
	}



void merge(struct node **q,struct node **r)
{
	struct node *a,*b,*temp4,*temp5,*temp6,*temp7,*p;
	a=*q;
	if(*r==NULL)
	{	temp4=(struct node *)malloc(sizeof(struct node));
		temp4->info=a->info;
		temp4->next=NULL;
		*r=temp4;
		p=temp4;
		a=a->next;
		while(a->next!=NULL)
		{
			temp5=(struct node *)malloc(sizeof(struct node));
			temp5->info=a->info;
			temp5->next=NULL;
			p->next=temp5;
			p=temp5;
			a=a->next;
		}

	}
	else
	{
		a=*q;
		b=*r;
		while(a->info<b->info&&a!=NULL)
		{
			temp6=(struct node *)malloc(sizeof(struct node));
			temp6->next=NULL;
			temp6->info=a->info;
			temp6->next=b;
			*r=temp6;
			a=a->next;
		}
		if(a==NULL)
		{
		return ;
		}
		else if(a->info>b->info)
		{
			temp7=(struct node *)malloc(sizeof(struct node));
			temp7->info=a->info;
			temp7->next=b->next;
			b=temp7;
			a=a->next;

		}
		else{
		b=b->next;
		}
	}
	*q==NULL;

}
void display(struct node **z)
{
	struct node *m;
	m=*z;
	while(m!=NULL)
	{
		printf("%d\n", m->info);
		m=m->next;
	}
}


This post has been edited by baavgai: 04 December 2012 - 08:10 AM
Reason for edit:: tag fixed


Is This A Good Question/Topic? 0
  • +

Replies To: i got runtime error while executing strand sort

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: i got runtime error while executing strand sort

Posted 04 December 2012 - 06:08 AM

Would you please edit your post and fix the code tags. Select the code and click the code button in the editor...

Also, post the exact error messages you are getting. It will make it easy to help you...
Was This Post Helpful? 1
  • +
  • -

#3 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: i got runtime error while executing strand sort

Posted 04 December 2012 - 07:09 AM

Well! I asked you to fix these code tags. Instead, you started a new thread, also without code tags...

Anyhow, I asked for error messages because I thought you are having some error message. But its a Runtime Error...

A further help can be provided only if you fix the code tags...

One thing to point out, you are using pointers to pointers in your code and you are dereferencing them incorrectly... With normal pointers, we dereference a pointer like this: *ptr which will give the value at the location to which the pointer is pointing... I hope you already understand that...

so, with pointers to pointers we need to use double dereference i.e. use the asterisk twice to get the value the pointer is pointing to...

Consider the example:
struct myStruct {
    int x;
};

void modify(myStruct **ptr) {
    (**ptr).x = 20;

    // or we can do it using this way
    (*ptr)->x = 20;
}

int main()
{
    myStruct *ms;
    ms->x = 10;
    std::cout << ms->x;    // prints 10

    modify(&ms);

    std::cout << ms->x;    // prints 20

    return 0;
}



Besides that, I think you are using the obsolete turbo-C, which allows you to use void main(). Remember its int main() and not void main()...

Please use a compiler that is compliant with the standards, like gcc... Or you can get Code::Blocks IDE which comes with MinGW... Remember to get the download which contains MinGW...
Was This Post Helpful? 0
  • +
  • -

#4 kaa  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 29
  • Joined: 15-April 11

Re: i got runtime error while executing strand sort

Posted 04 December 2012 - 11:22 AM

I agree with AKMafia001, you should join the modern world and stop using void main(). void main does not comply with C standards. Use it only in special situations where it is required, such as certain embedded systems that don't allow main to return a value. Otherwise always use int main.

When you have a runtime error, the first thing to do is find the line of code that triggers the error. Either learn to use a debugger (there are tutorials for that) which will help you pinpoint the error, or use printf statements to see how far the program gets before it crashes.

When I run your program it crashes after printing "first points to [the second element entered]" which is on line 76. But looking at the next line, it should simply print the same element again because you have set s = p. So apparently it just crashed before the print buffer was cleared (output to the screen doesn't always happen immediately). So you can clear the stdout buffer by addding
fflush(stdout);

right after these printf statements.

Now you should see that it crashes after printing "s point to [the second element entered]". But there's nothing apparently wrong with the next line -- it's a simple assignment from one pointer to another. So where does your program go next? Trace the logic of your program. If you keep following this procedure, adding these two lines
printf("got here [add a line number to help identify the location]\n");
fflush(stdout);


at various locations in the program you will eventually be able to pinpoint the exact line that triggers the error. It will generally be the next line after the last possible place where you can get a printf statement to run.

Eventually you will find the line of code that tries to dereference a null pointer (i.e., tries to access some value pointed to by NULL) which causes it to crash. But I don't want to deprive you of the satisfaction of finding that. Good luck.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: i got runtime error while executing strand sort

Posted 04 December 2012 - 11:44 AM

You repeatedly call malloc in your sort. You never call free...

Also, you don't need malloc in the sort. The point of a strand sort being useful with a linked list is that there's a lot of shuffling from the middle of things going on. You should have a pass through your current list that produces a strand and the stuff that's left. Not a single new node should be created.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1