6 Replies - 7945 Views - Last Post: 27 May 2010 - 02:58 AM Rate Topic: -----

#1 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Fix My Linked List Bubble Sort

Posted 26 May 2010 - 09:40 AM

can any one make run my code and tell me why when its sorted by bubble the last no. become 0's??? why after the sorting its not still the same liked list??? help

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

typedef int List_entry;
typedef int Node_entry;



class Node{
public:
Node_entry entry;
Node *next;
Node();
Node(Node_entry item,Node *add_on=NULL);
};

Node::Node(){entry=NULL;}
Node::Node(Node_entry item,Node *add_on)
{
entry=item;
next=add_on;
}


class List{
protected:
Node *head;
public:
List(){head=NULL;}
void insert(int poss,List_entry item);
void retrive(int poss,List_entry &item);
void remove(int poss);
void repleas(int poss,List_entry item);
int size();
};





int List::size()
{
int c=0;
Node *p=head;
while(p!=NULL)
{
c++;
p=p->next;
}
return c;
}


void List::insert(int poss,List_entry item)
{

if(poss==0)
{
Node *q=new Node(item);
q->next=head;
head=q;
}

if(poss>0&&poss<size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=new Node(item,p->next);
p->next=q;
}

if(poss==size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=new Node(item,NULL);
p->next=q;
}
}


void List::retrive(int poss,List_entry &item)
{
if(poss==0)
{
Node *q=head;
item=q->entry;
}
else
if(poss>0&&poss<size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=p;
item=q->entry;
}

else if(poss==size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=p;
item=q->entry;
}
}

void List::remove(int poss)
{
if(poss==0)
{
Node *q=head;
head=q->next;
delete q;
}

if(poss>0&&poss<size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q;
q=p->next;
p->next=q->next;
delete q;
}

if(poss==size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q;
q=p->next;
p->next=NULL;
delete q;
}
}

void List::repleas(int poss,List_entry item)
{
if(poss==0)
{
Node *q=head;
q->entry=item;
}
else
if(poss>0&&poss<size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=p;
q->entry=item;
}

else if(poss==size())
{
Node *p=head;
for(int i=1;i<poss;i++)
p=p->next;
Node *q=p;
q->entry=item;
}
}


void bubble(List &l,int m)
{
List_entry x,y;
for(int k=0;k<m;k++)
{
l.retrive(k,x);
l.retrive(k+1,y);
if(x<y){
l.repleas(k,y);
l.repleas(k+1,x);
}
cout<<x<<" ";
}}



void selection(List l,List_entry max,int last)
{
List_entry x,y;
int j=0;
if(last!=0)
{
for(int i=0;i<last;i++)
{
l.retrive(i,x);
if(x>max)
{
max=x;
j=i;
}
}
l.repleas(last,max);
l.retrive(last,y);
l.repleas(j,y);
last--;
}
selection(l,max,last);
}

void selection(List l)
{
List_entry x;
List_entry max;
l.retrive(0,x);
max=x;
int last=l.size();
selection(l,max,last);
}


void insertion(List l,int box)
{
List_entry x,y;
int i;
if(l.size()!=box)
{
i=box;
for(int j=i;j>0;j--)
{
l.retrive(j,x);
l.retrive(j-1,y);
if(x<y)
{
l.repleas(j,y);
l.repleas(j-1,x);
}
}
box++;
}
insertion(l,box);
}


void insertion(List l)
{
int box=1;
insertion(l,box);
}



void main()
{
List l;
List_entry x;
int m;
cin>>m;
for(int i=0;i<m;i++)
l.insert(i,(rand()%1000));

for(int j=0 ; j<m ; j++)
{
l.retrive(j,x);
cout<<x<<" ";
}

cout<<"\n";
clock_t b=clock();
{
bubble(l,m);
}
clock_t e=clock();
cout<<"time: "<<double(e-B)/>;
cin>>m;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Fix My Linked List Bubble Sort

#2 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Re: Fix My Linked List Bubble Sort

Posted 26 May 2010 - 10:10 AM

bkrah had almoq3... leh ma 7d bsa3dne

help :(

View Postmaxpower1991, on 26 May 2010 - 04:08 PM, said:

bkrah had almoq3... leh ma 7d bsa3dne

help :(


oops I have to write in English sorry
:D
Was This Post Helpful? -1
  • +
  • -

#3 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Re: Fix My Linked List Bubble Sort

Posted 26 May 2010 - 12:46 PM

No experts here ... All of you beginner :glare:
Was This Post Helpful? -5
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Fix My Linked List Bubble Sort

Posted 26 May 2010 - 01:30 PM

I don't think insulting EVERYONE, considering you are using an "absolute" fallacy above, is going to get you any help here. And maybe you if you posted the code section that you're having problems with, people would be more quick to help you...

The people here at DIC are more than willing to help, if you specify your problem, and the code portion where you're having an issue. You can't just come in and post your full, undocumented code and expect us to come running up and kiss your feet and fix your code so it compiles.

So in short, the help has not arrived because we (can't speak for all, but I) don't want to look through all of your code to find your issue, especially since I have no idea where the issue lies.
Was This Post Helpful? 1
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Fix My Linked List Bubble Sort

Posted 26 May 2010 - 01:38 PM

View Postmaxpower1991, on 26 May 2010 - 12:46 PM, said:

No experts here ... All of you beginner :glare:



Says the one asking for help. Hello pot, oh hi there kettle!


Please indent your code. Why do you have so much function delegation? It would make more sense to get a simple swap to work, then expand to your linked list. Throwing a bunch of random numbers into a list and then noticing the end(s)? are zero isn't much debugging.

It would have been helpful to see sample input/output.


In short you provided too much and not enough information at the same time.
Was This Post Helpful? 1
  • +
  • -

#6 maxpower1991  Icon User is offline

  • New D.I.C Head

Reputation: -14
  • View blog
  • Posts: 33
  • Joined: 21-May 10

Re: Fix My Linked List Bubble Sort

Posted 27 May 2010 - 12:03 AM

I'm so sorry because I was not polite :blush:

so ....
I will give you my output for 20 node in linked List with random NO.

this before sort:
 
346 346 130 982 90 656 117 595 415 948 126 4 558 571 879 492 360 412 721 463



after sorting for the same linked list I have this NO. :
346 346 130 130 90 90 90 90 90 90 90 4 4 4 4 4 4 4 4 4 



my Q is why its not sort the same liked list while I send the same linked list ???
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Fix My Linked List Bubble Sort

Posted 27 May 2010 - 02:58 AM

This is the pseudocode for bubble sort
http://en.wikipedia....iki/Bubble_sort

Does your code implement that algorithm?
If not then change your code until it does.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1