Program on Order linked list

Error in Order linked list

Page 1 of 1

3 Replies - 6546 Views - Last Post: 07 November 2008 - 10:19 AM Rate Topic: -----

#1 nalwaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 08

Program on Order linked list

Post icon  Posted 05 November 2008 - 09:45 AM

// here in below program i am not getting the desired output please help me in
// making below link list as a ordered linked list
// NOTE:- i have used TURBO C/C++ compiler
// please replay soon with error explanation thank you in advance
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct link
{
  int num;
  struct link *next;
};
typedef struct link node;
node *start = NULL;
node *put,*temp,*disp;


void create()
{

 int n;

 put=(node*)malloc(sizeof(node));

 cout<<"\n Enter no.";
 cin>>n;
 put->num=n;
 put->next=NULL;

}

void insert(node *put)
{

if(start==NULL)
	{
	start=put;
	temp=start;
	disp=start;
	}
 while(disp!=NULL)
	  {
	if((disp->num) > (put->num))
	   {
	  temp->next=put;
	  temp=put;
	   }
	  }

   {
	temp->next=put;
	temp=put;
	}
}

void display(node *disp)
{

  cout<<"\n Displaying List:";
  while(disp!=NULL)
	  {
	cout<<disp->num<<"->";
	disp=disp->next;
	  }
}

void main()
{

clrscr();
 char ch;

prg:

cout<<"\n Do you want to create (Y/N):";
cin>>ch;

 if( ch=='y' || ch=='Y')
  {
   create();
   insert(put);
   display(disp);
   goto prg;
  }
 else
  {
   exit(0);
  }
getch();

}


Is This A Good Question/Topic? 0
  • +

Replies To: Program on Order linked list

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Program on Order linked list

Posted 05 November 2008 - 11:13 PM

I think this is what you are trying to do...

void insert(node *put)
{
    disp=start;
    if(start==NULL) // if list has no nodes.
    {
        start=put;
        temp=start;
        disp=start;
        return;
    }
    temp = disp->next;
    if((disp->num > put->num)) // compare first node and insert node at start if it is smallest value.
    {
            start = put;
            put->next= disp;
            disp = start;
    }
    else if(temp==NULL && (disp->num <= put->num)) // if new node has bigger value and start node is the only node in list
    {
         start->next = put;
    }
    else
    {
        while(temp!=NULL)
        {
            if((temp->num) <= (put->num)) // if current node is smaller, we proceed to compare with next node in list.
            {
                temp = temp->next;
            }
            else // current node is bigger than new node, so we will add it before current node.
            {
                disp->next = put;
                put->next = temp;
                disp = start;
                return;
            }
        }
    }
}



This code is written on the fly and hence is not compiled. Please check for small mistakes.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 nalwaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 08

Re: Program on Order linked list

Posted 06 November 2008 - 07:08 AM

Hi,
Thanx for reply but my problem remains the same, still I could not get the ORDER LINKED list . I have pasted my whole program below

View PostAmitTheInfinity, on 5 Nov, 2008 - 10:13 PM, said:

I think this is what you are trying to do...

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct link
{
  int num;
  struct link *next;
};
typedef struct link node;
node *start = NULL;
node *put,*temp,*disp;


void create()
{

 int n;

 put=(node*)malloc(sizeof(node));

 cout<<"\n Enter no.";
 cin>>n;
 put->num=n;
 put->next=NULL;

}
void insert(node *put)  
{  
    disp=start;  
    if(start==NULL) // if list has no nodes.  
    {  
        start=put;  
        temp=start;  
        disp=start;  
        return;  
    }  
    temp = disp->next;  
    if((disp->num > put->num)) // compare first node and insert node at start if it is smallest value.  
    {  
            start = put;  
            put->next= disp;  
            disp = start;  
    }  
    else if(temp==NULL && (disp->num <= put->num)) // if new node has bigger value and start node is the only node in list  
    {  
         start->next = put;  
    }  
    else  
    {  
        while(temp!=NULL)  
        {  
            if((temp->num) <= (put->num)) // if current node is smaller, we proceed to compare with next node in list.  
            {  
                temp = temp->next;  
            }  
            else // current node is bigger than new node, so we will add it before current node.  
            {  
                disp->next = put;  
                put->next = temp;  
                disp = start;  
                return;  
            }  
        }  
    }  
}  

void display(node *disp)
{

  cout<<"\n Displaying List:";
  while(disp!=NULL)
      {
	cout<<disp->num<<"->";
	disp=disp->next;
      }
}

void main()
{

clrscr();
 char ch;

prg:

cout<<"\n Do you want to create (Y/N):";
cin>>ch;

 if( ch=='y' || ch=='Y')
  {
   create();
   insert(put);
   display(disp);
   goto prg;
  }
 else
  {
   exit(0);
  }
getch();

}




This code is written on the fly and hence is not compiled. Please check for small mistakes.

I hope this will help you. :)

This post has been edited by nalwaya: 06 November 2008 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 nalwaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 08

Re: Program on Order linked list

Posted 07 November 2008 - 10:19 AM

Please help me in solving the problem still i could not find the right output

View PostAmitTheInfinity, on 5 Nov, 2008 - 10:13 PM, said:

I think this is what you are trying to do...

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct link
{
  int num;
  struct link *next;
};
typedef struct link node;
node *start = NULL;
node *put,*temp,*disp;


void create()
{

 int n;

 put=(node*)malloc(sizeof(node));

 cout<<"\n Enter no.";
 cin>>n;
 put->num=n;
 put->next=NULL;

}
void insert(node *put)  
{  
    disp=start;  
    if(start==NULL) // if list has no nodes.  
    {  
        start=put;  
        temp=start;  
        disp=start;  
        return;  
    }  
    temp = disp->next;  
    if((disp->num > put->num)) // compare first node and insert node at start if it is smallest value.  
    {  
            start = put;  
            put->next= disp;  
            disp = start;  
    }  
    else if(temp==NULL && (disp->num <= put->num)) // if new node has bigger value and start node is the only node in list  
    {  
         start->next = put;  
    }  
    else  
    {  
        while(temp!=NULL)  
        {  
            if((temp->num) <= (put->num)) // if current node is smaller, we proceed to compare with next node in list.  
            {  
                temp = temp->next;  
            }  
            else // current node is bigger than new node, so we will add it before current node.  
            {  
                disp->next = put;  
                put->next = temp;  
                disp = start;  
                return;  
            }  
        }  
    }  
}  

void display(node *disp)
{

  cout<<"\n Displaying List:";
  while(disp!=NULL)
      {
	cout<<disp->num<<"->";
	disp=disp->next;
      }
}

void main()
{

clrscr();
 char ch;

prg:

cout<<"\n Do you want to create (Y/N):";
cin>>ch;

 if( ch=='y' || ch=='Y')
  {
   create();
   insert(put);
   display(disp);
   goto prg;
  }
 else
  {
   exit(0);
  }
getch();

}




This code is written on the fly and hence is not compiled. Please check for small mistakes.

I hope this will help you. :)

[/quote]

This post has been edited by nalwaya: 07 November 2008 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1