Question

Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.  

Task5: Concatenate Stacks Create the following function: Stack* concat_stack(Stack* si, Stack* s2); The function creates and

Stack 2: Capacity = 2, Stack Size = 0 <emtpy_stack> Stacks After Concatenation: Stack 3: Capacity 5, Stack Size = 0 <emtpy_st

Stack 1: Capacity 3, Stack Size = 3 [5](1000010004, 14) [4](1000010003, 13) [3](1000010002, 12) Stack 2: Capacity = 2, Stack

**SKELTON CODE**

#include

#include

#include

Stack* concat_stack(Stack *s1, Stack *s2) {

//your code here

return NULL;

}

**STACK CLASS FOR YOU TO REFERENCE**

#include

#include

#include

#include

Stack* create_stack(int stack_capacity) {

Stack *s = (Stack*) malloc(sizeof(Stack));

if (stack_capacity < 1) {

fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n");

s->capacity = 10;

}

s->capacity = stack_capacity;

s->array = (Data*) malloc(sizeof(Data) * (s->capacity));

s->top = -1;

return s;

}

void destroy_stack(Stack **s) {

assert(s!=NULL);

Data *data = NULL;

while (!is_empty_stack(*s)) {

data = pop_stack(*s);

destroy_data(&data);

}

free((*s)->array);

(*s)->array = NULL;

(*s)->capacity = 0;

(*s)->top = 0;

free(*s);

*s = NULL;

return;

}

Data* pop_stack(Stack *s) {

assert(s!=NULL);

Data *d = NULL;

if (is_empty_stack(s)) {

printf("Error(pop_stack): stack is empty\n");

return d;

}

d = copy_data(&s->array[s->top]);

Data *temp = &s->array[s->top];

destroy_data(&temp);

s->top--;

return d;

}

int push_stack(Stack *s, Data *d) {

assert(s!=NULL && d!=NULL);

if (is_full_stack(s)) {

printf("Error(push_stack): stack is full\n");

return False;

}

s->top++;

s->array[s->top] = *copy_data(d);

return True;

}

void print_stack(Stack *s) {

assert(s!=NULL);

printf("Stack Size = %d\n", len_stack(s));

if (is_empty_stack(s))

printf("\n");

for (int i = s->top; i >= 0; i--) {

print_data(&s->array[i]);

printf("\n");

}

return;

}

Data* peek_stack(Stack *s) {

assert(s!=NULL);

if (is_empty_stack(s)) {

printf("Error(peek_stack): stack is empty\n");

return NULL;

}

return copy_data(&(s->array[s->top]));

}

int len_stack(Stack *s) {

assert(s!=NULL);

return s->top + 1;

}

int is_empty_stack(Stack *s) {

assert(s!=NULL);

return (s->top == -1);

}

int is_full_stack(Stack *s) {

assert(s!=NULL);

return (s->top == s->capacity - 1);

}

Stack* copy_stack(Stack *s) {

assert(s!=NULL);

Stack *s2 = create_stack(s->capacity);

int i, size = len_stack(s);

Data *array = (Data*) malloc(sizeof(Data) * size);

for (i = 0; i < size; i++)

array[i] = *pop_stack(s);

for (i = size - 1; i >= 0; i--) {

push_stack(s, &array[i]);

push_stack(s2, &array[i]);

}

free(array);

return s2;

}

**DATA CLASS FOR YOU TO REFERENCE**

#include "data.h"

void print_data(Data *d) {

print_process(d);

return;

}

void destroy_data(Data **d) {

destroy_process(d);

return;

}

Data* copy_data(Data *d) {

return copy_process(d);

}

**PROCESS CLASS FOR YOUR REFERENCE**

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "process.h"

Process* create_process(long pid, int time, int arrival) {
   Process *p = (Process*) malloc(sizeof(Process));

   //set time
   if (time <= 0) {
       fprintf(stderr, "Error(create_process): invalid time - set to 0\n");
       p->time = 0;
   } else
       p->time = time;

   if (arrival < 0) {
       fprintf(stderr,
               "Error(create_process): invalid arrival time - set to 0\n");
       p->arrival = 0;
   } else
       p->arrival = arrival;

   if (pid <= 0) {
       fprintf(stderr, "Error(create_process): invalid pid - set to random\n");
       p->PID = get_UPID();
   } else
       p->PID = pid;

   return p;
}
void destroy_process(Process **p) {
   assert(*p != NULL);
   (*p)->PID = 0;
   (*p)->time = 0;
   (*p)->arrival = 0;
   //free(*p);
   *p = NULL;
   return;
}

