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

The following code should be coded in C language! Thank you! Use the following s

ID: 3854596 • Letter: T

Question

The following code should be coded in C language! Thank you!

Use the following structure definition:

typedef struct Integer_

{

    int num;

    struct Integer_* nextInt;

    struct Integer_* prevInt;

} Integer;

You will make a simple stack using a linked list. This linked list will have pointers pointing both ways so that we can get to the bottom of the stack if we need to, but also just keep track of the top of the stack since that is where we are adding nodes. In the real world, you will likely keep track of both the top and bottom using a pointer for each, but for this, we will just keep track of the top.

Make sure you don’t screw up your list! You need to hook up the prevInt pointer as well as the nextInt pointer. Make sure the bottom of the stack also has a prevInt value set as well. What should it be?

Use the following function prototypes:

Integer* addInt(Integer*, Integer*);

Adds an integer to the stack. Returns the new top of the stack.

void printNumbers(Integer*);

Prints out all numbers, starting with the BOTTOM of the stack. Be careful! The pointer passed is pointing to the top of the stack.

void freeNumbers(Integer*);

Frees all nodes, starting with the top of the stack.

Integer* deleteInt(Integer*);

Deletes an integer from the stack. Returns the new top of the stack.

Sample Output

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>1

Insert your Number: 10

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>1

Insert your Number: 11

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>1

Insert your Number: 12

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>3

10->11->12

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>2

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>3

10->11

Select an option:

1: add a number

2: take a number off

3: print numbers

4: quit

Option:

>4

RUN SUCCESSFUL (total time: 15s)

Explanation / Answer

Hi,

This is the program which implements your problem :

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct Integer_
{
int num;
struct Integer_* nextInt;
struct Integer_* prevInt;
} Integer;

Integer *top, *bottom, *temp, *temp1, *new_top;

void create(int num)
{
temp =(struct Integer_ *)malloc(1*sizeof(struct Integer_));
temp->prevInt = NULL;
temp->nextInt = NULL;
temp->num = num;
}


Integer* addInt(int num)
{
if (bottom == NULL)
{
create(num);
bottom = temp;
temp1 = bottom;
top = bottom;
}
else
{
create(num);
temp1->nextInt = temp;
temp->prevInt = temp1;
temp1 = temp;
top = temp1;
}
return top;
}


Integer* deleteInt()
{
if ( top != NULL && top->prevInt !=NULL )
{
top->prevInt->nextInt = NULL;
top = top->prevInt;
}
if ( top == bottom)
top = bottom = NULL;

return top;
}
void printNumbers(Integer * top)
{
Integer * tmp_bottom = bottom;
while (tmp_bottom != NULL)
{
printf("%d >", tmp_bottom->num);
tmp_bottom = tmp_bottom->nextInt;
}
printf(" ");
}


int main()
{
int choice;
top = NULL;
temp = temp1 = NULL;
while (1)
{
printf("Select an option: ");
printf("1: add a number ");
printf("2: take a number off ");
printf("3: print numbers ");
printf("4: quit ");
scanf("%d", &choice);
switch (choice)
{
case 1:
new_top = (struct Integer_ *)malloc(sizeof(*new_top));
int num;
printf("enter your number ");
scanf("%d",&num);
new_top = addInt(num);
break;
case 2:
new_top = deleteInt();
break;
case 3:
printNumbers(top);
break;
case 4:
exit(0);
break;
default:
printf("Wrong value entered. Enter again ");
}
}
return 0;
}

Thanks

Nikhil Jain

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