Question

in the c programming language. List *init() {    head = ( List * ) malloc( sizeof(...

in the c programming language.

List *init()

{

   head = ( List * ) malloc( sizeof( List ) );

   if ( head == NULL ) {

      prtError( "Insufficient memory!" );

      return( NULL );

   }

   head->data = -1;

   head->next = NULL;

   tail = head;

   return ( head );

}

/* Insert a new data element d into the list. */

/* Insert at the front of the list, right behind the dummy node. */

/* Return NULL if a new node cannot be created. */

/* Otherwise, return the pointer to the newly created node. */

List *insertFirst( int d )

{

   /***** ADD YOUR CODE HERE *****/

}



/* Insert a new data element d into the list. */

/* Insert at the end of the list. */

/* Return NULL if a new node cannot be created. */

/* Otherwise, return the pointer to the newly created node. */

List *insertLast( int d )

{

   /***** ADD YOUR CODE HERE *****/

}



/* Remove the first element of the list, i.e., the node right behind the dummy node. */

/* Return -1 if the list is empty, i.e., containing only the dummy node, */

/* and display error message "Empty list!" on the standard output. */

/* Otherwise, return the data (integer) of the node to be remove. */

int removeFirst()

{

   /***** ADD YOUR CODE HERE *****/

}


/* Search the list for an element containing integer k. */

/* If found, return the pointer to that element.  Otherwise, return NULL. */

/* If there is more than one element containing k, return the pointer to the first encountered element. */

List *search( int k )

{

   /***** ADD YOUR CODE HERE *****/

}

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

#include<stdio.h>
#include<stdlib.h>// for using malloc()

// define the structure for node
struct List {

struct List * next ;
int data;
};

struct List * head = NULL ;

struct List *init(){;
head = ( struct List * ) malloc( sizeof( struct List ) );
if ( head == NULL ) {
printf( "Insufficient memory!" );
return( NULL );
}
head->data = -1;

head->next = NULL;
return ( head );

}

/* Insert a new data element d into the list. */

/* Insert at the front of the list, right behind the dummy node. */

/* Return NULL if a new node cannot be created. */

/* Otherwise, return the pointer to the newly created node. */

struct List *insertFirst( int d )
{ //create new node
struct List * newNode = (struct List * ) malloc( sizeof( struct List ) );
// if node cannot be created
if ( newNode == NULL ) {
printf( "Node cannot be created " );
return( NULL );
}
else //attach the newley created node
newNode->data = d ;
head->next = newNode;
newNode->next = NULL;
return head ;
}
/* Insert a new data element d into the list. */

/* Insert at the end of the list. */

/* Return NULL if a new node cannot be created. */

/* Otherwise, return the pointer to the newly created node. */

struct List *insertLast( int d )

{//create a node
struct List * lNode = NULL ;
lNode = ( struct List * ) malloc( sizeof( struct List ) );

if ( lNode == NULL ) {

printf( "Insufficient memory!" );

return( NULL );

}
else // find the current last node
{
struct List * curr = head ;
while(curr->next != NULL)// to reach to the last node
{
curr = curr->next ;
}
// you are currently at the last node
curr->next = lNode ;// attach created node at the last
//assign value to this node
lNode->data = d ;
return head;
}
}
/* Remove the first element of the list, i.e., the node right behind the dummy node. */

/* Return -1 if the list is empty, i.e., containing only the dummy node, */

/* and display error message "Empty list!" on the standard output. */

/* Otherwise, return the data (integer) of the node to be remove. */

int removeFirst(){
if( head== NULL )
printf("Empty list ");
else
{
struct List* list = head ->next ;//list is pointing to right of dummy node
int value = list->data ;
head->next = head->next->next ;
free(list);
return value;
}
}

/* Search the list for an element containing integer k. */

/* If found, return the pointer to that element. Otherwise, return NULL. */

/* If there is more than one element containing k, return the pointer to the first encountered element. */

struct List *search( int k )
{
struct List * list= head ;
while( list != NULL )
{
if( list->data = k)
return list;
}
printf("Element not found in the List ");

return NULL ;
}
// program to print element of linked list
void print_list( struct List * head ){
struct List *parse = head ;
while( parse != NULL )
{
printf( "%d \t " , parse->data);

parse = parse->next ;

}
printf("\n");
}


int main(){

struct List * head = init();
insertFirst(3);
insertLast(4);
printf("current list elements :\n");
print_list(head);
removeFirst(head);
printf("current list elements after calling removeFirst :\n");
print_list(head);
printf("let insert more elements using insertLast:\n");
insertLast(9);
insertLast(5);

print_list(head);}



Add a comment
Know the answer?
Add Answer to:
in the c programming language. List *init() {    head = ( List * ) malloc( sizeof(...
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
  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • c++ please Change InsertLast to be used on a double linked list instead of a single...

    c++ please Change InsertLast to be used on a double linked list instead of a single linked list. Use prev as the other pointer name. bool LinkedList::InsertLast(int v) { if(size == 0) { return InsertFirst(v); } Node *tmp = new Node; if(tmp == NULL) return false; tmp->value=v; last->next=tmp; last = tmp; size++; if(size == 1){ first = last; } return true; }

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

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