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.
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
Please Write the following code in C: PRELAB - Week of October 14 For this prelab...
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...
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...
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...
// 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; ...
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. 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...
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...
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;...
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 =...