#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-1;i++)
p=p->next;
Node *q=new Node();
q=p->next;
p->next=q->next;
delete q;
}
if(poss==size())
{
Node *p=head;
for(int i=1;i<poss-1;i++)
p=p->next;
Node *q=new Node();
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 i)
{
List_entry x,y;
if(i!=l.size())
{
l.retrive(i,x);
l.retrive(i+1,y);
if(x>y)
{
l.repleas(i,y);
l.repleas(i+1,x);
}
i++;
}
bubble(l,i);
}
void bubble(List l)
{
int i=0;
bubble(l,i);
}
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()%101));
for(int j=0 ; j<m ; j++)
{
l.retrive(j,x);
cout<<x<<"\n";
}
cout<<"\n";
bubble(l);
for(int j=0 ; j<m ; j++)
{
l.retrive(j,x);
cout<<x<<"\n";
}
cin>>m;
}
1 error ?
Page 1 of 19 Replies - 474 Views - Last Post: 25 May 2010 - 02:45 AM
#1
1 error ?
Posted 24 May 2010 - 11:18 AM
Replies To: 1 error ?
#2
Re: 1 error ?
Posted 24 May 2010 - 11:20 AM
#3
Re: 1 error ?
Posted 24 May 2010 - 11:21 AM
help
#4
Re: 1 error ?
Posted 24 May 2010 - 11:34 AM
#5
Re: 1 error ?
Posted 24 May 2010 - 12:02 PM
#6
Re: 1 error ?
Posted 24 May 2010 - 12:04 PM
void bubble(List &l,int i)
{
List_entry x,y;
if(i!=l.size())
{
l.retrive(i,x);
l.retrive(i+1,y);
if(x>y)
{
l.repleas(i,y);
l.repleas(i+1,x);
}
i++;
}
bubble(l,i);
}
My Compiler threw this warning:
warning C4717: 'bubble' : recursive on all control paths, function will cause runtime stack overflow
So that will definately result in an infinite recursion loop. This should fix that problem (notice the good indenting that I have applied to your code):
void bubble(List &l, int i)
{
List_entry x, y;
if (i != l.size())
{
l.retrive(i, x);
l.retrive(i+1, y);
if (x > y)
{
l.repleas(i,y);
l.repleas(i+1,x);
}
// Modify i from the recursive function. That's
// how recursion works, right?
bubble(l, i + 1);
}
}
Also, a better indenting in the future, wouldn't hurt.
This post has been edited by sarmanu: 24 May 2010 - 12:05 PM
#7
Re: 1 error ?
Posted 24 May 2010 - 12:23 PM
thats the message come when I run the code and the program c++ just shot down...I don't know what the problem
and I need to complete my code but I cant because this problem... so what this message mean??
#8
Re: 1 error ?
Posted 24 May 2010 - 12:33 PM
Since you are using pointers you should check for null values whenever you access the list. Could you post your code as well as the line the error occurred on?
#9
Re: 1 error ?
Posted 24 May 2010 - 12:37 PM
and btw, void main is bad practice. Change to int main()
edit: got ninja'd
This post has been edited by ImaSexy: 24 May 2010 - 12:47 PM
#10
Re: 1 error ?
Posted 25 May 2010 - 02:45 AM
maxpower1991, on 25 May 2010 - 03:34 AM, said:
Yes it does help.
Reporting a bug correctly is a key skill a developer must learn.
This forum is a great place for you practicing how to best report problems in code where you don't get sacked for wasting time with poor error reporting.
If you don't believe people get fired for making reports like "This code is broken" then I am here to burst that bubble.
Nothing makes it clearer that someone is not going to make it as a useful team member than being useless at reporting errors and bugs .
Use the chance here to practice different ways to report bugs and errors and find out which is the best and fastest.
Copy and paste is something to consider because it is fast and requires no special language skills.
|
|

New Topic/Question
Reply




MultiQuote








|