C Program It is only 1 rotation Uprotation of a stack Write

C++ Program: It is only 1 rotation.

Up-rotation of a stack. Write a function stack upRotate(Stack s) which returns the up-rotation of a stack (the top node is moved to the bottom) Down-rotation of a stack. Write a function stack downRotate(Stack s) which returns the down-rotation of a stack (the top node is moved to the bottom). For this assignment append all member functions of Node to the provided Node.h header file (for simplicity, implement them in the header). All the functions which use Stack should go in a main.cpp. Note that for Node, you are creating member functions, or methods. For stack, you are creating functions. The difference is important. You cannot add to the public/private data of Node or Stack; you must keep them as such. You must demonstrate all the functions you create in main.cpp! For example, if you are doing list problems, load list with some numbers, such as 2 3 5 7. Then demonstrate your rotations, reversals, etc. by printing out the list before/after the function call. Label your output appropriately so that I can grade it easily.

Solution

#define MAXLEN 100

typedef struct {

char element [MAXLEN];

int top;

}stack;

stack init()

{

stack S;

s.top= -1;

return S;

}

int isempty( stack S)

{

return(S.top== -1)

}int isFull( stack S)

{

return(S.top == MAXLEN -1);

}

char top(stack s)

{

if (isEmpty(s))

{

fprintf(stderr, \"top: Empty stack\ \");

return \'\\0\';

}

return S.element[S.top];

}

stack push(stack S char ch)

{
if (isFull(s)) {

fprintf(stderr, \"push:Full stack\ \");

return S;

}

++S.top;

S.element[S.top]=ch;

return S;

}

stack pop(stack S)

{

if (isEmpty(S)){

fprintf(stedrr,\"pop:Empty stack\ \");

return S;

}

--S.top;

returm S;

}

void print(stack S)

{

int i;

for(i=S.top; i >-0; --i) printf(\"%c\",S.element[i]);

}

Here is a possible main() function calling these routines:

int main()

stack S;

S=int(); printf(\"Current stack:\"); print(S); printf(\"with top=%c.\ \", top(S));

S=push(S,\'d\'); printf(\"Current Stack:\"); print(S);printf(\"with top =%c.\ \",top(S));

S=push(S\',f\'); printf(\"Current stack:\"); print(S); printf(\"with top =%c.\ \", top(S));

S=push(S,\'a\'); printf(\"Current stack:\"); print(S); printf(\"with top =%c.\ \" ,top(S));

S=pop(S); printf(\"Current stack :\"); printf(S); printf(\"with top=%c.\ \",top(S));

S=push(S,\'x\'); printf(\"Current stack:\"); printf(S); printf(\"with top=%c.\ \",top(S));

S=pop(S); printf(\"Current stack:\"); printf(S); printf(\"with top=%c.\ \",top(S));

S=pop(S); printf(\"Current stack:\"); printf(S); printf(\"with top =%C.\ \",top(S));

S=pop(S); printf(\"Current stack:\"); printf(S); prientf(\" with top=%c.\ \",top(S));

S=pop(S); print(\"Current stack:\"); printf(S); printf(\"with top=%c.\ \", top(S));

Here is the complete progrem. the output of the program is given beloe:

top: Empty stack

Current stack : with top =.

Current stack : d with top = d.

Current stack : fd with top = f.

Current stack : afd with top = a.

Current stack : fd with top = f.

Current stack : xfd with top = x.

Current stack : fd with top = f.

Current stack : d with top = d.

top Empty stack

Current stack : with top = .

pop : Emoty stack

top : Emoty stack

Current stack : with top = .

Animation example: Implementation of stack with satatic memory

Using dynamic linked lists

As we have seen earlier , it is no big deal to create and maintain a dynamic list of elements. the only consideration now is to decide wether the beginning or the end of the list is to be treated as the top of the stack. Deletion becomes costly, if we choose the end f the list as the top,choosing the beginning as the top makes the implementations of both push and pop easy. we stick to this convention. As usual, we maintaion a dummy node at the top (beginning) for simplifying certain operetion. The ADT functions are implemented below:

typeef struct _node{

char element;

struct _node *next;

}node;

typedef node *stack;

stack init()

{

stack S;

/* crestr the dummy node */

S= (node *) malloc(sizeof(node));

S-> element =\'\\0\';

return S;

}

int isEmpty (stack S)

{ return (S-> next ++NULL);

}

int isFULL (stack S)

{

/* with dynamic memory the stack never gets full. However, a new allocation request may fail because of memory limitations. That may better be checked immediately after each malloc statement is executed. For simplicity we avoid this check in this implementaion.

return 0;

}

char top (stack s)

{

if (is empty (s))

{

fprintf (stderr, \"top: Empty stack\ \");

return \'\\0\';

}

return s -> next -> element;

}

stack push (stack s, char ch)

{

node* T;

if (is Full(s))

{

fprint (stderr, \"push: Full stack\ \");

return s;

}

C++ Program: It is only 1 rotation. Up-rotation of a stack. Write a function stack upRotate(Stack s) which returns the up-rotation of a stack (the top node is m
C++ Program: It is only 1 rotation. Up-rotation of a stack. Write a function stack upRotate(Stack s) which returns the up-rotation of a stack (the top node is m
C++ Program: It is only 1 rotation. Up-rotation of a stack. Write a function stack upRotate(Stack s) which returns the up-rotation of a stack (the top node is m
C++ Program: It is only 1 rotation. Up-rotation of a stack. Write a function stack upRotate(Stack s) which returns the up-rotation of a stack (the top node is m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site