两张卡片c1和c4似乎相等......但它们不是原因.我希望它们相等,以便在Set中只允许其中一个.:|
import java.util.*;
class Card2
{
private int value;
private String type;
public Card2(int v,String t)
{
value=v;
type=t;
}
public int getValue()
{
return value;
}
public String getType()
{
return type;
}
public String toString()
{
return(type+" "+value);
}
public boolean equals(Object oo)
{
if(!(oo instanceof Card))
return false;
Card cc=(Card) oo;
if(this.getValue()==cc.getValue() && this.getType().equals(cc.getType()))
return true;
else
return false;
}
public int hashCode()
{
return value;
}
public static void main(String args[])
{
HashSet<Card> deck=new HashSet<Card>();
Card …Run Code Online (Sandbox Code Playgroud) 我正在阅读一个教程,我也在谷歌上搜索过,但我找不到关于链接列表如何工作的详细说明的很好的解释......我真的对结构/格式感到困惑,我真的希望链接列表有意义对我来说,它们听起来很棒,因为它们是一个可调整大小和可修改的数组……如果您需要了解我在说什么,下面是我从 tut 中获得的一些代码。我对追加方法或删除等方法、它们的作用以及列表中的尾头事物的工作方式感到困惑......这本书只是从一个例子开始,没有给出解释..
请帮助解决这个困惑..
class ListEntry
{
int data;
ListEntry next;
public ListEntry( int d )
{
data = d;
next = null;
}
public int Data
{
get{ return data; }
set{ data = value; }
}
public ListEntry Next
{
get{ return next; }
set{ next = value; }
}
public override string ToString( )
{
return( data.ToString( ) );
}
}
class TestProgram
{
static void Main( )
{
List list = new List( );
list.Append( 3); …Run Code Online (Sandbox Code Playgroud) 这是一个使用的FIFO程序linked list.该程序没有提供所需的输出,但会生成一个长循环,该循环在某个时间后停止并且有一条消息表明程序已停止工作.问题是什么 ?
#include <iostream>
using namespace std;
struct node {
int data;
struct node* previous; // This pointer keeps track of the address of the previous node
};
struct queue {
node* first;
node* last;
};
node* dataNode_P_A;
bool loop = true;
struct node* enterData();
struct node* enter_N_Data();
void displayQueue();
int main() {
struct node* dataNode= enterData();
while( loop ) {
cout << "Want to enqueue ? Press y/n : ";
char ans;
cin >> ans;
if( …Run Code Online (Sandbox Code Playgroud) 我知道这是重复的,但我的答案确实很复杂.有人说Hash表,但是对于哈希表,如果有两个名称那么它将如何唯一或者它与键是唯一的?
我应该使用哪种数据结构?
我想通过电话号码和姓名查询.电话簿也很大.
在用于评估后缀表达式的代码中,写入了将字符作为整数传递,我们将其写为character - '0'.这有什么意义?
我在C++中有两个类
Class A
{
public:
int v;
}
Class B
{
public :
void calculation ();
}
void B::calculation ()
{
int i;
for (i= 1; i < object_a.v ;i++)
{
//some body
}
}
//now I have created an object for class A and for that object I have
//got some value for "v"
int main ()
{
A object_a();
cout << "The value of variable v in class A is" << object_a.v << ;
}
Run Code Online (Sandbox Code Playgroud)
对于上面的示例代码,我尝试使用在类B中定义的另一个函数中使用object_a检索的变量.但是它会抛出未声明的错误.有人可以帮我这里怎么去吗?
PS:我在B类中编译函数"calculation()"时遇到错误
我想建立一个数据结构,其插入,删除,搜索是O(1).缓存删除缓存中请求最少的值.并且具有给定大小的值.有任何想法吗?
我想到了树+哈希表或skiplist +哈希表(缓存大小的哈希表).
谢谢
我不是很擅长数据结构,但我想尝试在C#List类中使用方法实现添加这是我需要的唯一方法,我无法弄清楚下一步该做什么我只有这段代码
public class myList<T> : List<T>
{
public T[] items;
public int size;
public myList()
{
items = new T[0];
}
public myList(int dim)
{
items = new T[dim];
}
public new void Add(T item)
{
items[size++] = item;
}
}
Run Code Online (Sandbox Code Playgroud)
我也使用继承到List,因为我不想实现其他东西(接口).当我试图看到我添加到列表(foreach)时我看不到有这些代码的东西我看不到没有.有人可以帮我吗?
最佳排序算法,可用于使用c中的结构对链表进行排序,其中获取错误的可能性最小(例如,分段)?
struct Dates
{
int day;
int month;
int year;
}accountinfo[3];
struct Accounts
{
string name,lastname;
int number;
float balance;
}account[3];
void sortduetoaccnumbers()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (account[j].number>account[j+1].number)
{
//swap
}
}
}
}
void sortduetodates()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (accountinfo[j].year>accountinfo[j+1].year)
{
//swap
}
else if (accountinfo[j].year==accountinfo[j+1].year)
{
if (accountinfo[j].month>accountinfo[j+1].month)
{
//swap
}
else if (accountinfo[j].month==accountinfo[j+1].month)
{
if (accountinfo[j].day>accountinfo[j+1].day)
{
//swap
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法使用排序算法对这些帐户进行排序.如果我输入它会崩溃.cmd突然停止并完成程序.
我进入了一个评论行,交换函数必须去.所以你可以分析代码.
除此之外,其他所有功能都在工作.我陷入了困境.
data-structures ×10
c# ×3
c++ ×3
c ×2
java ×2
linked-list ×2
algorithm ×1
caching ×1
equality ×1
fifo ×1
graph ×1
queue ×1
struct ×1
visual-c++ ×1