Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Need help in increment of size of a linked list in add function :confused:
#include<iostream.h>
class node
{
private:
	int data;
	int size;
	node *next;
public:
	node()
	{
size=1;
data=0;
next=NULL;

	}
node(int d,int a)
{
data=d;
size=a;
size++;

next=NULL;

}
void setdata(int d)
{
data=d;

}
void setsize(int s)
{
size=s;
//size++;
}
int getsize()
{
	return size;
}

int getdata()
{
return data;


}
void setnext(node *n)
{
next=n;
}
node *getnext()
{
return next;
}
void display()
{
cout<<data<<"|"<<size<<endl;
}
};
class nodelist
{
private:
	node *head;
	node *current;
	node *lastcurrent;
public:
	nodelist()
	{
head=current=lastcurrent=NULL;

	}

void add(int d)// i want to increment size after addition of every node for eg when a node is added it size is 1 then another adds its size becomes 2 and so on 

{
int si=1;
node *temp=new node(d,si);


	if(head==NULL)
{
		
		head=current=lastcurrent=temp;
//		head->setsize(si);

}
while(current->getnext()!=NULL)
{
	cout<<"add loop"<<endl;
lastcurrent=current;
current=current->getnext();
current->getnext();

}

current->setnext(temp);
lastcurrent=current;
current=temp;
current->setnext(NULL);

}
void del()
{
int a=0;
cout<<"enter the choice:"<<endl;
cout<<"1:remove from tail"<<endl;
cout<<"2:remove from the head"<<endl;
cout<<"3:remove from user choosed node"<<endl;
cin>>a;
cout<<endl<<endl;
switch(a)
{
case 1:
	{
		cout<<"the list after tail deletion is:"<<endl<<endl;
node *temp=new node();
temp=current;
current=lastcurrent;
delete []temp;
current->setnext(NULL);
	}
break;
case 2:
	{
cout<<"the list after the deltion of data from head is:"<<endl<<endl;
node *temp=new node();
temp=head;
head=head->getnext();
delete []temp;
	}
break;
case 3:
	{
		node *temp=new node();
		temp=head;
			node *temp1=new node();
	node *temp2=new node();

int c=0;
cout<<"enter the data from the list to delete:"<<endl<<endl;
cin>>c;
while(temp->getnext()!=NULL)
{
temp1=temp;

temp=temp->getnext();
temp->getnext();
if(c==head->getdata())
{
node *temp=new node();
temp=head;
head=head->getnext();
delete []temp;
break;

}
else if(c==temp->getdata())
{
	




temp2=temp;
temp=temp1;
temp->setnext(temp2->getnext());
delete []temp2;

break;



	}
else if(c==current->getdata())
{
node *temp=new node();
temp=current;
current=lastcurrent;
delete []temp;
current->setnext(NULL);
break;


}

}

	}
}

}
void searching()
{
	node *temp=new node();
	temp=head;
	int c=0,count=1;
	cout<<"enter the number to search in the list:"<<endl;
	cin>>c;
while(temp->getnext()!=NULL)
{
if(c==temp->getdata())
{
cout<<"found "<<c<<" at node"<<count<<endl<<endl;

}
temp=temp->getnext();
temp->getnext();
count++;

}

}void display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
 temp->display();
temp=temp->getnext();

}

}
//void xyz(nodelist x,nodelist y)
//{



//}
void sort()
{int t=0;
for(node *temp=head;temp!=NULL;temp=temp->getnext())
{
for(node *temp1=temp->getnext();temp1!=NULL;temp1=temp1->getnext())
{//cout<<"temp 1 "<<temp1->getdata()<<" temp "<<temp->getdata()<<endl;
	if(temp1->getdata()<temp->getdata())
	{//cout<<"temp  "<<temp->getdata()<<" temp 1 "<<temp1->getdata()<<endl;
		
t=temp->getdata();
temp->setdata(temp1->getdata());
temp1->setdata(t);


	}

}

}


}
};


