Searching for "array"

Q:

Difference between Arraylist and Linked list?

Answer

ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non synchronized classes.


 


But there are many differences between ArrayList and LinkedList classes that are given below.


 

ArrayList    V/s     LinkedList ::


 

1) ArrayList internally uses dynamic array to store the elements.


                   Whereas


LinkedList internally uses doubly linked list to store the elements.


 

2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.


                   Whereas


Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory.


 

3) ArrayList class can act as a list only because it implements List only.


                   Whereas


LinkedList class can act as a list and queue both because it implements List and Deque interfaces.


 

4) ArrayList is better for storing and accessing data.


                   Whereas


LinkedList is better for manipulating data.

Report Error

View answer Workspace Report Error Discuss

Q:

How do you decide when to use ArrayList and When to use LinkedList?

Answer

If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList offers the optimal collection. If, however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList offers the better implementation.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

Q:

Whats the difference between a NSArray and a NSMutableArray?

Answer

A NSArray’s contents can not be modified once it’s been created whereas a NSMutableArray can be modified as needed, i.e items can be added/removed from it.

Report Error

View answer Workspace Report Error Discuss

Q:

Write an algorithm to separate all ones & zeroes in an array.

Answer

1. Have two indexes pointing to two ends of array, say i and j.


2. Approach towards each other with a check condition that they dont cross each other.


3. Each iteration of while loop, swap the numbers pointed by two indexes when num[i] index number is not equal to 1.


 


void sort()


{


     int a[]={1,0,0,0,1,1,0,1,0,1,0,0,1,0};


     int i=0;


     int j=13;


     int temp;


      while(j>i)


     {


          if(a[i]==1)


                 i++;


          if(a[j]==0)


                 j--;


          if(a[i]==0)


         {


                 temp=a[i];


                a[i]=a[j];


                a[j]=temp;


         }


     } 


     for(i=0;i<14;i++)


             Console.Write(a[i]+", ");


}


Output: 1,1,1,1,1,1,0,0,0,0,0,0,0

Report Error

View answer Workspace Report Error Discuss

Q:

Differentiate linked list from arrays.

Answer

Arrays can store data in a fix allotted space. The use of linked list allows more flexibility because space is dynamically allocated as needed.

Report Error

View answer Workspace Report Error Discuss

Q:

How would you use qsort() function to sort an array of structures?

Answer

#include "string.h"


#include "stdlib.h"


struct stud


{


       int rollno;


       int marks;


       char name[30];


};


int sort_m (struct stud *, struct stud *);


int sort_name (struct stud *, struct stud *);


int sort_marks (struct stud *, struct stud *);


 


main()


{


static struct stud ss[] = {


                                            { 15, 96, "Akshay" },


                                            { 2, 97, "Madhuri" },


                                            { 8, 85, "Aishvarya" },


                                            { 10, 80, "Sushmita" }


                                   };


int x,w;


clrscr();


w = sizeof (struct stud);


 


printf ('\nIn order of roll numbers:");


qsort (ss, 4, w, sort_rn);


for(x=0; x<4;x++)


     printf ("\n%d%s%d", ss[x].rollno, ss[x].name,ss[x].marks);


 


printf("\n\nIn order of names:");


qsort(ss, 4, sort_name);


 


for (x=0; x<4;x++)


      printf("\n%d%s%d",ss[x].rollno, ss[x].name,ss[x].marks);


printf("\n\nIn order of marks:");


qsort(ss,4,w,sort_marks);


 


for (x=0;x<4;x++)


      printf ("\n%d%s%d",ss[x].rollno,ss[x].name,ss[x].marks);


}


int sort_rn (struct stud *t1, struct stud *t2)


{


     return (t1->rollno-t2->rollno);


}


 


int sort_name (struct stud *t1, struct stud *t2)


{


     return (strcmp(t1->name,t2->name));


}


int sort_marks (struct stud *t1, struct stud *t2)


{


     return (t2->marks-t1->marks);


}


 


 


 

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

Q:

Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?

Answer

Yes, using the realloc() function as shown below:


main()


{


        int *p;


        p = ( int *) malloc (20) ;


        t = p;


        t = (int *) realloc ( p, 40);


        if ( t == NULL )


        Printf (" Cannot reallocate, leaves previous allocated region unchanged ");


       else


       {


              if ( p ==t )


              ;  / * the array expanded at the same region */


             else


            { 


                 free ( p ); / * deallocate the original array */


                 p = t;  /* set p to newly allocated region */


             }


      }


}  

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

Q:

How would you dynamically allocate a 2-D array of integers?

Answer

#include "alloc.h"


#define MAXROW 3


#define MAXcol 4


main()


{


        int *p, i, J;


        p = (int *) malloc (MAXROW * MAXCOL * sizeof (int));


         for ( i=0; i < MaxROW ; i++)


         {


                for (j=0; j < MAXCOL ; j++)


                { 


                      p [ i * MAXCOL + j] = i;


                       printf ( "%d", p [i * MAXCOL + j] );


                 }


                  printf ("\n");


          }


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming