Question

13. Given the following structure struct node { struct node *next; int id; }; Write a...

13. Given the following structure struct node { struct node *next; int id; };

Write a code to insert a new node into the linked list as the last node of the linked list

struct node *insertLast(stuct node **head, int newId)

{





}


14. Given the following structure struct node { struct node *next; int id; };

Write a code to insert a new node into the linked list after a node with the same id as the input parameter id

struct node *insert(stuct node **head, int id, int newId) {}

15. Given the following structure struct node {

struct node *next;

int id;
};
Write a code to delete the first node from the linked list. Copy the id from the deleted node to the parameter id

struct node *delete(stuct node **head, int *id)

{





}


16. Given the following structure

struct node {

struct node *next;

int id;

};

Write a code to delete a node from the linked list that has same id as the input parameter id

struct node *deleteById(stuct node **head, int id) { }


17. Given the following structure

struct node {

struct node *next;

int id;

};

Write a code to delete the last node from the linked list. Copy the id from the deleted node to the parameter id
struct node *deleteLast(stuct node **head, int *id) {}
  

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

13. Given the following structure struct node { struct node *next; int id; };

Write a code to insert a new node into the linked list as the last node of the linked list

struct node *insertLast(struct node **head, int newId)
{
   struct node *temp = *head;
  
   //create a new node
   struct node *newnode = (struct node*)malloc(sizeof(struct node));
   newnode->id = newId;
   newnode->next = NULL;
  
   //check is linked list empty
   if(*head==NULL)
   {
       *head = newnode;
       return *head;
   }

   while(temp->next!=NULL)
   {
       temp = temp->next;
   }
   temp->next = newnode;

   return *head;
}


14. Given the following structure struct node { struct node *next; int id; };

Write a code to insert a new node into the linked list after a node with the same id as the input parameter id

struct node *insert(struct node **head, int id, int newId)
{
struct node *temp = *head;

   //create a new node
   struct node *newnode = (struct node*)malloc(sizeof(struct node));
   newnode->id = newId;
   newnode->next = NULL;
  
   //check is linked list empty
   if(*head==NULL)
   {
       *head = newnode;
       return *head;
   }

   while(temp->id!=id)
   {
       temp = temp->next;
   }

   struct node *nextnode = temp->next;
  
   temp->next = newnode;
   newnode->next = nextnode;

   return *head;

}


15. Given the following structure struct node {

struct node *next;

int id;
};
Write a code to delete the first node from the linked list. Copy the id from the deleted node to the parameter id

struct node *delete(struct node **head, int *id)
{
   struct node *temp = *head;
  
   *head = temp->next;
   temp->next = NULL;
*id = temp->id;

   //deallocate memory
   free(temp);

   return *head;
}


16. Given the following structure

struct node {

struct node *next;

int id;

};

Write a code to delete a node from the linked list that has same id as the input parameter id

struct node *deleteById(struct node **head, int id)
{
   struct node *temp = *head;
   struct node *ptemp = NULL;

   while(temp->id!=id)
   {
       ptemp = temp;
       temp = temp->next;
   }

   ptemp->next = temp->next;
   temp->next = NULL;
*id = temp->id;

   //deallocate memory
   free(temp);

   return *head;
}


17. Given the following structure

struct node {

struct node *next;

int id;

};

Write a code to delete the last node from the linked list. Copy the id from the deleted node to the parameter id
struct node *deleteLast(struct node **head, int *id)
{
   struct node *temp = *head;
   struct node *ptemp = NULL;

   while(temp->next!=NULL)
   {
       ptemp = temp;
       temp = temp->next;
   }

   ptemp->next = NULL;
   temp->next = NULL;
*id = temp->id;

   //deallocate memory
   free(temp);

   return *head;

}

Add a comment
Know the answer?
Add Answer to:
13. Given the following structure struct node { struct node *next; int id; }; Write a...
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
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