在增加链表中的大小时遇到​​问题 [英] Having problem in incrementing size in a linked list

查看:73
本文介绍了在增加链表中的大小时遇到​​问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在添加函数:confused:
中帮助增加链表的大小

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;
}
}
}

推荐答案

您需要的增量缓冲区大于1.假设这是256(我选择的任意数字).始终以256个块为单位添加内存.最初,您将有256个插槽,一旦集合达到256个项目,您将创建一个新的512个内部块,将256个项目从旧块复制到该块,然后删除旧块堵塞.一旦您击中512个项目,请类似地重复该过程.
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.


此代码无助.

它不会事件编译.它无法修复(或不值得)-从头开始,了解您的工作.

关于Add的问题是没有用的,因为在此功能之前一切都会出错.

size字段在node中没有任何意义.
int getdata()这样的函数应声明为int getdata() const.
节点数据不应是可变的(为什么setdata?),在构造函数中的分配已足够(但缺少构造函数).
如果发生灾难,则与控制台内部人员列表类进行I/O交互(我是说您就把它放在那儿了)-尤其希望用户在del(?!!)...
内部进行选择 还有什么..?您几乎无法使用这种文本格式来整理任何代码.它应该至少在最小程度上整齐;现在,只读它就很丢人.
....
该列表可能比所有代码"长几倍.

我不知道该如何帮忙.也许,退后一步,学习如何做更简单的事情...
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...


这篇关于在增加链表中的大小时遇到​​问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