473,373 Members | 1,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

Need help in sorting a singly linked list...

4
// Linked Lists in classes(excluding structures) without using tail pointer

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

void Swap(int num1, int num2)
{
int a = num1;
num2 = num1;
num2 = a;
}

class node
{
private:
int data;
node* next;
node* headptr;
public:
node()
{headptr = NULL;}
void insert_at_head(int d)
void insert_at_tail(int d);

void display_all();

void sort_ascending_order(); // complicated
void sort_descending_order(); // complicated



};

///////////////////////////////////////////////////////////////////////////////

void node::insert_at_head(int d)
{
node* ptr = new node;
ptr->data = d;

ptr->next = headptr;
headptr = ptr;

}

void node::display_all()
{
node* tempptr = headptr;

if(headptr == NULL)
{cout<<"The list is empty"<<endl;}

while(tempptr->next != NULL)
{
cout<<tempptr->data<<" , ";
tempptr = tempptr->next;
}

if(tempptr->next == NULL)
{cout<<tempptr->data;}
}

void node::insert_at_tail(int d)
{
node* temptr = new node;
temptr->data = d;
temptr->next = NULL;

node* ptrCurrent = headptr;

while(ptrCurrent->next != NULL)
{
ptrCurrent = ptrCurrent->next;

}
ptrCurrent->next = temptr;
}





void node::sort_ascending_order()
{
node* realptr = headptr;
node* ptrCurrent = headptr;

while(ptrCurrent->next != NULL)
{
if(ptrCurrent->data > ptrCurrent->next->data)
{
realptr->data = ptrCurrent->data;
cout<<realptr->data<<endl;

Swap(ptrCurrent->data,ptrCurrent->next->data);
}
ptrCurrent = ptrCurrent->next;
realptr = realptr->next;


}

}




void main()
{
node n1;
n1.insert_at_head(1);
n1.insert_at_head(2);
n1.insert_at_head(3);
n1.insert_at_tail();
n1.insert_at_tail();
n1.display_all();
cout<<endl<<endl;

cout<<"Testing Sorting"<<endl<<endl;
n1.sort_ascending_order();

cout<<endl<<endl;
n1.display_all();

}

//void node::sort_ascending_order() this is not working

Please help me out using singly linked list and do keep the code less complicated. Thankx
Aug 5 '06 #1
3 7822
Banfa
9,065 Expert Mod 8TB
My computer just crashed at the end of me typing a long post, 'fraid I can be bother to type it all again but here are some pointers

Your Swap function doesn't work (go on try it with 2 ints)

You need to look up about static data members and static function members of classes

You should always check your pointers, where you have already checked them you need to act on it not just continue as if everything was OK insert_at_head and insert_at_tail among other places

In your print function your while loop has the wrong control expression and wont print the last list member, the final if statement is useless as the condition MUST be false for the while loop to exit.

You can not perform an array sort in a single pass of the array, it is not possible. The realptr variable is confusing and useless, it always points to the same place as ptrCurrent.

1 sort method is called a bubble sort, you scan down your list comparing adjacent pairs of entries. If the need swaping you swap them, when you get to the end of the list if you swapped any entries on this scan of the list then you scan the list again.

1 possible method is
Aug 5 '06 #2
malik
4
Thankx Banfa, God Bless u.
Aug 6 '06 #3
void LinkList::sort()
{
Node *q, *r;
for( q = p ; q->link != NULL ; q = q->link )
{
for( r = q->link ; r->link != NULL ; r = r->link )
{
if (r->data<q->data)
{
int temp;
temp = r->data;
r->data = q->data;
q->data = temp;
}
}
}
}
Dec 11 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
7
by: Shwetabh | last post by:
Hi, can some one tell me: -> how to remove a loop from a singly linked list with a loop. -> how to count the number of nodes in a looped singly link list. Thanks
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
3
by: jou00jou | last post by:
Hello, I am trying to sort a singly linked list for the following typedef typedef struct message { int messageId; char * messageText; struct message * next; } message; I have successfully...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.