Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Code by Kowsik Nandagopan D. CSE S3 */
- /* Use txt file to input IN to insert, DEL to pop element, DIS to display whole queue*/
- /* Header file declaration */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Funtion Declaration */
- void inv_stk();
- void insert(int);
- void inv_stk_A();
- void display();
- /* Node structure to implement the STACK data structure */
- struct Node{
- int data;
- struct Node* next;
- };
- /* Two Global STACK data structures A and B */
- struct Node* STACK_A_TOP = NULL;
- struct Node* STACK_B_TOP = NULL;
- /* Invert funtion to invert the STACK A to STACK B to get the last elemennt in the STACK A */
- void inv_stk(){
- struct Node* temp_A = STACK_A_TOP;
- STACK_B_TOP = NULL;
- struct Node* temp_B = STACK_B_TOP;
- while (temp_A != NULL){
- struct Node* node = (struct Node*) malloc(sizeof(struct Node));
- node->data = temp_A->data;
- if (STACK_B_TOP == NULL){
- node->next = NULL;
- STACK_B_TOP = node;
- } else {
- node->next = STACK_B_TOP;
- STACK_B_TOP = node;
- }
- temp_A = temp_A->next;
- }
- }
- /* Inset operation on the STACK A to push the newely added elements to the QUEUE. Inserting to the TOP of STACK A */
- void insert(int x){
- struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
- temp->data = x;
- if (STACK_A_TOP == NULL){
- temp->next = NULL;
- STACK_A_TOP = temp;
- } else {
- temp->next = STACK_A_TOP;
- STACK_A_TOP = temp;
- }
- inv_stk();
- }
- /* Inverting back the STACK B back to STACK A as the first element in the STACK B is deleted that is the last element of STACK A */
- void inv_stk_A(){
- struct Node* temp = STACK_B_TOP;
- while (temp != NULL){
- insert(temp->data);
- temp = temp->next;
- }
- }
- /* To display the QUEUE that is the STACK B after inverting the STACK A */
- void display(){
- STACK_A_TOP = NULL;
- struct Node* temp = STACK_B_TOP;
- while (temp != NULL){
- printf("%d ", temp->data);
- temp = temp->next;
- }
- }
- void pop_element(){
- int x;
- if (STACK_B_TOP == NULL) printf("Queue is empty");
- else {
- x = STACK_B_TOP->data;
- struct Node* temp;
- temp = STACK_B_TOP;
- STACK_B_TOP = STACK_B_TOP->next;
- free(temp);
- // Invert the stack B back to A
- inv_stk_A();
- printf("%d", x);
- }
- }
- void main(int argc, char* argv[]){
- char c[1000];
- int i=0, d;
- /* Reading input form a text file based on the command line argument */
- FILE* f= fopen(argv[1], "r");
- if (!f) printf("NO file named %s", argv[1]);
- else {
- while (fscanf(f, "%s %d\n", c, &d) >= 1)
- /* Insert operation */
- if (strcmp(c, "IN") == 0){
- // printf("IN %d", d);
- insert(d);
- // printf("\n");
- /*Pop Operation*/
- } else if (strcmp(c, "DEL") == 0){
- // printf("POP");
- pop_element();
- printf("\n");
- /* Display whole QUEUE*/
- } else if (strcmp(c, "DIS") == 0){
- // printf("DIS\n");
- display();
- printf("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement