Question

Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete...

Exercise: Delete First Element from a Linked List (pair)

This is a pair exercise to complete with your lab partner.

Download list_delete_first.c here, or copy it to your CSE account using the following command:

cp -n /web/dp1091/20T1/activities/list_delete_first/list_delete_first.c .

Your task is to add code to this function in list_delete_first.c:

//
// Delete the first node in list.
// The deleted node is freed.
// The head of the list is returned.
//
struct node *delete_first(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return NULL;
}

Note list_delete_first.c uses the following familiar data type:

struct node {
    struct node *next;
    int          data;
};

delete_first is given one argument, head, which is the pointer to the first node in the linked list.

Add code to delete_first so that it deletes the first node from list.

delete_first should return a pointer to the new first node in the list.

If the list is now empty delete_first should return NULL.

delete_first should call free to free the memory of the node it deletes.

For example if the linked list contains these 8 elements:

16, 7, 8, 12, 13, 19, 21, 12

delete_first should return a pointer to a list with these elements:

7, 8, 12, 13, 19, 21, 12

Hint: this is a simple task requiring only a few lines of code.

Testing

list_delete_first.c also contains a main function which allows you to test your delete_first function. It converts command-line arguments to a linked list, calls delete_first, and then prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your delete_first function will be called directly in marking. The main function is only to let you test your delete_first function

Here is how you the main function allows you to test delete_first:

cp -n /web/dp1091/20T1/activities/list_delete_first/list_delete_first.c .
dcc list_delete_first.c -o list_delete_first
./list_delete_first 16 7 8 12 13 19 21 12
[7, 8, 12, 13, 19, 21, 12]
./list_delete_first 2 4 6 2 4 6
[4, 6, 2, 4, 6]
./list_delete_first 42
[]
./list_delete_first
[]

Assumptions/Restrictions/Clarifications.

delete_first should call free to free the memory for the node it deletes

delete_first should not change the data fields of list nodes.

delete_first should not use arrays.

delete_first should not call malloc.

delete_first should not call scanf (or getchar or fgets).

delete_first should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

New! You can run an automated code style checker using the following command:

~dp1091/bin/style list_delete_first.c

When you think your program is working you can use autotest to run some simple automated tests:

HERE IS THE STARTING CODE

https://shrib.com/#BlackVulture7DKWejX

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have attached code and output screenshots for better understanding .Please go through it.

Note : while entering numbers through command line press -1 to stop.


// linked list.
#include <stdio.h>
#include<stdlib.h>

/* Link list node */
struct Node {
   int data;
   struct Node* next;
};

/* Function to remove the first node
of the linked list */
struct Node* delete_first(struct Node* head)
{
   if (head == NULL)
       return NULL;

   // Move the head pointer to the next node
   struct Node* temp = head;
   head = head->next;

   free(temp);

   return head;
}

// Function to insert node at last
void insert(struct Node** head_ref, int new_data)
{
   struct Node* new_node = (struct Node*)malloc(sizeof(struct node*));
   new_node->data = new_data;
   new_node->next = NULL;
   struct Node*temp=*head_ref;
   if((*head_ref)==NULL)
       *head_ref=new_node;
   else
   {
       while(temp->next!=NULL)
           temp=temp->next;
       temp->next=new_node;
   }
  

}

// Driver code
int main(int argc ,char* argv[])
{
   /* Start with the empty list */
   struct Node* head = NULL;   /* Use insert() function to construct
   the list*/
int i;
for (i=1;i<argc;i++)
{
   int val=atoi(argv[i]);

if (val == -1)
       break;
   insert(&head, val);
}


   head = delete_first(head);
   for (struct Node* temp = head; temp != NULL; temp = temp->next)
       printf(" %d",temp->data);

   return 0;
}


  

Add a comment
Know the answer?
Add Answer to:
Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete...
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
  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • Define a struct Node to represent a node in a double linked-list that stores a string...

    Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • 1. A linked list does not need to have a pointer that points to the first...

    1. A linked list does not need to have a pointer that points to the first node of the list. True or False? 2. A linked list needs to have a pointer to point to the last node of the list.  True or False? 3. If "head" is the only pointer that points to the first node of a linked list, to traverse the list, you should move the head pointer to each node one at a time.  True or False? Please...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

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