what is the definition of compareTo method?
what is the definition of compareTo method?
how to check for greater or smaller?
how can i compare the values of temp.info and max.info until null is reached?
i.e. checking for if(temp.info>max.info).
i have to define compareTo method itself?
isnt it inbuilt function?
QueueImpl.java:65: cannot find symbol
symbol : method compareTo(QueueImpl<E>.Node)
location: class QueueImpl<E>.Node
if(((temp).compareTo(max))>0)
^
import java.util.*;
import java.io.*;
import java.lang.*;
public class QueueImpl<E extends Comparable<E>>
{
private int count;
private class Node
{
private E info;
Node next;
}
Node front;
Node rear;
public QueueImpl()
{
count = 0;
}
public void enqueue(E e)
{
Node x = new Node();
if(e==null)
{
throw new IllegalArgumentException();
}
else{
if(count==0)
{
x.info = e;
front = x;
rear = x;
}
else
{
x.info = e;
rear.next = x;
x.next = null;
rear = x;
}
count++;}
//System.out.println(x.info);
}
public E peekMaximum()
{
Node temp = new Node();
temp = front;
Node max = new Node();
//E max = temp;
max = temp;
while(temp!=null)
{
//System.out.println(temp.info);
//if(temp.info>max)
if(((temp).compareTo(max))>0)
//if(((Integer)(temp.info) - (Integer)(max.info)) >0)
{
max = temp;
}
//temp=(Node)temp.next;
temp= temp.next;
}
//Iterator i = q.Iterator();
return max.info;
//return null;
}
public static void main(String[] args)
{
QueueImpl<Integer> q = new QueueImpl<Integer>();
//ArrayList<Integer> a = new ArrayList();
q.enqueue(2);
q.enqueue(1);
q.enqueue(3);
q.enqueue(4);
System.out.println(q.peekMaximum());
//q.peekMaximum();
}
}
public class QueueImpl<E extends Comparable<E>>
{
private int count;
private class Node
{
private E info;
Node next;
}
Node front;
Node rear;
public E peekMaximum()
{
Node temp = new Node();
temp = front;
Node max = new Node();
max = temp;
while(temp!=null)
{
System.out.println(temp.info);
//if(temp.info>max)
if((temp).compareTo(max))>0) // getting error here
//if(((Integer)(temp.info) - (Integer)(max.info)) >0)
{
max = temp;
}
temp=temp.next;
}
return max.info;
}
}
i have implemented my own queue . now i want to find object having maximum value.
how should i compare the two objects?
int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf("\n %d", i);
i should have write this: explain the output
why not 100 is the output as pf is pointing to i and afterthat we change the value at the address pointed by pf?
int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf("\n %d", i);
are these declaration same?
const char *const s;
char const *const s;
in dev-c++ it works fine.
but the problem is when i submit the code on site. it gives me compile error.
i am using strrev function and also included the header file string.h but i am getting the compile error
‘strrev’ was not declared in this scope.
what else is required?
scanf works.
but how to do it in c++?
in my code i have to calculate just sqrt of input.
i tried scanf but it gives TLE too. so what should i do?
actually there is a question on a website in which number of test cases are <1000000
i am submitting there. the site is giving me time limit exceeded.
my code complexity is O(1). so i tried it on ideone which gives me run time error.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
long int t,n;
cin>>t;
while(t--)
{
cout<<"abc";
}
return 0;
}
i am getting run time error for t>10000.
how to solve it. range of t>1000000
ok..it works.
but what does queue<char*> myqueue means?
actually i m using strcmp function for ccomparing which is giving type mismatch error.
so i did this : queue<char*> myqueue .
earlier it was: queue<string> myqueue
now the error has gone and code works fine but i didnot understand what does queue<char*> myqueue means?
what type value the queue have?
and sorry for the late reply.
i have made a queue using STL. the queue contains strings only. how to check for a particular string if it present in queue or not?
it means that since no space is allocated for abcd so it is NULL? then what about abc? i didnot allocate memory for abc too?
@nitni1 then acc to you what should be the value of int a for abcd??
what about abcd? i also define it like abc.
@vish
you are saying pointing head to temp doesnot store any value unless i allocate memory to it.
#include<iostream>
using namespace std;
struct node
{
int a;
node *next;
};
struct node * head;
void insert()
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->a = 10;
temp->next = NULL;
head= temp;
cout<<head->a;
}
int main()
{
insert();
getch();
return 0;
}
#include<iostream>
//#include<dos.h>
#include<conio.h>
using namespace std;
struct node
{
int a;
node *next;
};
struct node * abcd;
//void show(node *);
void insert()
{
if(abcd==NULL)
cout<<"abcd";
}
int main()
{
struct node* abc;
if(abc==NULL) cout<<"abc";
insert();
getch();
return 0;
}
tell me why only 'abcd' is printed and not 'abc'?
(using dev-c++ compiler)
my head pointer is of type struct*. if head is pointing to first node of a linked list, its info part automatically stored the info of first node.
same goes for 'root' pointer of BST. i am trying to point it to root of BST by declaring in main(). the problem is i cant check it for NULL even if i dont allocate memory to it. whereas i can check for 'head' node for NULL in some other function but not in main . why ?
if i dont dont allocate memory here then it gives me run time error.
i did this:
struct node *root = NULL;
if(root==NULL) cout<< root->info;
my actual problem is:
i make a linked list and i make the 'head' pointer global. then i created a insert() function. now in this function i have this condition: if(start==NULL). it works fine.
now i want to make a BST. and i am using pointer 'root' in main() (as shown in code i have posted). but here i cant compare it to NULL. why is so?
I imagine it's not printing because root is not NULL.
why root is not NULL? or how can i make it to NULL?
struct node
{
int info;
node *left,*right;
};
int main()
{
struct node *root;
root = (struct node*)malloc(sizeof(struct node));
root->info=NULL;
root->left=NULL;
root->right=NULL;
if(root==NULL) printf("%d",root->info);
return 0;
}
why it doesnt print root->info?
oh yes..my mistake..this is C forum.sorry
it gives run time error.
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *left,*right;
};
int main()
{
struct node *root;
root=NULL;
//root->info=NULL;
//root = (struct node*)malloc(sizeof(struct node));
if(root==NULL) //cout<<root->info;
cout<<root->info<< " "<<root->left<<" "<<root->right<<"\n";
getch();
return 0;
}
why it is giving an error?
int a=-3,b=2,c=0,d;
d=++a&&++b||++c;
printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
why value of 'c' is not incremented in output?
char str[]="S\065AB";
printf("\n%d", sizeof(str));
explain the output.
i am getting 5.
str1="strings are good");
memmove(str1+8,str1+11,4);
returns------ strings are are good
memcpy(str+8,str1+11,4)
returns------ strings are read
i run the above code.
i am getting "strings googood" as my output for both cases
i understand my memmove output but why it is same as memcpy?
and what about memcpy and memmov?
can u explain me with the help of a code?
this means when i used memcpy the whole string is copied in str1 while using strcpy str1 contains only "mem"??
Quoted Text Here
strcpy() uses '\0' as the stopping character
char str[] = "mem\0abc";
char str1[20];
memcpy (str1,str,10);
//strcpy(str1,str);
puts (str1);
both memcpy and strcpy giving me same output i.e. mem
what is difference between a)strcpy and memcpy b) memcpy and memmov?
@deceptikon..ok got it..thanks
@deceptikon..then why dont the first code is working without allocating the memory?
my basic ques is do we need to allocate memory to a pointer before using it? i mean in my first code why dont we malloc 'b' before the (b=a) statement?
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<conio.h>
#include<math.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int *b;
//b=(int *) malloc(sizeof(a));
//b=a;
memcpy(b,a,sizeof(a));
//cout<<b;
FOR(i,0,10) cout<<b[i]<<" ";
//cout<<sizeof(b);
getch();
return 0;
}
now i am using memcpy function to copy the bytes. but here i require to allocate memory to b. why is so?
@sokurenko...how can we use a pointer without allocating memory to it?
@waltp..sorry for the inconvenience..actually i have a file in which i have defined all the header file and macros. i just copied all these and make a new program.i actually delete most of them here.
@priya..the question is simple. i have to copy the contents of array a to b without using any loop or inbuit function
Why the program is running without line 14?
#include<stdio.h>
#include<iostream>
//#include<conio.h>
//#include<math.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int *b;
//b=(int *) malloc(sizeof(a));
b=a;
//cout<<b;
FOR(i,0,10) cout<<b[i]<<" ";
//cout<<sizeof(b);
//getch();
return 0;
}
i am new to android application development. i have run hello world program only.
now i want to run some sample code.
when i try to run them i got this error in new dialog box:
"Could not write file: C:\Program Files (x86)\Android\android-sdk\samples\android-15\SoftKeyboard.project.
C:\Program Files (x86)\Android\android-sdk\samples\android-15\SoftKeyboard.project (Access is denied)"
i am trying to run 'softkeyboard' sample .
i did this: new android project>create project from another source>
and the path i have given is C:\Program Files (x86)\Android\android-sdk\samples\android-15\SoftKeyboard
why is this error coming?
strcat (string3, &string2[start-of-second-half]); This passes the address of the position of string2 you want to add.
start of second half can be calculated by using strlen and half of it.
the string contains only the numerals from 0-9.
i am using 'atof' function to convert string into double but it shows the correct value if the string length is 17(max). is ther any function if string length is more than 17?
@djsan...yeah you are right,my mistake..:)
oopes the first line is :Cant we compare two strings for inequality?
cant we compare two for inequality?
i mean if i have a char array 'a' and i want to know the number of digits in an array which is not '4' or not '7'
i.e if my array has value 4567778 the output should be 3 as there are three 7's and one 4 so output is 3.
my condition for check is (a!=52!!a!=55).
but this condition is never checked and if my condition is (a==52!!a==55) i got right output.
so that means we can only compare if the strings are equal?
this code give wrong output:
#include<stdio.h>
int main()
{
int t,i,o;
char a[100000];
scanf("%d",&t);
while(t--)
{
i=0;
o=0;
scanf("%s",a);
while(a[i]!=NULL)
{
if(a[i]!=52||a[i]!=55) //this condition is not working?
o++;
i++;
}
printf("%d",o);
}
return 0;
}
this works fine:
#include<stdio.h>
//#include<string.h>
//#include<conio.h>
using namespace std;
int main()
{
int t,i,o;
char a[100000];
// cin>>t;
scanf("%d",&t);
while(t--)
{
i=0;
o=0;
scanf("%s",a);
while(a[i]!=NULL)
{
if(a[i]==52||a[i]==55)
o++;
i++;
}
printf("%d ",i-o);
}
return 0;
}