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

typedef struct node { ElemType val; struct node *next; }NODE; typedef struct lis

ID: 3569433 • Letter: T

Question

typedef struct node {

ElemType val;

struct node *next;

}NODE;

typedef struct list_struct {

NODE *front;

NODE *back;

}LIST;

description: assumes both list a and b are in

*    sorted (non-descending) order and merges them

*    into a single sorted list with the same

*    elements.

*

*    This single sorted list is stored in a while

*    b becomes empty.

*

*    if either of given lists are not sorted,

*    we blame the caller and the behavior is

*    implementation dependent -- i.e., don't worry

*    about it!

*

*    Example:

*

*        a: [2 3 4 9 10 30]

*        b: [5 8 8 11 20 40]

*

*        after call on (a,b)

*

*        a: [2 3 4 5 8 8 9 10 11 20 30 40]

*        b: []

*

* implementation: should not allocate ANY new list

*    nodes -- it should just re-link existing

*    nodes.

*

*    Must be linear time in the |a|+|b| -- i.e.,

*    the total number of elements being processed.

*/

void lst_merge_sorted(LIST *a, LIST *b);

assumes given list is already in sorted order

*   and inserts x into the appropriate position

*    retaining sorted-ness.

* Note 1: duplicates are allowed.

*

* Note 2: if given list not sorted, behavior is undefined/implementation

*        dependent. We blame the caller.

*        So... you don't need to check ahead of time if it is sorted.

*/

void lst_insert_sorted(LIST *l, ElemType x);

Explanation / Answer

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 50

int size;

// Defining the stack structure

struct stack {

  int arr[MAX];

  int top;

};

// Initializing the stack(i.e., top=-1)

void init_stk(struct stack *st) {

  st->top = -1;

}

// Entering the elements into stack

void push(struct stack *st, int num) {

  if (st->top == size - 1) {

     printf(" Stack overflow(i.e., stack full).");

     return;

  }

  st->top++;

  st->arr[st->top] = num;

}

//Deleting an element from the stack.

int pop(struct stack *st) {

  int num;

  if (st->top == -1) {

     printf(" Stack underflow(i.e., stack empty).");

     return NULL;

  }

  num = st->arr[st->top];

  st->top--;

  return num;

}

void display(struct stack *st) {

  int i;

  for (i = st->top; i >= 0; i--)

     printf(" %d", st->arr[i]);

}

int main() {

  int element, opt, val;

  struct stack ptr;

  init_stk(&ptr);

  printf(" Enter Stack Size :");

  scanf("%d", &size);

  while (1) {

     printf(" tSTACK PRIMITIVE OPERATIONS");

     printf(" 1.PUSH");

     printf(" 2.POP");

     printf(" 3.DISPLAY");

     printf(" 4.QUIT");

     printf(" ");

     printf(" Enter your option : ");

     scanf("%d", &opt);

     switch (opt) {

     case 1:

       printf(" Enter the element into stack:");

       scanf("%d", &val);

       push(&ptr, val);

       break;

     case 2:

       element = pop(&ptr);

       printf(" The element popped from stack is : %d", element);

       break;

     case 3:

       printf(" The current stack elements are:");

       display(&ptr);

       break;

     case 4:

       exit(0);

     default:

       printf(" Enter correct option!Try again.");

     }

  }

  return (0);

}

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 50

int size;

// Defining the stack structure

struct stack {

  int arr[MAX];

  int top;

};

// Initializing the stack(i.e., top=-1)

void init_stk(struct stack *st) {

  st->top = -1;

}

// Entering the elements into stack

void push(struct stack *st, int num) {

  if (st->top == size - 1) {

     printf(" Stack overflow(i.e., stack full).");

     return;

  }

  st->top++;

  st->arr[st->top] = num;

}

//Deleting an element from the stack.

int pop(struct stack *st) {

  int num;

  if (st->top == -1) {

     printf(" Stack underflow(i.e., stack empty).");

     return NULL;

  }

  num = st->arr[st->top];

  st->top--;

  return num;

}

void display(struct stack *st) {

  int i;

  for (i = st->top; i >= 0; i--)

     printf(" %d", st->arr[i]);

}

int main() {

  int element, opt, val;

  struct stack ptr;

  init_stk(&ptr);

  printf(" Enter Stack Size :");

  scanf("%d", &size);

  while (1) {

     printf(" tSTACK PRIMITIVE OPERATIONS");

     printf(" 1.PUSH");

     printf(" 2.POP");

     printf(" 3.DISPLAY");

     printf(" 4.QUIT");

     printf(" ");

     printf(" Enter your option : ");

     scanf("%d", &opt);

     switch (opt) {

     case 1:

       printf(" Enter the element into stack:");

       scanf("%d", &val);

       push(&ptr, val);

       break;

     case 2:

       element = pop(&ptr);

       printf(" The element popped from stack is : %d", element);

       break;

     case 3:

       printf(" The current stack elements are:");

       display(&ptr);

       break;

     case 4:

       exit(0);

     default:

       printf(" Enter correct option!Try again.");

     }

  }

  return (0);

}