Question

IN C ONLY PLZ (read the instruction carefully plz) You will implement the following functions: List...

IN C ONLY PLZ (read the instruction carefully plz)

You will implement the following functions:

List * initIntegerList( ) // Return empty list

int insertAtHead(int k, List*) // Insert k at head

int insertAtTail(int k, List*) // Insert k at tail

int removeHead(List *) // Remove head and return its key

int getListSize(List *) // Return number of elements in list

void printHead(List *) // Print key in head

void moveHeadToTail(List *) // Move key at head to tail

This doesn’t seem so bad – and it isn’t. However, there is a performance requirement that must be satisfied: all of the functions must take only a constant amount of time independent of the length of the list, i.e., they must all have O(1) complexity. Does this make things much more difficult? No, you just need to define a struct (List) that contains a pointer to the head of a linked list; a pointer to the tail of the list; and an integer containing the number of items/keys in the list. (Note that nodes in the linked list will not be List structs.) The information in the List struct is what will allow all operations to be satisfied in O(1) time.

Note that the above prototypes impose restrictions on how you handle potential error conditions, e.g., if the user tries to remove the head of an empty list, so you will need to document those situations for the user. For example, the function printHead will need documentation telling the user that nothing is printed if the list is empty. The two insert functions, however, have integer return values that can be used for error codes.

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

// C program to implement Linked list operations in O(1) time

#include <stdio.h>

#include <stdlib.h>

// structure to represent a node in the linked list

typedef struct Node

{

       int data;

       struct Node *next;

}Node;

// structure to represent the list

typedef struct List

{

       Node *head;

       Node *tail;

       int size;

}List;

// function prototype

List * initIntegerList( ); // Return empty list

int insertAtHead(int k, List*); // Insert k at head

int insertAtTail(int k, List*); // Insert k at tail

int removeHead(List *); // Remove head and return its key

int getListSize(List *); // Return number of elements in list

void printHead(List *); // Print key in head

void moveHeadToTail(List *); // Move key at head to tail

int main(void) {

       printf("\nInsert At Head :");

       List *list = initIntegerList();

       int result;

       if(list != NULL)

       {

             int i;

             for(i=1;i<=5;i++ )

             {

                    result = insertAtHead(i,list);

                    if(result == 0)

                    {

                           exit(1);

                    }

                    printHead(list);

             }

             printf("\nInsert At Tail :");

             for(i=6;i<=10;i++)

             {

                    result = insertAtTail(i,list);

                    if(result == 0)

                           exit(1);

             }

             printHead(list);

             printf("\nSize : %d",getListSize(list));

             printf("\nMove Head to Tail :");

             for(i=0;i<getListSize(list);i++)

             {

                    printHead(list);

                    moveHeadToTail(list);

             }

             printHead(list);

             printf("\nSize : %d",getListSize(list));

             printf("\nRemove Head :");

             while(getListSize(list) > 0)

             {

                    //printHead(list);

                    printf("\nRemoved : %d",removeHead(list));

             }

       }

       return EXIT_SUCCESS;

}

// function that creates and returns a new empty list, returns NULL if memory allocation failed

List * initIntegerList( ) // Return empty list

{

       // create a new List

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

       // check if memory is allocated successfully

       if(list != NULL)

       {

             // initialize head and tail to NULL and set size to 0

             list->head = NULL;

             list->tail = NULL;

             list->size = 0;

       }

       return list; // return list

}

// function to insert the node at the head of the list

// returns 1 if insertion is successful else 0

int insertAtHead(int k, List *list) // Insert k at head

{

       Node *node = (Node*)malloc(sizeof(Node)); // create a new node to contain k

       // if memory allocation is successful

       if(node != NULL)

       {

             // insert the node at head

             node->data = k;

             node->next = list->head;

             list->head = node;

             if(list->tail == NULL)

                    list->tail = node;

             list->size++;

             return 1; // insertion successful

       }

       return 0; // insertion unsuccessful

}

// function to insert a node at the tail

// returns 1 if insertion is successful else 0

int insertAtTail(int k, List *list) // Insert k at tail

{

       // create a new node to contain k

       Node *node = (Node*)malloc(sizeof(Node));

       // if memory allocation is successful

       if(node != NULL)

       {

             // insert node at the tail

             node->data = k;

             node->next = NULL;

             if(list->tail !=NULL)

             {

                    list->tail->next = node;

             }else

                    list->head = node;

             list->tail = node;

             list->size++;

             return 1; // insertion successful

       }

       return 0; // insertion unsuccessful

}

// function to remove and return the value at the head of the list

// return the key value if deletion is successful else returns -999

int removeHead(List *list) // Remove head and return its key

{

       if(list->head == NULL) // empty list

       {

             printf("\nEmpty list");

             return -999;

       }

       else

       {

             // delete the node at the head

             int k = list->head->data;

             Node *node = list->head;

             list->head = list->head->next;

             if(list->head == NULL)

                    list->tail = NULL;

             list->size--;

             free(node);

             return k;

       }

}

// function to return the size of the list

int getListSize(List *list) // Return number of elements in list

{

       return list->size;

}

// function to print the value at the head of the list

void printHead(List *list) // Print key in head

{

       if(list->head == NULL) // empty list

             printf("\nEmpty list");

       else // non-empty list

             printf("\nHead : %d",list->head->data);

}

// function to move the node at head to the tail

void moveHeadToTail(List *list) // Move key at head to tail

{

       if(list->head == NULL) // empty list

             printf("\nEmpty list");

       else

       {

             if(list->head->next != NULL) // more than 1 node

             {

                    // move the head node to tail

                    Node *node = list->head;

                    list->head = list->head->next;

                    node->next = NULL;

                    list->tail->next = node;

                    list->tail = node;

             }else // 1 node so no movement

                    printf("\nList has only one element");

       }

}

//end of program

Output:

Insert At Head Head 1 Head 2 Head 3 Head 4 Head 5 Insert At Tail Head 5 Size 10 Move Head to Tail Head 5 Head 4 Head 3 Head 2

Add a comment
Know the answer?
Add Answer to:
IN C ONLY PLZ (read the instruction carefully plz) You will implement the following functions: List...
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
  • Please Write the following code in C: PRELAB - Week of October 14 For this prelab...

    Please Write the following code in C: PRELAB - Week of October 14 For this prelab you will implement the following functions: List * initIntegerList() // Return empty list int insertAtHead(int k, List*) // Insert k at head int insertAtTail(int k, List*) // Insert k at tail int removeHead(List *) // Remove head and return its key int getListSize(List *) // Return number of elements in list void printHead(List *) // Print key in head void moveHeadToTail(List *) // Move...

  • 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...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • Using C++ • Implement the following in both string linked list and in the template version...

    Using C++ • Implement the following in both string linked list and in the template version of the class: • Add to the back and remove from back of the list : - void addBack(….); - string removeBack(); E removeBack(…); • Insert elements in a sorted list i.e., find the position where an element fits and add it to the list. Note you must call the addBack and addFront functions if you insert in the front or rare of the...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • 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...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...

  • 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...

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