W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Snehil Khanor's Binary Log
Showing posts with label traverse. Show all posts
Showing posts with label traverse. Show all posts

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]); 

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log



eXTReMe Tracker