Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in C Create a Stack (ADT). Items that are pushed and popped are strings.

ID: 3725708 • Letter: T

Question

This is in C

Create a Stack (ADT). Items that are pushed and popped are strings. Implement the Stack using a linked list.

Stack.h

The type stack is a struct with 2 data members. (an integer – size of the stack, a pointer to the top item on the stack)

Function declarations for the stack should look like the following.

void initialize(stack* s);

void push(stack* s,char* str);

void pop(stack* s, char* str);

void peek(stack* s, char* str);

bool is_empty(stack* s);

int get_length(stack* s);

void display(stack* s);

Create a main.c that reads in a file “myfile.txt”

File contains something like the following:

Dempsey Hello My - - Name – Duce

Words get pushed onto the stack. - pop an item from the stack.

After reading the file – display the stack as follows:

Duce Dempsey

I will run your main.c with my own txt file. I will run your stack functions with my own main.c file.

Comment your code.

Files needed:

main.c

stack.h

myfile.txt

Explanation / Answer

//stack.h

#pragma once

#define _CRT_SECURE_NO_DEPRECATE

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

typedef struct stck

{

int size;

char *top;

struct stck *next;

}stack;

stack *head;

void initialize(stack* s);

void push(stack* s, char* str);

void pop(stack* s, char* str);

void peek(stack* s, char* str);

bool is_empty(stack* s);

int get_length(stack* s);

void display(stack* s);

void initialize(stack* s)

{

s->size = 0;

s->top = NULL;

s->next = NULL;

}

void push(stack* s, char* str)

{

stack *ptr = s, *newptr;

if (is_empty(s))

{

//s = (stack*)malloc(sizeof(stack));

s->size = strlen(str) + 1;

s->top = (char*)malloc(s->size * sizeof(char));

strcpy(s->top, str);

s->next = NULL;

}

else

{

//copy new str to newly created pointer

newptr = (stack*)malloc(sizeof(stack));

newptr->size = strlen(str) + 1;

newptr->top = (char*)malloc(newptr->size * sizeof(char));

newptr->next = NULL;

strcpy(newptr->top, str);

head = newptr;

head->next = ptr;

}

}

void pop(stack* s, char* str)

{

stack *ptr = NULL;

if (s->next != NULL)

ptr = s->next;

strcpy(str, s->top);

free(s);

head = ptr;

}

void peek(stack* s, char* str)

{

strcpy(str, s->top);

}

bool is_empty(stack* s)

{

if (s->top == NULL)

return true;

else

return false;

}

int get_length(stack* s)

{

stack *ptr = s;

int count = 0;

while (ptr != NULL)

{

count++;

ptr = ptr->next;

}

return count;

}

void display(stack* s)

{

stack *ptr = s;

printf("Items on stack are: ");

while (ptr != NULL)

{

printf("%s ", ptr->top);

ptr = ptr->next;

}

}

--------------------------------------------------------------------------

//main.c

#include"Mar_8_stack.h"

//declare a pointer to stack linked list as head

extern stack *head = NULL;

int main()

{

//declare a file pointer to read a input file

FILE *fp;

char str[20];

fp = fopen("stack.txt", "r");

if (!fp)

{

printf("Not able to open the file stack.txt ");

return -1;

}

//initialize stack before pushing items

head = (stack*)malloc(sizeof(stack));

initialize(head);

while (fscanf(fp, "%s", str) != EOF)

{

//psh each items onto stack

push(head, str);

}

//now check which item is on stack using peek

peek(head, str);

printf("Item on the stack is %s ", str);

//chk number of items on stack

printf("Number of items on the stack are: %d ", get_length(head));

//display all the items on the stack

display(head);

//pop some elements and print after some elements popped

pop(head,str);

printf("Item popped is %s ", str);

pop(head,str);

printf("Item popped is %s ", str);

//display after two items popped

display(head);

}

----------------------------------------------------------------------------

/*stack.txt file

This is to test the stack program.
Hello World !!

--------------------------------------

//output

Item on the stack is !!

Number of items on the stack are: 10

Items on stack are:

!!

World

Hello

program.

stack

the

test

to

is

This

Item popped is !!

Item popped is World

Items on stack are:

Hello

program.

stack

the

test

to

is

This

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote