0% found this document useful (0 votes)
170 views

Program of Stack Using Array

The document contains code for implementing different data structures - stack, queue and file handling in C programming language. It includes code for implementing: 1) Stack using array and linked list with push, pop and display functions. 2) Queue using linked list and array with insert, delete and display functions. 3) Opening and reading a file to delete consecutive spaces.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Program of Stack Using Array

The document contains code for implementing different data structures - stack, queue and file handling in C programming language. It includes code for implementing: 1) Stack using array and linked list with push, pop and display functions. 2) Queue using linked list and array with insert, delete and display functions. 3) Opening and reading a file to delete consecutive spaces.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

/* Program of stack using array*/ #include #define MAX 5 int top = -1; int stack_arr[MAX]; main() { int choice;

while(1) { printf("1.Push\n"); printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ push() { int pushed_item; if(top == (MAX-1)) printf("Stack Overflow\n"); else { printf("Enter the item to be pushed in stack : "); scanf("%d",&pushed_item); top=top+1;

stack_arr[top] = pushed_item; } }/*End of push()*/ pop() { if(top == -1) printf("Stack Underflow\n"); else { printf("Popped element is : %d\n",stack_arr[top]); top=top-1; } }/*End of pop()*/ display() { int i; if(top == -1) printf("Stack is empty\n"); else { printf("Stack elements :\n"); for(i = top; i >=0; i--) printf("%d\n", stack_arr[i] ); } }/*End of display()*/

/* Program of stack using linked list*/ # include # include struct node { int info; struct node *link; } *top=NULL; main() { int choice; while(1) { printf("1.Push\n");

printf("2.Pop\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : ") ; scanf("%d", &choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ }/*End of main() */ push() { struct node *tmp; int pushed_item; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the new value to be pushed on the stack : "); scanf("%d",&pushed_item); tmp->info=pushed_item; tmp->link=top; top=tmp; }/*End of push()*/ pop() { struct node *tmp; if(top == NULL) printf("Stack is empty\n"); else { tmp=top; printf("Popped item is %d\n",tmp->info); top=top->link; free(tmp);

} }/*End of pop()*/ display() { struct node *ptr; ptr=top; if(top==NULL) printf("Stack is empty\n"); else { printf("Stack elements :\n"); while(ptr!= NULL) { printf("%d\n",ptr->info); ptr = ptr->link; }/*End of while */ }/*End of else*/ }/*End of display()*/

/* Program of queue using linked list*/ # include # include struct node { int info; struct node *link; }*front=NULL,*rear=NULL; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d", &choice); switch(choice)

{ case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { struct node *tmp; int added_item; tmp = (struct node *)malloc(sizeof(struct node)); printf("Input the element for adding in queue : "); scanf("%d",&added_item); tmp->info = added_item; tmp->link=NULL; if(front==NULL) /*If Queue is empty*/ front=tmp; else rear->link=tmp; rear=tmp; }/*End of insert()*/ del() { struct node *tmp; if(front == NULL) printf("Queue Underflow\n"); else { tmp=front; printf("Deleted element is %d\n",tmp->info); front=front->link; free(tmp); } }/*End of del()*/

display() { struct node *ptr; ptr = front; if(front == NULL) printf("Queue is empty\n"); else { printf("Queue elements :\n"); while(ptr != NULL) { printf("%d ",ptr->info); ptr = ptr->link; } printf("\n"); }/*End of else*/ }/*End of display()*/

/*Program of queue using array*/ # include # define MAX 5 int queue_arr[MAX]; int rear = -1; int front = -1; main() { int choice; while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) {

case 1 : insert(); break; case 2 : del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ insert() { int added_item; if (rear==MAX-1) printf("Queue Overflow\n"); else { if (front==-1) /*If queue is initially empty */ front=0; printf("Input the element for adding in queue : "); scanf("%d", &added_item); rear=rear+1; queue_arr[rear] = added_item ; } }/*End of insert()*/ del() { if (front == -1 || front > rear) { printf("Queue Underflow\n"); return ; } else { printf("Element deleted from queue is : %d\n", queue_arr[front]); front=front+1; } }/*End of del() */

display() { int i; if (front == -1) printf("Queue is empty\n"); else { printf("Queue is :\n"); for(i=front;i<= rear;i++) printf("%d ",queue_arr[i]); printf("\n"); } }/*End of display() */

#include<stdio.h> #include<fileio.h> Main() { FILE *fp; Char ch; fp=fopen(sample.txt,a); ch=fgetc(); while(ch!=EOF) { If(ch== ) { Ch=fgetc(); If(ch== ) { Delete(ch); }

} } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy