Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete the code based on the comments above each function. #include <st

ID: 3629357 • Letter: P

Question

Please complete the code based on the comments above each function.


#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "llist.h"

/*
* This function allocates storage for a new doubly-linked list element of type LList t; storage is allocated dynamically using malloc(). The function also initializes the structure, setting the values for the next and prev fields to NULL and the value field to equal the value of the argument of this function. The return value is a pointer to the newly created element (of type LList t *), or NULL if malloc was unsuccessful. Since this is a library function, rather than printing out a message and exiting upon encountering an error, this function simply returns NULL to inform the caller that the function did not succeed. This function must be the only place in your code that calls malloc(). For example, when functions like llist insert before() need to create a new LList t structure, they must use this function.
*/
LList_t *llist_create(int val)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}



/*
* This function just releases the resources allocated to a single LList t struct by llist create(), calling free() to return the LList t structure to the pool of heap memory available to your program. llist delete does not need to do anything about the relation of ele to other elements in the list; it just needs to release resources. This function must be the only place in the code that calls free(), and when llist delete list()
releases a LList t struct, it must use this function. This function should handle the situation where
ele is NULL.
*/
void llist_delete(LList_t *ele)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
}



/*
* Given a pointer to an element in a list (or NULL to mean the list is empty) this function returns a pointer to the first element in the list. If the list is empty, it returns NULL.
*/

LList_t *llist_head(LList_t *ele)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}



/*
* Given a pointer to any element in a list, this function returns a pointer to the last element in the list. If the list is empty, it returns NULL.
*/
LList_t *llist_tail(LList_t *ele)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}


/*
* This function searches the list starting at ele and returns a pointer to the first element it finds that has a value field equal to val. If there is no such element, then this function returns NULL. Notice that the search begins at the given element, not the start of the list, and stops when it either nds an element with the given value or when it reaches the end of the list. Although the search begins at a given element, not necessarily the head of the list, the search is in one direction only, so if you wanted to think of the given element as a new head of the list, that would be consistant with our implementation.
*/
LList_t *llist_lookup(LList_t *ele, int val)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}



/*
* This function inserts a new element, with value val, into the list of which ele is an element. The new element must be inserted immediately before the given element ele (and ele does not need to be the head of the list). The return value of this function is a pointer to the new element. If ele is NULL, this
function creates a new list with the given value in it and returns a pointer to it. This function returns NULL if it fails (e.g., an internal call to llist create() failed).
*/
LList_t *llist_insert_before(LList_t *ele, int val)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}

/*
* This function inserts a new element, with value val, into the list of which ele is an element. The new
element must be inserted immediately after the given element (and ele does not need to be the head
of the list). The return value of this function is a pointer to the new element. If ele is NULL, this
function creates a new list with the given value in it and returns a pointer to it. This function returns
NULL if it fails (e.g., an internal call to llist create() failed).
*/

LList_t *llist_insert_after(LList_t *ele, int val)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}



/*
* This function removes and deletes the first element in list whose value is val. Just as in llist lookup,
the search begins at list, and you should think of that as the start of the list. The return value for
this function is the new start of your list. In particular, the function acts as follows:
1. If the input list is NULL, then NULL is returned.
2. If the removed element is the only element of the list, then NULL is returned.
3. If the removed element is the first element in the list, then a pointer to the second element in the
list is returned.
4. Otherwise, the pointer list is returned.
*/

LList_t *llist_remove(LList_t *list, int val)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
return NULL;
}


/*
* This function deletes all of the elements of a list starting at the element pointed to by list. If the element pointed to by list is not the head of a list, the element preceding the one pointed to by list becomes the tail of the larger list. In other words, you can use llist delete list to delete the suffix of a larger list.

*/
void llist_delete_list(LList_t *list)
{
/* REPLACE THIS CODE WITH YOUR IMPLEMENTATION. */
}

/*
* end of llist.c
*/

Explanation / Answer

This tutorial is intended to tell beginner the power of pointers in C programming. This tutorial will try to explain how with the help of pointer we can solve the problem of memory management. If you don’t know what pointer is in C language then please click here to learn it now. We have used variable which requires all variables to be declared before we use it. So if we need to store 100 students roll no then we need to declare 100 individual integer variables. This problem can be solved with the help of array; single array declaration can store 100 or more integer values. One of the biggest limitation is array size cannot be increased or decreased during execution of a program. To get this better consider the following C program statement: int roll_no[100]; Suppose we want to store roll_no of 100 students (200 bytes of memory occupied due to above declaration) and above C language statement will do the same task. During C program execution I thought that I will enter only 40 students’ roll_no. This decision of entering only 40 students’ roll_no will use 80 bytes out of 200 bytes (2 bytes per integer*40 students) of memory and remaining 120 bytes of memory will be useless. If we want to store 150 students’ roll_no using same program, we cannot do so because we have declared array to store only 100 students’ roll_no. To overcome this memory allocation problem we need to dynamically allocate memory according to user input. We can do this with C’s two inbuilt functions calloc, malloc and a pointer. Calloc and malloc is two functions which help to dynamically allocate memory at runtime (during program execution) and this can reduce memory wastage problem. C Program to show Dynamic Memory Allocation using Malloc() #include #include #include void main() { int no, *pt,i; clrscr(); printf("Enter no of Students :"); scanf("%d",&no); pt=(int *)malloc(no*2); if(pt== NULL) { printf(" Memory allocation failed!"); getch(); exit(0); } printf("* * * * Enter roll no of students. * * * * "); for (i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote