Write a program to Implement a Linked List in C++

The user is provided with the following options..



To insert an item at begining

To insert an item at end

To insert an item at position

To traverse the list

To delete first item

To delete last item

To delete item from position

To count no. of items in list





Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>

  2. #include<stdio.h>

  3. #include<alloc.h>

  4. #include<process.h>

  5. struct node

  6. { int info;

  7. struct node *next;

  8. };


  9. typedef struct node sn;


  10. void insertatbeg(sn **,int);

  11. void insertatend(sn **,int);

  12. void insertatpos(sn **,int,int);

  13. void traverse(sn *);

  14. int delfirst(sn *);

  15. int dellast(sn *);

  16. int delpos(sn *,int);

  17. int count(sn *);

  18. //Coding by: Snehil Khanor

  19. //http://WapCPP.blogspot.com

  20. void main()

  21. { sn *p;

  22. int n,i,l;

  23. char ch;

  24. clrscr();

  25. p=NULL;

  26. do

  27. { printf("\n-----select an operation-----");

  28. printf("\n1->To insert an item at begining<-");

  29. printf("\n2->To insert an item at end<-");

  30. printf("\n3->To insert an item at position<-");

  31. printf("\n4->To traverse the list<-");

  32. printf("\n5->To delete first item<-");

  33. printf("\n6->To delete last item<-");

  34. printf("\n7->To delete item from position<-");

  35. printf("\n8->To count no. of items in list<-");

  36. printf("\n9->Exit<-");

  37. printf("\n->Enter your choice:");

  38. scanf("%d",&n);

  39. switch(n)

  40. { case 1:printf("\n->Enter the item to insert");

  41. scanf("%d",&i);

  42. insertatbeg(&p,i);

  43. break;

  44. case 2:printf("\n->Enter the item to insert");

  45. scanf("%d",&i);

  46. insertatend(&p,i);

  47. break;

  48. case 3:printf("\n->Enter the item to insert");

  49. scanf("%d",&i);

  50. printf("\n->Enter the position");

  51. scanf("%d",&l);

  52. insertatpos(&p,i,l);

  53. break;

  54. case 4:traverse(p);

  55. break;

  56. case 5:i=delfirst(p);

  57. printf("\nItem deleted is %d.",i);

  58. break;

  59. case 6:i=dellast(p);

  60. printf("\nItem deleted is %d.",i);

  61. break;

  62. case 7:printf("\n->Enter the position");

  63. scanf("%d",&l);

  64. i=delpos(p,l);

  65. printf("\nItem deleted is %d.",i);

  66. break;

  67. case 8:i=count(p);

  68. printf("\nNumber of nodes in list are %d.",i);

  69. break;

  70. case 9:exit(0);

  71. break;

  72. default:printf("\n->You have entered wrong choice");

  73. }

  74. printf("\n->Do you want to continue/exit(y/n)");

  75. fflush(stdin);

  76. scanf("%c",&ch);

  77. } while((ch=='y')||(ch=='Y'));

  78. getch();

  79. }


  80. int count(sn *p)

  81. { sn *temp;

  82. int c=1;

  83. temp=p;

  84. while((temp->next)!=NULL)

  85. { c++;

  86. temp=temp->next;

  87. }

  88. return c;

  89. }


  90. void insertatbeg(sn **p,int item)

  91. { sn *ptr;

  92. ptr=(sn *)malloc(sizeof(sn));

  93. ptr->info=item;

  94. if(*p==NULL)

  95. ptr->next=NULL;

  96. else

  97. ptr->next=*p;

  98. *p=ptr;

  99. }


  100. void insertatend(sn **p,int item)

  101. { sn *ptr,*temp;

  102. ptr=(sn *)malloc(sizeof(sn));

  103. ptr->info=item;

  104. ptr->next=NULL;

  105. if(*p==NULL)

  106. *p=ptr;

  107. else

  108. { temp=*p;

  109. while((temp->next)!=NULL)

  110. temp=temp->next;

  111. temp->next=ptr;

  112. }

  113. }


  114. void insertatpos(sn **p,int item,int loc)

  115. { sn *ptr,*temp,*s;

  116. int i,c;

  117. temp=*p;

  118. ptr=(sn *)malloc(sizeof(sn));

  119. ptr->info=item;

  120. c=count(*p);

  121. if(loc>c)

  122. printf("\nList is short,Item can't inserted");

  123. else

  124. {

  125. for(i=0;i<loc-1;i++)

  126. { s=temp;

  127. temp=temp->next;

  128. }

  129. ptr->next=s->next;

  130. s->next=ptr;

  131. }

  132. }


  133. //Coding by: Snehil Khanor

  134. //http://WapCPP.blogspot.com

  135. void traverse(sn *p)

  136. { sn *temp;

  137. if(p==NULL)

  138. printf("\nList is empty.");

  139. else

  140. {

  141. temp=p;

  142. while((temp->next)!=NULL)

  143. { printf("\nno.=%d",temp->info);

  144. temp=temp->next;

  145. }

  146. printf("\nno.=%d",temp->info);

  147. }

  148. }


  149. int delfirst(sn *p)

  150. { int item;

  151. sn *temp;

  152. if(p==NULL)

  153. { printf("\nList is empty");

  154. return 0;

  155. }

  156. else

  157. { temp=p;

  158. p=p->next;

  159. item=temp->info;

  160. free(temp);

  161. return item;

  162. }

  163. }


  164. int dellast(sn *p)

  165. { int item;

  166. sn *temp,*s;

  167. if(p==NULL)

  168. { printf("\nList is empty");

  169. return 0;

  170. }

  171. else

  172. { temp=p;

  173. while((temp->next)!=NULL)

  174. { s=temp;

  175. temp=temp->next;

  176. }

  177. item=temp->info;

  178. s->next=NULL;

  179. free(temp);

  180. return item;

  181. }

  182. }


  183. int delpos(sn *p,int loc)

  184. { int item,i,c;

  185. sn *temp,*s;

  186. c=count(p);

  187. if(p==NULL)

  188. { printf("\nList is empty");

  189. return 0;

  190. }

  191. else

  192. { if(loc>c)

  193. { printf("\nItem is not in list.");

  194. return 0;

  195. }

  196. else

  197. { temp=p;

  198. for(i=0;i<loc-1;i++)

  199. { s=temp;

  200. temp=temp->next;

  201. }

  202. item=temp->info;

  203. s->next=temp->next;

  204. free(temp);

  205. return item;

  206. }

  207. }

  208. }


Write a program to Implement a Queue in C++

User is asked to Insert into.. Delete from or Display the entire queue..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #include<process.h> 

  4. int queue[5]; 

  5. long front,rear; 

  6. void initqueue(); 

  7. void display(); 

  8. void main() 


  9. int choice,info; 

  10. clrscr(); 

  11. while(1) 


  12. clrscr(); 

  13. printf(" MENU \n"); 

  14. printf("1.Insert an element in queue\n"); 

  15. printf("2.Delete an element from queue\n"); 

  16. printf("3.Display the queue\n"); 

  17. printf("4.Exit!\n"); 

  18. printf("Your choice: "); 

  19. scanf("%i",&choice); 

  20. //Coding by: Snehil Khanor  

  21. //http://WapCPP.blogspot.com  

  22. switch(choice) 


  23. case 1:if(rear<4) 


  24. printf("enter the number: "); 

  25. scanf("%d",&info); 

  26. if (front==-1) 


  27. front=0; 

  28. rear=0; 


  29. else 

  30. rear=rear+1; 

  31. queue[rear]=info; 


  32. else 

  33. printf("queue is full"); 

  34. getch(); 

  35. break; 

  36. case 2: int info; 

  37. if(front!=-1) 


  38. info=queue[front]; 

  39. if(front==rear) 


  40. front=-1; 

  41. rear=-1; 


  42. else 

  43. front=front+1; 

  44. printf("no deleted is = %d",info); 


  45. else 

  46. printf("queue is empty"); 

  47. getch(); 

  48. break; 

  49. case 3: display(); 

  50. getch(); 

  51. break; 

  52. case 4: exit(1); 

  53. break; 

  54. default:printf("You entered wrong choice!"); 

  55. getch(); 

  56. break; 




  57. void initqueue() 


  58. front=rear=-1; 


  59. void display() 


  60. int i; 

  61. for(i=front;i<=rear;i++) 

  62. printf("%i\n",queue[i]); 


Write a program to Implement a stack in C++

User is asked to PUSH, POP , or TRAVERSE.. PUSH is to add items to the Stack, POP is to remove item from the Stack, and TRAVERSE is to traverse the whole Stack

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #include<process.h> 

  4. #define MAXSIZE 10 

  5. void push(); 

  6. int pop(); 

  7. void traverse(); 

  8. int stack[MAXSIZE]; 

  9. int Top=-1; 

  10. void main() 


  11. int choice; 

  12. char ch; 

  13. do 


  14. clrscr(); 

  15. printf("\n1. PUSH "); 

  16. printf("\n2. POP "); 

  17. printf("\n3. TRAVERSE "); 

  18. printf("\nEnter your choice "); 

  19. scanf("%d",&choice); 

  20. switch(choice) 


  21. case 1: push(); 

  22. break; 

  23. case 2: printf("\nThe deleted element is %d ",pop()); 

  24. break; 

  25. case 3: traverse(); 

  26. break; 

  27. default: printf("\nYou Entered Wrong Choice"); 


  28. printf("\nDo You Wish To Continue (Y/N)"); 

  29. fflush(stdin); 

  30. scanf("%c",&ch); 


  31. while(ch=='Y' || ch=='y'); 


  32. //Coding by: Snehil Khanor  

  33. //http://WapCPP.blogspot.com  

  34. void push() 


  35. int item; 

  36. if(Top == MAXSIZE - 1) 


  37. printf("\nThe Stack Is Full"); 

  38. getch(); 

  39. exit(0); 


  40. else 


  41. printf("Enter the element to be inserted "); 

  42. scanf("%d",&item); 

  43. Top= Top+1; 

  44. stack[Top] = item; 



  45.  

  46. int pop() 


  47. int item; 

  48. if(Top == -1) 


  49. printf("The stack is Empty"); 

  50. getch(); 

  51. exit(0); 


  52. else 


  53. item = stack[Top]; 

  54. Top = Top-1; 


  55. return(item); 


  56.  

  57. void traverse() 


  58. int i; 

  59. if(Top == -1) 


  60. printf("The Stack is Empty"); 

  61. getch(); 

  62. exit(0); 


  63. else 


  64. for(i=Top;i>=0;i--) 


  65. printf("Traverse the element "); 

  66. printf("%d\n",stack[i]); 




Write a program to sort elements of array using Selection Sorting

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. void main() 


  4. int a[100],n,i,j,temp,loc,min; 

  5. clrscr(); 

  6. printf("\How many elements:\n"); 

  7. scanf("%d",&n); 

  8. printf("Enter the element of array\n"); 

  9. for(i=0;i<=n-1;i++) 


  10. scanf("%d",&a[i]); 


  11. min=a[0]; 

  12. //Coding by: Snehil Khanor  

  13. //http://WapCPP.blogspot.com  

  14. for(i=0;i<=n-1;i++) 


  15. min=a[i]; 

  16. loc=i; 

  17. for(j=i+1;j<=n-1;j++) 


  18. if(a[j]<min) 


  19. min=a[j]; 

  20. loc=j; 



  21. if(loc!=1) 


  22. temp=a[i]; 

  23. a[i]=a[loc]; 

  24. a[loc]=temp; 


  25.  


  26. printf("The number after selection sorting are:\n"); 

  27. for(i=0;i<=n-1;i++) 


  28. printf("%d\n",a[i]); 


  29. getch(); 



Write a program to sort elements of array using Quick Sort

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #define max 100 

  4. int a[max],n,i,l,h; 

  5. void main() 


  6. void input(void); 

  7. input(); 

  8. getch(); 


  9.  

  10. void input(void) 


  11. void output(int a[],int n); 

  12. void quick_sort(int a[],int l,int h); 

  13. printf("How many elements in the array : "); 

  14. scanf("%d",&n); 

  15. printf("\n"); 

  16. printf("Enter the elemennts : \n"); 

  17. for(i=0;i<=n-1;i++) 


  18. scanf("%d",&a[i]); 


  19. l=0; 

  20. h=n-1; 

  21. quick_sort(a,l,h); 

  22. printf("Sorted Array :\n "); 

  23. output(a,n); 


  24.  

  25. void quick_sort(int a[],int l, int h) 


  26. int temp,key,low,high; 

  27. low=l; 

  28. high=h; 

  29. key=a[(low+high)/2]; 

  30. do 


  31. while(key>a[low]) 


  32. low++; 


  33. while(key<a[high]) 


  34. high--; 


  35. if(low<=high) 


  36. temp=a[low]; 

  37. a[low++]=a[high]; 

  38. a[high--]=temp; 


  39. //Coding by: Snehil Khanor 

  40. //http://WapCPP.blogspot.com 

  41. } while(low<=high); 

  42. if(l<high) 

  43. quick_sort(a,l,high); 

  44. if(low<h) 

  45. quick_sort(a,low,h); 


  46. void output(int a[],int n) 


  47. for(i=0;i<=n-1;i++) 


  48. printf("%d\n",a[i]); 


  49. }

Write a program to sort elements of array using Insert Sorting

Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. main() 


  3. int a[100],n,k,i,j,temp; 

  4. printf("How many elements: \n"); 

  5. scanf("%d",&n); 

  6. printf("Enter the element of array: \n"); 

  7. for(i=0;i<=n-1;i++) 

  8. scanf("%d",&a[i]); 

  9. for(k=1;k<=n-1;k++) 


  10. temp=a[k]; 

  11. j=k-1; 

  12. //Coding by: Snehil Khanor 

  13. //http://WapCPP.blogspot.com 

  14. while((temp<a[j])&&(j>=0)) 


  15. a[j+1]=a[j]; 

  16. j=j-1; 


  17. a[j+1]=temp; 


  18. printf("Elements of array after sorting: \n"); 

  19. for(i=0;i<=n-1;i++) 

  20. printf("%d\n",a[i]); 

  21. getch();


Swap two variables without using third and in one line

Couple of weeks back I was interviewing a fresh graduate. Even though they are taught programming, I am not sure if they take it seriously and learn or practice it well. One of the questions I asked was to swap 2 numbers without using a temp variable.

Looking back now, I think it may be a bigger challenge to ask to swap numbers without using a temp variable and in one line. Below are my three different approaches but I would advise you to try it yourself before looking at the answer.


//Program to swap 2 numbers without using 3rd variable and in one line
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

void
approach1(int& a, int& b)
{

cout<<"\nApproach 1"<<endl;
a^=b^=a^=b;
}


void
approach2(int& a, int& b)
{

cout<<"\nApproach 2"<<endl;
//b=(a+b)-(a=b); - This should work but doesnt, why?
a =((a = a + b) - (b = a - b));
}


void
approach3(int& a, int& b)
{

cout<<"\nApproach 3"<<endl;
a = ((a = a * b) / (b = a / b));
}




int
main()
{

int
a = 13, b = 29;
cout<<"\nOriginal"<<endl;
cout<<"a = "<<a<<", b = "<<b<<endl;

approach1(a, b);
cout<<"a = "<<a<<", b = "<<b<<endl;

a = 13, b = 29;
approach2(a, b);
cout<<"a = "<<a<<", b = "<<b<<endl;

a = 13, b = 29;
approach3(a, b);
cout<<"a = "<<a<<", b = "<<b<<endl;

return
0;
}


The output is as follows:
Agreed that the above would be applicable only for integers.

'sizeof' a class

Continuation from last week. What is the sizeof of a class? Wrote a simple program as follows:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

class
A
{

int
a;
};


class
B
{

int
b;
int
someFunc(void) {return b;}
};


class
C
{

void
someFunc1(void) {};
void
someFunc2(void) {};
};


class
D : public A
{

void
someFunc(void) {};
};



int
main()
{

cout<<"Size of class A = "<<sizeof(A)<<endl;
cout<<"Size of class B = "<<sizeof(B)<<endl;
cout<<"Size of class C = "<<sizeof(C)<<endl;
cout<<"Size of class D = "<<sizeof(D)<<endl;
}



The output is as follows:

After finishing this, I found this interesting article here.

Html Forms Tutorial video

http://www.youtube.com/watch?v=TeV2eDHtVa0endofvid [starttext]
Html Forms Tutorial video
[endtext]

Extensible Markup Language (XML) - Tutorial

http://www.youtube.com/watch?v=-oLlHA0Uy-sendofvid [starttext] Lecture Series on Internet Technologies by Prof.I.Sengupta, Department of Computer Science & Engineering ,IIT Kharagpur. For more details on NPTEL visit http://nptel.iitm.ac.in
[endtext]

TLGen - Persistence Layer Generator (EJB 3) - Automatic persistence layer code generator UML domain model as input

http://www.youtube.com/watch?v=2Qat6eYo4F8endofvid [starttext] TLGen is a persistence layer code generator which generates the persistence layer code and test classes automatically using a domain or database model as input.TLGen generates Java code in latest J2EE standard (EJB 3) and combines the advantages of MDA and CASE.
TLGen - Persistence Layer Generator (EJB 3) - Automatic persistence layer code generator UML domain model as input
[endtext]

J2EE Hibernate Tutorial SETUP in Eclipse - [ Part 1 ]

http://www.youtube.com/watch?v=GINvxAaXDbYendofvid [starttext]
Easy to follow Hibernate Tutorial using Java JPA Annotations.
Eclipse Hibernate Database setup instructions
[endtext]

J2EE Hibernate Tutorial - first class [ Part 4 ]

http://www.youtube.com/watch?v=fURXIkP7pWAendofvid [starttext]
Java Hibernate Tutorial Part 4 - Our First Class!
[endtext]

J2EE Hibernate Tutorial - customise cfg.xml [ Part 3 ]

http://www.youtube.com/watch?v=WKWnbhc4_6gendofvid [starttext]
Java Hibernate Tutorial Part 3 - customise cfg.xml
[endtext]

J2EE Hibernate Tutorial SETUP in Eclipse - [ Part 2 ]

http://www.youtube.com/watch?v=4oaVSnfiTE0endofvid [starttext]
Easy to follow Hibernate Tutorial using Java JPA Annotations.
Eclipse Hibernate Database setup instructions.
[endtext]

J2EE Hibernate Tutorial - Insert record (object) [ Part 7 ]

http://www.youtube.com/watch?v=6i12sNUO0aYendofvid [starttext]
Java Hibernate Tutorial Part 7 - Insert record (object)
[endtext]

J2EE Hibernate Tutorial - Schema Change [ Part 6 ]

http://www.youtube.com/watch?v=5YWYU2h8uCoendofvid [starttext]
Java Hibernate Tutorial Part 6 - Schema Change
[endtext]

J2EE Hibernate Tutorial - Create table from Class [ Part 5 ]

http://www.youtube.com/watch?v=ReAZmA83Mygendofvid [starttext]
Java Hibernate Tutorial Part 5 - Create table from Class
[endtext]

J2EE Hibernate Tutorial - Auto Generate Primary key [ Part 10 ]

http://www.youtube.com/watch?v=0wkyQHxvkLkendofvid [starttext]
Java Hibernate Tutorial Part 10 - Auto Generate Primary key
[endtext]

J2EE Hibernate Tutorial - more annotations [ Part 9 ]

http://www.youtube.com/watch?v=EIjiNJQ5pBUendofvid [starttext]
Java Hibernate Tutorial Part 9 - more annotations
[endtext]

J2EE Hibernate Tutorial - Chapter 1 Review 1 [ Part 8 ]

http://www.youtube.com/watch?v=KfsdyXEwan4endofvid [starttext]
Java Hibernate Tutorial Part 8 Chapter 1 Review 1
[endtext]

J2EE Hibernate Tutorial - Compound Primary Key [ Part 13 ]

http://www.youtube.com/watch?v=YDxTQUVkujsendofvid [starttext]
Java Hibernate Tutorial Part 13 - Compound Primary Key
[endtext]

J2EE Hibernate Tutorial - Two classes to One table [ Part 12 ]

http://www.youtube.com/watch?v=N9dJPpsIoCsendofvid [starttext]
Java Hibernate Tutorial Part 12 - Two classes to One table
[endtext]

J2EE Hibernate Tutorial - One class to Two tables [ Part 11 ]

http://www.youtube.com/watch?v=F3Bct8tkfcsendofvid [starttext]
Java Hibernate Tutorial Part 11 - One class to Two tables
[endtext]

J2EE Hibernate Tutorial -One to One Bi-directional [ Part 16 ]

http://www.youtube.com/watch?v=TanXgmG6ZuUendofvid [starttext]
Java Hibernate Tutorial Part 16 - One to One Bi-directional
[endtext]

J2EE Hibernate Tutorial - One to One Mappingy [ Part 15 ]

http://www.youtube.com/watch?v=019nGJGd9JMendofvid [starttext]
Java Hibernate Tutorial Part 15 - One to One Mapping
[endtext]

J2EE Hibernate Tutorial - Inheritance Mapping [ Part 14 ]

http://www.youtube.com/watch?v=oJ4gpmPG-MUendofvid [starttext]
Java Hibernate Tutorial Part 14 - Inheritance Mapping
[endtext]

J2EE Hibernate Tutorial - One to Many Mapping [ Part 17 ]

http://www.youtube.com/watch?v=PIg6F9TayOkendofvid [starttext]
Java Hibernate Tutorial Part 17 - One to Many Mapping
[endtext]

J2EE Hibernate Tutorial - Many to Many Mapping [ Part 18 ]

http://www.youtube.com/watch?v=zbH59X281f4endofvid [starttext]
Java Hibernate Tutorial Part 18 - Many to Many Mapping
[endtext]

[ part 1 ] - Struts 2 Framework Tutorial - Session 1 Introduction to Struts 2 Framework

http://www.youtube.com/watch?v=y91YILd8DGoendofvid [starttext]
[endtext]

using GeoServer - J2EE Tutorial

http://www.youtube.com/watch?v=0LCc4s8NOBMendofvid [starttext]
The GeoServer project is a full transactional Java (J2EE) implementation of the OpenGIS Consortium's Web Feature Server specification and Web Coverage Server specification, with an integrated Web Map Server.
[endtext]

J2EE Component Assembly Model - Packaging and Deploying J2EE Applications [Part1]

http://www.youtube.com/watch?v=oyMbniKhpBIendofvid [starttext]
[endtext]

Packing EJBs in J2EE application -Packaging and Deploying J2EE Application [Part2]

http://www.youtube.com/watch?v=9t1K65nowrQendofvid [starttext]
[endtext]

Packing Web Components in J2EE application - Packaging and Deploying J2EE Application [Part 3]

http://www.youtube.com/watch?v=ruwflHqKKMMendofvid [starttext]
[endtext]

Deploying a J2EE application on J2EE server - Packaging and Deploying J2EE Application [Part 4]

http://www.youtube.com/watch?v=Dn3K5FG4ntIendofvid [starttext]
[endtext]

Packaging and Deploying J2EE Applications [Part 5]

http://www.youtube.com/watch?v=Kwk8HNxKzoQendofvid [starttext]
[endtext]

J2EE INTRO - Intoduction to J2EE

http://www.youtube.com/watch?v=nFIS1A_qgxYendofvid [starttext]
Intoduction to J2EE
[endtext]

Checking the 'sizeof' doubts

I noticed sometime back that one of my programs behaved unexpectedly in certain scenarios which was traced to an incorrect use of sizeof. As a result, I made a small program to make sure that my understanding of sizeof is correct. Here is the program:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

int
main()
{

char
a[]="";
cout<<"Size of a[] = "<<sizeof(a)<<endl;

char
b[]=" ";
cout<<"Size of b[ ] = "<<sizeof(b)<<endl;

char
c[10];
cout<<"Size of c[10] = "<<sizeof(c)<<endl;

char
* d;
cout<<"Size of d = "<<sizeof(d)<<endl;

char
* e = c;
cout<<"Size of e = "<<sizeof(e)<<endl;

char
f[] = "123456";
cout<<"Size of f = "<<sizeof(f)<<endl;

char
g[] = {'1','2','3','4','5','6'};
cout<<"Size of g = "<<sizeof(g)<<endl;

return
0;
}



The output is as follows:
A lot of interviewers love the sizeof questions.

Have you come across any interesting sizeof problems? Please feel free to add them in comments.

C++ example of Linked List implementation

It is debatable if Linked lists are needed in C++, especially because STL can do a very good job for the same functionality for which Linked Lists are required. Nevertheless sometimes it maybe convenient to know linked lists.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <string>
#include <istream>
#include <limits>

using namespace
std;

class
Node
{

public
:
Node() : next(NULL) {}; //default constructor
Node(const string& s1) : info(s1), next(NULL) {};
string info;
Node* next;
};


Node* getNewNode()
{

//Lets flush the input buffer first
istream& in(cin);
in.ignore( numeric_limits<std::streamsize>::max(), '\n' );

//Read the String from console
cout<<"\tEnter a string: ";
istreambuf_iterator<char> eos; // end-of-range iterator
istreambuf_iterator<char> iit (cin.rdbuf()); // stdin iterator
string s2;
while
(iit!=eos && *iit!='\n') s2+=*iit++;

//Call the constructor with the input string
Node* newNode = new Node(s2);

return
newNode;
}


int
main()
{

Node* head = NULL;
bool
contFlag = true;
char
c;

while
(contFlag)
{

cout<<"\n\n";
cout<<"0. Quit"<<endl;
cout<<"1. Traverse and Print"<<endl;
cout<<"2. Insert node at the start"<<endl;
cout<<"3. Insert node at the end"<<endl;
cout<<"4. Insert after specified number of nodes"<<endl;
cout<<"5. Remove node from the start"<<endl;
cout<<"6. Remove node from the end"<<endl;
cout<<"7. Remove node from a specified number"<<endl;

cout<<"\nEnter your choice: ";
cin>>c;

switch
(c)
{

case
'0':
contFlag = false;
break
;
case
'1':
{

Node* tempNode = head;
int
count = 0;
while
(tempNode)
{

cout<<"\tItem "<<count<<" = "<<tempNode->info<<endl;
tempNode = tempNode->next;
count++;
}
}

break
;
case
'2':
{

Node* newNode = getNewNode();
newNode->next = head;
head = newNode;
}

break
;
case
'3':
{

Node* newNode = getNewNode();

Node* endNode = head;
if
(endNode)
{

while
(endNode->next)
endNode = endNode->next;
endNode->next = newNode;
}

else
//In case its the first element
{
head = newNode;
}
}

break
;
case
'4':
{

Node* tempNode = head;
if
(tempNode)
{

unsigned
num;
cout<<"\tInsert after how many nodes? : ";
cin>>num;
Node* newNode = getNewNode();

unsigned
count = 1;
//If less elements are present then insert at the end
while(tempNode->next && (count < num))
{

tempNode = tempNode->next;
count++;
}

newNode->next = tempNode->next;
tempNode->next = newNode;
}

else

{

cout<<"\tNo nodes present yet"<<endl;
}
}

break
;
case
'5':
{

Node* firstNode = head;
if
(firstNode)
{

head = firstNode->next;
delete
firstNode;
}
}

break
;
case
'6':
{

Node* endNode = head;
if
(endNode) //Atleast one element
{
Node* endEndNode = endNode->next;
if
(endEndNode) //Atleast 2 elements
{
while
(endEndNode->next)
{

endNode = endEndNode;
endEndNode = endEndNode->next;
}

endNode->next = NULL;
delete
endEndNode;
}

else
//Only one element
{
head = NULL;
delete
endNode;
}
}
}

break
;
case
'7':
{

Node* tempNode = head;
if
(tempNode)
{

unsigned
num;
cout<<"\tRemove after how many nodes? : ";
cin>>num;

unsigned
count = 1;
//If less elements are present then return
while(tempNode->next && (count < num))
{

tempNode = tempNode->next;
count++;
}

if
(count == num && tempNode->next && tempNode->next->next)
{

Node* nodeToRemove = tempNode->next;
tempNode->next = nodeToRemove->next;
delete
nodeToRemove;
}
}

else

{

cout<<"\tNo nodes present yet"<<endl;
}
}

break
;
default
:
cout<<"Incorrect selection."<<endl;
}
}


return
0;
}



The output is as follows:
You can try more combinations and if you have ideas for improvements please add in the comments section.

Few things to note:
1. This program is for reference and I have not accounted for memory leaks after pressing '0'.
2. For simplicity, getNewNode() is shown as a seperate function. In a real program, it may be better as a method of class Node
3. General Linked Lists use Struct's but in C++, Structs and Classes are nearly the same so its better to use Class.
4. In C++ there is rarely a need to use Linked Lists. Its better to use the STL which already have vectors, stacks, lists, etc.
5. There should be check for memory allocation and try catch blocks that have been ignored for simplicity

Check out this stream