Question

Would you please help me to do this problem? this is c programming. 1. write figures of inserting...

Would you please help me to do this problem?

this is c programming.

1. write figures of inserting 3 elements into a stack linked list and a queue linked list with code fragments and then delete 2 elements from each with associated code fragments.

thank you.

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

Inserting three elements into stack using linked list

Insert 10,20,30

Initially top=NULL

Suppose Node consists of two parts

  1. data

  2. address of next node

Node representation=(data,address)

Insert 10

(10,NULL)

100(address of newly created node)(top points to 100)

Insert 20

(20,100)--------->(10,NULL)

200------------------>100

top

Insert 30

(30,200)----->(20,100)-----> (10,NULL)

300------------> 200-----------> 100

top

(now top is at 300)

Code for inserting elements into stack using linked list

struct Node* insert(int element)
{
struct Node* newnode;
newnode=(struct Node*)malloc(sizeof(struct Node*));
newnode->data=element;
if(top==NULL)
{
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}
return top;
}

Inserting three elements into queue using linked list

Insert 10,20,30

Initially front=NULL,rear=NULL

Suppose Node consists of two parts

  1. data

  2. address of next node

Node representation=(data,address)

Insert 10

(10,NULL)

100(address of newly created node)(front,rear points to 100)

Insert 20

(10,200)------------>(20,NULL)

100--------------------->200

front--------------------- rear

Insert 30

(10,200)------------>(20,300)------>(30,NULL)

100-------------------> 200--------------> 300

front----------------------------------------- rear

Code for inserting elements into queue using linked list

struct Node* insert(int element)
{
struct Node* newnode;
newnode=(struct Node*)malloc(sizeof(struct Node*));
newnode->data=element;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
return front;
}

Deleting two elements from stack using linked list (say 30,20)

(30,200)----->(20,100)-----> (10,NULL)

300-----------> 200-------------> 100

top

(now top is at 300)

Delete 30 from stack

(20,100)----------->(10,NULL)

200 ------------------->100

top

Delete 20 from stack

(10,NULL)

100

top

Code for deleting element from stack using linked list

void delete()
{
struct Node* temp;
temp=top;
if(top==NULL)
{
printf("stack is empty");
}
else
{
top=top->next;
free(temp);
}
}

Deleting two elements from queue using linked list (say 10,20)

((10,200)------------>(20,300)------>(30,NULL)

100-------------------> 200--------------> 300

front ----------------------------------------- rear

Delete 10 from queue

(20,300)------>(30,NULL)

200---------------> 300

front ----------------rear

Delete 20 from queue

(30,NULL)

300

front

rear

Code for deleting element from queue using linked list

void delete()
{
struct Node* temp;
temp=front;
if(front==NULL)
{
printf("queue is empty");
}
if(front==rear) //if there is only one element in queue to be removed
{
free(temp);
front=NULL;
rear=NULL;
}
else
{
front=front->next;
free(temp);
}
}

Add a comment
Know the answer?
Add Answer to:
Would you please help me to do this problem? this is c programming. 1. write figures of inserting...
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
  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • could someone help me make a linked list in javascript, that has methods of inserting, updating...

    could someone help me make a linked list in javascript, that has methods of inserting, updating element by index, deleting by index, obtaining all the elements, obtaining elements by index, I would be very grateful to you to help me :)

  • Would someone be able to help me with this please? It has to do with Linked...

    Would someone be able to help me with this please? It has to do with Linked List Arrays, and I'm very confused on what to do. Any help along with steps would be very much appreciated. Cheers! You are hired by a company to implement a singly-linked list that will hold integers, using Java programming language. However, the boss of the company insists that you don’t use any objects or structures in your implementation. You may use only integer arrays...

  • C Programming -- please use simple coding Write a C program that determines whether a line...

    C Programming -- please use simple coding Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation).  Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out).

  • Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

    Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...

  • It is C++ problems, please explain your each line of code as well. Task 1: Implement/...

    It is C++ problems, please explain your each line of code as well. Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for this example. The user will input a string and the program will output the string backwards. Displaying correct Stack implementation. Utilize the conventional static methods as needed. push() pop() empty() peek() peek() Sample Execution Please input String: happy yppah Task 2: Implement /...

  • CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write...

    CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write a program for the patient information system in a policlinic. The patients are examined according to the triage char assigned to them. Triage codes are as follows: - ‘h’ : high priority - ‘m’ : medium priority - ‘l’ : low priority The patients having ‘h’ triage char are examined first then the patients having ‘m’ comes and finally patients with ‘l’ triage code...

  • Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e....

    Exereise 1: 7 рoints Write a program in C/C++ to enter a string of characters i.e. your name, then try to compute this string in reverse order using a Stack structure with the Linked List principle. For example: "Nguyen Van A""A Nav Neyugn Exercise 2: 8 points Assume that a queue of N passengers waiting to enter a train car Only the first person in the queue can enter at a time New persons arrive will go to the end...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • This is a Practice problem for a Exam. PLEASE WRITE IN JAVA ONLY. Be able to...

    This is a Practice problem for a Exam. PLEASE WRITE IN JAVA ONLY. Be able to write the code for inserting a node into a Doubly Linked List. Please answer the following scenarios 1.At the Start 2.At the End 3.In the Middle

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