Question

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 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 CODE:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
   int data;
   struct Node *next;
} Node;

typedef struct List{
   Node *head, *tail;
   int len;//Number of items in the list
} List;

// Function prototypes
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(){
   List *l = initIntegerList();//Create an empty list
   insertAtHead(2, l);
   insertAtHead(1, l);
   insertAtTail(4, l);
   insertAtTail(5, l);
   removeHead(l);
   printHead(l);
   printf("Length of list: %d\n", getListSize(l));
   return 0;
}

List* initIntegerList(){
   List *l = (List *)malloc(sizeof(List));
   l->head = NULL;
   l->tail = NULL;
   l->len = 0;
   return l;
}

// This function return 0 if memory allocation fails otherwise 1 on success
int insertAtHead(int k, List* l){
   Node *newNode = (Node*)malloc(sizeof(Node));
   if(newNode == NULL)
      return 0;//Memory allocation failed
   
   newNode->data = k;
   // If list is empty
   if(l->head == NULL){
      newNode->next = NULL;
      l->head = newNode;
      l->tail = newNode;
   }
   else{
      newNode->next = l->head;
      l->head = newNode;
   }
   l->len++;
   return 1;//Indicates value inserted successfully
}

// This function return 0 if memory allocation fails otherwise 1 on success
int insertAtTail(int k, List* l){
   Node *newNode = (Node*)malloc(sizeof(Node));
   if(newNode == NULL)
      return 0;//Memory allocation failed
   
   newNode->data = k;
   newNode->next = NULL;
   // If list is empty
   if(l->tail == NULL){
      l->head = newNode;
   }
   else{
      l->tail->next = newNode;
   }
   l->tail = newNode;
   l->len++;
   return 1;//Indicates value inserted successfully
}

// This function return 0 if list is empty otherwise 1 on success
int removeHead(List *l){
   if(l->len == 0)
      return 0;//Cannot remove. List is empty
   if(l->len == 1){
      free(l->head);
      l->head = NULL;
      l->tail = NULL;
   }
   else{
      Node *temp = l->head->next;
      free(l->head);
      l->head = temp;
   }
   l->len--;
   return 1;//Remove successful
}

int getListSize(List *l){
   return l->len;
}

// This function prints nothing if list is empty
void printHead(List *l){
   // If list is empty
   if(l->len == 0)
      return;
   printf("%d\n", l->head->data);
}

void moveHeadToTail(List *l){
   if(l->len == 0)
      return;
   l->tail->data = l->head->data;
}

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Please Write the following code in C: PRELAB - Week of October 14 For this prelab...
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
  • 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...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success =...

    Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success = false;         aList.evensFrontOddsEnd(3);         aList.evensFrontOddsEnd(10);         aList.evensFrontOddsEnd(6);         aList.evensFrontOddsEnd(9);         aList.evensFrontOddsEnd(17);         aList.evensFrontOddsEnd(12);         aList.printForward(); // output should be: 12 6 10 3 9 17         aList.printBackward(); // output should be: 17 9 3 10 6 12         success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl;         success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...

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

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

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