void main()
{
char ch=NULL;

//int a=1,b=2,c=3,d=15,e=20,f=27;
//int g=1,h=3,i=4,j=6;
nodelist x,y;int a;
//node l,p;
/*x.add(a);
x.add(b);
x.add(c);
x.add(d);
x.add(e);
x.add(f);
x.display();

y.add(g);
y.add(h);
y.add(i);
y.add(j);*/


while(ch!='6')
{
cout<<"1:To add data"<<endl;
cout<<"2:To delete data from list"<<endl;
cout<<"3:To Sort list in ascendin order"<<endl;
cout<<"4:To display list"<<endl;
cout<<"5:To search data in list"<<endl;
cout<<"6:to quit"<<endl;
cout<<"Enter the choice:"<<endl;
cin>>ch;

switch(ch)
{
case '1':
	{
		int size=0;
		cout<<"Enter the size to enter data:"<<endl;
		cin>>size;
		cout<<"enter integer data in list:"<<endl;
for(int i=1;i<=size;i++)
{cin>>a;
	x.add(a);
	}
}
	break;
case '2':
	{

x.del();
x.display();
	}
	break;
case '3':
	{cout<<"the sorted list is:"<<endl;
x.sort();
x.display();
	}
break;
case '4':
	{
cout<<"the data stored in list is"<<endl;
		x.display();


	}
	break;
case '5':
	{
cout<<endl;
x.searching();
	}

break;
}
}
}
Posted
Updated 19-Jan-11 6:35am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jan-11 12:44pm    
Another school assignment... where is your question? I don't think anybody is interested to put this boring code right for you.
irtz 19-Jan-11 12:58pm    
my question is just this i cant figure out how to increment size rest program is running.
Manfred Rudolf Bihy 19-Jan-11 13:14pm    
Oh my god! It looks like you've been sleep coding again.
Here's your sign!

You need to have a bigger increment-buffer than 1. Say this is 256 (arbitrary number I chose). Always add memory in blocks of 256. Initially you will have 256 slots, once the collection gets to 256 items, you create a new internal block of 512 items, copy over the 256 items from the old block to it, and then delete the old block. Once you hit 512 items, repeat the process similarly.
 
Share this answer
 
v2
Comments
irtz 19-Jan-11 12:58pm    
you mean i need another temp pointer to maintain the size also
Nish Nishant 19-Jan-11 13:06pm    
You don't need a pointer, just an int member to hold the max size will do.
irtz 19-Jan-11 15:09pm    
Thanks i got it i just added a counter in main and then passed the parameter in function of add

void add(int d,int si)// i want to increment size after addition of every node for eg when a node is added it size is 1 then another adds its size becomes 2 and so on
{
node *temp=new node(d,si);


if(head==NULL)
{

head=current=lastcurrent=temp;


}
while(current->getnext()!=NULL)
{
cout<<"add loop"<<endl;
lastcurrent=current;
current=current->getnext();
current->getnext();

}

current->setnext(temp);
lastcurrent=current;
current=temp;
current->setnext(NULL);

}
void main()
{
char ch=NULL;
nodelist x;
int a;
while(ch!='6')
{
cout<<"1:To add data"<<endl;
cout<<"Enter the choice:"<<endl;
cin>>ch;

switch(ch)
{
case '1':
{
int size=0,s=1;
cout<<"Enter the size to enter data:"<<endl;
cin>>size;
cout<<"enter integer data in list:"<<endl;
for(int i=1;i<=size;i++)
{cin>>a;
x.add(a,s);
s++;
}
}
break;



}
}
}
Nish Nishant 19-Jan-11 15:11pm    
Ok, cool.
Sergey Alexandrovich Kryukov 19-Jan-11 13:08pm    
Nishant, don't you see your concerns are too advanced? Just look at very first lines...
This code is helpless.

It won't event compile. It cannot be fixed (or does not worth it) -- start from scratch, understand what are your doing.

Question about Add is useless, because everything goes wrong well before this function.

The field size makes no sense in node.
Functions like int getdata() should be declared int getdata() const.
Node data should not be mutable (why setdata?), assignment in constructor is enough (but constructor is missing).
I/O interaction with console insider list class if a disaster (I mean the mere fact your put it there) -- especially expecting user to select choice inside del (?!!)...
What else..? You hardly can sort out any code with such text formatting; it should be neat at least minimally; right now, just reading it is humiliation.
....
The list could be several time longer than all the "code".

I don't know how can I help. Perhaps, step back and learn how to do even more simple things...
 
Share this answer
 
v2
Comments
Nish Nishant 19-Jan-11 13:11pm    
Yours is a much better and well thought out answer, I've voted it a 5.
Sergey Alexandrovich Kryukov 19-Jan-11 13:21pm    
Thank you.
fjdiewornncalwe 19-Jan-11 13:57pm    
+5. Well described.
irtz 19-Jan-11 14:07pm    
all i wanted to ask is this when i add some nodes the output should be like 22(data in list)|1(size of the list), 12|2, 40|3 and so on but iam having problem when i increment it gives like 22|2,12|2,40|2 and i have used functions getdata() ,setdata() etc in case there is a problem in my code i.e adding, deleting etc so i should sort it out.
Sergey Alexandrovich Kryukov 19-Jan-11 15:37pm    
I don't know how else to explain... all right, remove the field size from the class "node", add such field in the class "nodelist" and you might see. If all nodes are accessed, added, or removed to "nodelist", how do you think, what's a "size", which class it belongs to? Does it provide you a clue now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900