LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-13-2006, 03:56 PM   #1
showri
LQ Newbie
 
Registered: Sep 2006
Posts: 1

Rep: Reputation: 0
Post sorting in linking lists


hi friends please help me with this program the output is a merging of two linked lists .like if we give the two linked lists input as cat,dog the output should be displayed as acdgot.something like sorting order.my program is written in c++ and i want the same program to be written in c also so what changes should i make .i am pasting the program. the output i am getting as cat dog this program doesnot contain any errors and i have executed it. please do the sorting using bubble sort

#include <iostream>
using namespace std;
#include <string>
#include <sched.h>
#include<stdio.h>
#include<ncurses.h>
struct node
{
char name[25];
node *next;
};

class orderedlist
{
public:
node *start;

orderedlist()
{
start=NULL;
}

int isempty()
{
if(start==NULL)
return 1;
return 0;
}

void insert();
void display();
};
void orderedlist::insert()
{
node *temp;node *exc;
temp=new node();
cout<<"enter the name :";
cin>>temp->name;
temp->next=NULL;
if(isempty()==1)
start=temp;
else
{
node *t;
t=start;
if(t->next!=NULL)
{
t=t->next;

t->next=temp;




}

}
}


void orderedlist::display()
{
cout<<"\n";
if(isempty()==1)
{
cout<<"List is empty";
return;
}
node *t=start;
while(t!=NULL)
{
cout<<"\t"<<t->name;
t=t->next;
}
}


int main()
{

orderedlist l1,l2;

//reading first list
int opt;
do
{
cout<<"\n\n\n\n\n U R ENTERING ELEMENTS INTO FIRST LIST";
cout<<"\n1.insert\n2.display\n3.return\n\nenter ur choice:";
cin>>opt;
switch(opt)
{
case 1: l1.insert();
break;

case 2: l1.display();
return(0);
break;

case 3: cout<<"coming out of the loop";
break;
default:cout<<"U have entered an invalid option";
}
}while(opt!=3);

//reading the second list
do
{

cout<<"\n\n\n\n\n U R ENTERING ELEMENTS INTO SECOND LIST";
cout<<"\n1.insert\n2.display\n3.return\n\nenter ur choice:";
cin>>opt;
switch(opt)
{
case 1: l2.insert();
break;



case 2: l2.display();
return(0);
break;

case 3: cout<<"coming out of the loop";
break;
default:cout<<"U have entered an invalid option";
}
}while(opt!=3);

//merging of the two lists
orderedlist l3;
node *one,*two,*n;
one=l1.start;
two=l2.start;
while(one!=NULL&&two!=NULL)
{
node *temp;
temp=new node();
temp->next=NULL;
if(l3.isempty()==1)
{
l3.start=temp;
n=temp;
}
else
{
n->next=temp;
n=n->next;
}
if(strcmp(one->name,two->name)<0)
{
strcpy(temp->name,one->name);
one=one->next;
}
else
{
strcpy(temp->name,two->name);
two=two->next;
}
}
while(one!=NULL)
{
node *temp;
temp=new node();
temp->next=NULL;
strcpy(temp->name,one->name);
one=one->next;
if(l3.isempty()==1)
{
l3.start=temp;
n=temp;
}
else
{
n->next=temp;
n=n->next;
}

}
while(two!=NULL)
{
node *temp;
temp=new node();
temp->next=NULL;
strcpy(temp->name,two->name);
two=two->next;
if(l3.isempty()==1)
{
l3.start=temp;
n=temp;
}
else
{
n->next=temp;
n=n->next;
}

}

l1.display();
l2.display();
l3.display();

return(0);
}

Last edited by showri; 09-13-2006 at 03:58 PM.
 
Old 09-14-2006, 05:17 AM   #2
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
Moved: This thread is more suitable in the Programming Forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-14-2006, 06:29 AM   #3
shyamdey
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
There are so many method .this is one ...
first u traverse the pointer of first linked list up to NULL and then linked to second list. after that u can sort the entire list using any sorting menthod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 04:49 AM
sorting a map (C++) Thinking Programming 6 09-13-2005 05:52 PM
Which sorting algorithm? nodger Programming 6 01-28-2005 07:13 PM
sorting and stuff arnulfo Programming 13 01-01-2005 05:37 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 10:10 AM


All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration