Assignment - 1 OF Data Structures: Submitted By:-Kashish Kansal M.Tech (Cse) - Part Time ROLL NO: - 6558
Assignment - 1 OF Data Structures: Submitted By:-Kashish Kansal M.Tech (Cse) - Part Time ROLL NO: - 6558
Q 3. Write an algorithm and program to implement queue using two stacks. Algorithm :enQueue(q, x) 1) Push x to stack1 (assuming size of stacks is unlimited). deQueue(q) 1) If both stacks are empty then error. 2) If stack2 is empty While stack1 is not empty, push everything from satck1 to stack2. 3) Pop the element from stack2 and return it. Program #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> struct sNode { int data; struct sNode *next; };
void push(struct sNode** top_ref, int new_data); int pop(struct sNode** top_ref); struct queue { struct sNode *stack1; struct sNode *stack2; }; void enQueue(struct queue *q, int x) { push(&q->stack1, x); } int deQueue(struct queue *q) { int x; if(q->stack1 == NULL && q->stack2 == NULL) {
cout<<"queue is empty"; getchar(); exit(0); } if(q->stack2 == NULL) { while(q->stack1 != NULL) { x = pop(&q->stack1); push(&q->stack2, x); } } x = pop(&q->stack2); return x; } void push(struct sNode** top_ref, int new_data) { struct sNode* new_node =(struct sNode*)malloc(sizeof(struct sNode)); if(new_node == NULL) { cout<<"stack overflow"; getchar(); exit(0); } new_node->data = new_data; new_node->next = (*top_ref); (*top_ref) = new_node; } int pop(struct sNode** top_ref) { int res; struct sNode *top; if(*top_ref == NULL) { cout<<"stack overflow"; getchar(); exit(0); } else { top = *top_ref;
res = top->data; *top_ref = top->next; free(top); } return res; } int main() { clrscr(); struct queue *q = (struct queue*)malloc(sizeof(struct queue)); q->stack1 = NULL; q->stack2 = NULL; enQueue(q, 1); enQueue(q, 2); enQueue(q, 3); cout<<deQueue(q)<<endl; cout<<deQueue(q)<<endl; cout<<deQueue(q)<<endl; getchar(); return 1; } Q 4. Write a program to implement stack and queue using link list. STACK USING LINKLIST #include<iostream.h> #include<conio.h> #include<string.h> #include<process.h> struct node { int data; node *link; }; node *push(node *top,int val) { node *temp; temp=new node;
temp->data=val; temp->link=NULL; if(top==NULL) { top=temp; } else { temp->link=top; top=temp; } return top; } node *pop(node *top,int &val) { node *temp; clrscr(); if(top==NULL) { cout<<"Stack Empty"; val=-1; } else { temp=top; top=top->link; val=temp->data; temp->link=NULL; delete temp; } return top; } void show_stack(node *top) { node *temp; temp=top; cout<<"the value are \n"; while(temp!=NULL)
{ cout<<"\n"<<temp->data; temp=temp->link; } }
void main() { node *top; int val,choice; char opt='Y'; top=NULL; clrscr(); do { cout<< "1 for push 2. pop 3 for traverse the stacik 4. for exit"; cin>>choice; switch(choice) { case 1: do { cout<<"enter item"; cin>>val; top=push(top,val); cout<<"want to add more"; cin>>opt; }while(opt=='Y'); break;
case 2: opt='Y'; do { top=pop(top,val); if(val!=-1) cout<<"value deleted is"<<val; cout<<"do you want to delete more";
} }while(choice!=4); } QUEUE USING LINK LIST #include<iostream.h> #include<conio.h> #include<process.h> struct node { char data; node *link; }; node *add_Q(node *rear, char val); node *del_Q(node *front, char &val); void show_Q(node *front); void main() { node *front, *rear; char val; int choice; char opt='Y'; front=rear=NULL; do { cout<<"\n\t\t Main Menu"; cout<<"\n\t1. Addition of Queue"; cout<<"\n\t2. Deletion from Queue";
cout<<"\n\t3. Traverse of Queue"; cout<<"\n\t4. Exit from Menu"; cout<<"\n\n Enter your choice from above"; cin>>choice; switch(choice) {case 1: do { cout<<"enter the value to added in the queue"; cin>>val; rear=add_Q(rear,val); if(front==NULL) front=rear; cout<<"\n Do you want to add more element <Y/N>?"; cin>>opt; } while(opt=='Y'); break; case 2: opt= 'Y'; do { front=del_Q(front,val); if(val!=-1) cout<<"value deleted from queue is"<<val; cout<<"\n Do you want to delete more element<Y/N?"; cin>>opt; } while(opt=='Y'); break; case 3: show_Q(front); break; case 4: exit(0); } } while(choice!=4); } node *add_Q(node *rear, char val) {
node *temp; temp=new node; temp->data=val; temp->link=NULL; rear->link=temp; rear=temp; return(rear); } node *del_Q(node *front, char &val) { node *temp; clrscr(); if(front==NULL) { cout<<"Queue empty"; val=-1; } else { temp=front; front=front->link; val=temp->data; temp->link=NULL; delete temp; } return(front); } void show_Q(node *front) { node *temp; temp=front; clrscr(); cout<<"the queue values are"; while(temp!=NULL) { cout<<"\n"<<temp->data; temp=temp->link; }
} q1 Consider the following three algorithms for determining whether anyone in the room has the same birthday as you. Algorithm 1: You say your birthday, and ask whether anyone in the room has the same birthday. If anyone does have the same birthday, they answer yes. Algorithm 2: You tell the first person your birthday, and ask if they have the same birthday; if they say no, you tell the second person your birthday and ask whether they have the same birthday; etc, for each person in the room. Algorithm 3: You only ask questions to person 1, who only asks questions to person 2, who only asks questions to person 3, etc. You tell person 1 your birthday, and ask if they have the same birthday; if they say no, you ask them to find out about person 2. Person 1 asks person 2 and tells you the answer. If it is no, you ask person 1 to find out about person 3. Person 1 asks person 2 to find out about person 3, etc. Question 1: For each algorithm, what is the factor that can affect the number of questions asked? Solution 1: The problem size is the number of people in the room. Question 2: In the worst case, how many questions will be asked for each of the three algorithms? Solution 2: Assume there are N people in the room. In algorithm 1 you always ask 1 question. In algorithm 2, the worst case is if no one has your birthday. Here you have to ask every person to figure this out. This is N questions. In algorithm 3, the worst case is the same as algorithm 2. The number of questions is 1 + 2 + 3 + ... + N-1 + N. We showed before that this sum is N(N+1)/2. Question 3: For each algorithm, say whether it is constant, linear, or quadratic in the problem size in the worst case. Solution 3: Given the number of questions you can see that algorithm 1 is constant time, algorithm 2 is linear time, and algorithm 3 is quadratic time in the problem size. 2. Write a program to implement queue of queue #include<iostream.h> #include<conio.h> #include<process.h> #define SIZE 20 int push(int *rear,int s[],int item) { if(*rear==SIZE-1)
{ cout<<"queue overflow"<<endl; } else s[++(*rear)]=item; return s[*rear]; } void Qpush(int *rear,int D[],int item) { if(*rear==SIZE-1) { cout<<"queue overflow"<<endl; } else D[++(*rear)]=s[*rear]; } int pop(int *front,int D[]) { if(*front==-1) { return 0; } else { //return s[(*front)]; return D[(*front)++]; } } void display(int front,int rear,int D[]) { for(int i=front;i<=rear;i++) { cout<<"Element of Queue is: "<<D[i]<<endl; } } void main() { clrscr();
int ch,front,rear,s[10],item,D[100]; front=0; rear=-1; for(;;) { cout<<"Enter 1 For Push 2 For Pop 3 For Display 4 For Exit"<<endl ; cin>>ch; switch(ch) { case 1: cout<<"enter item to be push into the queue"<<endl; cin>>item; Qpush(&rear,s,item); break; case 2: int c; c=pop(&front,D); if(c==0) { cout<<"Queue is empty"<<endl; } else { cout<<"item pop from queue is: "<<c<<endl; } break; case 3: display(front,rear,D); break; case 4: exit(0); default: cout<<"invalid input"<<endl; } } getch(); }