void print_process(Process *p) {
   assert(p != NULL);
   char info[30];
   strcpy(info, "");
   get_process_info(p, info);
   printf("%s", info);
   return;
}

Process* copy_process(Process *p1) {
   assert(p1!=NULL);
   Process *p2 = (Process*) malloc(sizeof(Process));
   p2->PID = p1->PID;
   p2->time = p1->time;
   p2->arrival = p1->arrival;
   return p2;
}

void get_process_info(Process *p, char *info) {
   assert(p!=NULL);
   char process[30];
   sprintf(process, "[%u](%lu,%u)", p->arrival, p->PID, p->time);
   strcpy(info, process);
   return;
}

int is_equal_process(Process *p1, Process *p2) {
   assert(p1 != NULL && p2 != NULL);
   if (p1->PID != p2->PID)
       return False;
   if (p1->time != p2->time)
       return False;
   return True;

}
unsigned long get_UPID() {
   static int counter = 1000;

   //GET higher 5 digits
   unsigned long higher5 = rand() % (39999 - 10000 + 1) + 10000;
   higher5 = higher5 * 100000;

   int lower5 = counter;
   counter++;

   return higher5 + lower5;
}

** STACK CLASS TYPE DEFINITION**

#ifndef STACK_ARRAY_H_
#define STACK_ARRAY_H_

#define True 1
#define False 0

#include "data.h"

typedef struct {
   Data *array; //stack array
   int top; //index for top of the stack
   int capacity; //maximum capcaity of stack
} Stack;

Stack* create_stack(int stack_capacity);
void destroy_stack(Stack **s);
Data* pop_stack(Stack *s);
int push_stack(Stack *s, Data *d);
void print_stack(Stack *s);
Data* peek_stack(Stack *s);
int len_stack(Stack *s);
int is_empty_stack(Stack *s);
int is_full_stack(Stack *s);
Stack* copy_stack(Stack *s);

#endif /* STACK_ARRAY_H_ */

** DATA CLASS TYPE DEFINITION**

#ifndef DATA_H_
#define DATA_H_

#include "process.h"

typedef Process Data;

void print_data(Data *d);
void destroy_data(Data **d);
Data* copy_data(Data *d);

#endif /* DATA_H_ */

** PROCESS CLASS TYPE DEFINITION**

#ifndef PROCESS_H_
#define PROCESS_H_

#define True 1
#define False 0

typedef struct {
   unsigned long PID; //process id (10-digit)
   unsigned int time; //requested processing time
   unsigned int arrival; //arrival time
} Process;

Process* create_process(long p_pid, int p_time, int arrival_time);
void destroy_process(Process **p);
void print_process(Process *p);
Process* copy_process(Process *p);
void get_process_info(Process *p, char *info);
int is_equal_process(Process *p1, Process *p2);
unsigned long get_UPID();

#endif /* PROCESS_H_ */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Stack* concat_stack(Stack *s1, Stack *s2) {

Stack *s3;

s3->capacity = s1->capacity + s2->capacity;

while (!is_empty_stack(*s1)) {

*s3.push_stack(*s1.top());

*s1.pop_stack();}

while (!is_empty_stack(*s2)) {

*s3.push_stack(*s2.top());

*s2.pop_stack();}

return *s3;

}

Add a comment
Know the answer?
Add Answer to:
Can you help with this C programming question. I have provided the skeleton code below along...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int #...

    Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int # define TYPE_SIZE sizeof(int) # endif # ifndef LT # define LT(A, B) ((A) < (B)) # endif # ifndef EQ # define EQ(A, B) ((A) == (B)) # endif typedef struct DynArr DynArr; /* Dynamic Array Functions */ void initDynArr(DynArr *v, int capacity); DynArr *newDynArr(int cap); void freeDynArr(DynArr *v); void deleteDynArr(DynArr *v); int sizeDynArr(DynArr *v); void addDynArr(DynArr *v, TYPE val); TYPE getDynArr(DynArr *v, int...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • This assignment is comprised of 3 parts: ​All files needed are located at the end of...

    This assignment is comprised of 3 parts: ​All files needed are located at the end of the directions. Part 1: Implementation of Dynamic Array, Stack, and Bag First, complete the Worksheets 14 (Dynamic Array), 15 (Dynamic Array Amortized Execution Time Analysis), 16 (Dynamic Array Stack), and 21 (Dynamic Array Bag). These worksheets will get you started on the implementations, but you will NOT turn them in. ​Do Not Worry about these, they are completed. Next, complete the dynamic array and...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT