Auxiliary space complexity of this solution - linked-list

This is a tail-recursive solution for reversing a singly linked list. What is the auxiliary space occupied by this solution?
void reverseUtil(Node* curr, Node* prev, Node** head)
{
if (curr->next==NULL) {
*head = curr;
curr->next = prev;
return;
}
Node* next = curr->next;
curr->next = prev;
reverseUtil(next, curr, head);
}
void reverse(Node** head)
{
if (head==NULL)
return;
reverseUtil(*head, NULL, head);
}

First, your code should check that *head is not NULL before making the initial call to reverseUtil, so add this condition here:
if (head==NULL || *head==NULL)
return;
This is indeed a case of tail recursion. All current mainstream C compilers perform tail call optimisation when the relevant option is set, and then the tail recursive calls are actually not increasing the stack space usage.
So provided that the compiler is instructed to perform this optimisation, the auxiliary space usage is O(1).
After the optimisation, the execution and space usage will be quite similar to what this iterative code would do:
void reverse(Node** head)
{
if (head==NULL || *head==NULL)
return;
Node* curr = *head;
Node* prev = NULL;
while (true) {
if (curr->next==NULL) {
*head = curr;
curr->next = prev;
return;
}
Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
}

Related

CS50 pset5 Speller [2022] - " :( program is free of memory errors"

I get error ":( program is free of memory errors valgrind tests failed; see log for more information."
Here is my code:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
//Declare variables
unsigned int word_count;
unsigned int hash_value;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// TODO
hash_value = hash(word);
node *cursor = table[hash_value];
// Go in link list
while (cursor != 0)
{
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
unsigned long total = 0;
for (int i = 0; i < strlen(word); i++)
{
total += tolower(word[i]);
}
return total % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// Open dictionary
FILE *file = fopen(dictionary, "r");
// it would be null if cant be open
if (file == NULL)
{
printf("Unable to open %s\n", dictionary);
return false;
}
// Declare variable words
char word[LENGTH + 1];
//Scan dictionary for strings up until EOF
while (fscanf(file, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
//copy wordds into node
strcpy(n->word, word);
hash_value = hash(word);
n->next = table[hash_value];
table[hash_value] = n;
word_count++;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if (word_count > 0)
{
return word_count;
}
return 0;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
while (cursor)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}
if (cursor == NULL)
{
return true;
}
}
return false;
}
Here are the errors in valgrind check50:
program is free of memory errors valgrind tests failed; see log for more information.
Here is ERR log:
56 bytes in 1 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 80)
And 80th line code is:
while (fscanf(file, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
unload will free one index and return to speller because of this if (cursor == NULL) block. The last node in an index should set cursor to NULL, so function is done. That conditional should be eliminated. There is really no condition in unload that should return false.

Why my front view of the Linked list is not printing

So i was trying this question in which I have written the below code:-
#include <bits/stdc++.h>
using namespace std;
struct Node
{
char data;
struct Node* next;
};
struct Node *reverseList(struct Node *head)
{
Node *current = head;
Node *prev = NULL, *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
bool isPalindrome(struct Node* head)
{
Node* frontHead = head;
Node* reverseHead = reverseList(head);
Node* temp = reverseHead;
cout<<"back view"<<endl;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
cout<<"front view"<<endl;
temp = frontHead;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
bool flag = true;
// while(frontHead && reverseHead)
// {
// // cout<<frontHead->data<<"-->"<<reverseHead->data<<endl;
// if(frontHead->data !=reverseHead->data)
// {
// flag=false;
// break;
// }
// frontHead = frontHead->next;
// reverseHead = reverseHead->next;
// }
return flag;
}
void push(struct Node** head_ref, char new_data)
{
struct Node* new_node = (struct Node*)malloc(
sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* ptr)
{
while (ptr != NULL)
{
cout << ptr->data << "->";
ptr = ptr->next;
}
cout << "NULL" << "\n";
}
// Driver code
int main()
{
struct Node* head = NULL;
char str[] = "abacba";
int i;
for (i = 0; i<strlen(str); i++)
push(&head, str[i]);
// printList(head);
isPalindrome(head) ? cout << "Is Palindrome" << "\n\n" : cout << "Not Palindrome" << "\n\n";
return 0;
}
When I try to print the node form the front as well as from the backward(after reversing the linked list I got this output:-
back view
a->b->a->c->b->a->
front view
a->
Is Palindrome
can Anyone tell me why i am not able to get the front view of the linked list .
Any help will be appreciated

threadsafe code with mutex

trying tom make my linked list implementation in c11 (gcc6), threadsafe.
only thing i do not get how many mutex lock and unlocks i should go with?
/**
* adds a new node to head of the list, alocation of node is done dynamically
* #param list address of list
* #param data pointer to data
*/
void add_head(Linked_list* list, void* data)
{
Node *node = (Node*) malloc(sizeof(Node));
//lock
node->data = data;
if (list->head == NULL) {
list->tail = node;
node->next = NULL;
} else {
node->next = list->head;
}
list->head = node;
list->current = node;
list_size ++;
//unlock
}
or
/**
* adds a new node to head of the list, alocation of node is done dynamically
* #param list address of list
* #param data pointer to data
*/
void add_head(Linked_list* list, void* data)
{
Node *node = (Node*) malloc(sizeof(Node));
//lock
node->data = data;
if ( list->head == NULL ) {
list->tail = node;
node->next = NULL;
} else {
node->next = list->head;
}
//unlock
//lock
list->head = node;
list->current = node;
list_size ++;
//unlock
}
or
/**
* adds a new node to head of the list, alocation of node is done dynamically
* #param list address of list
* #param data pointer to data
*/
void add_head (Linked_list* list, void* data)
{
Node *node = (Node*) malloc(sizeof(Node));
//lock
node->data = data;
if (list->head == NULL) {
list->tail = node;
node->next = NULL;
} else {
node->next = list->head;
}
//unlock
//lock
list->head = node;
//unlock
//lock
list->current = node;
//unlock
//lock
list_size ++;
//unlock
}
looking for a way to not make other thread wait too much, since i will have many task of tiny time duration read up to 10 bytes from file, change 10 bytes in memory, write 10 bytes file.
Because you want to support threadsafe for your implementation of function add_head(), you need to guarantee that all shared data accesses have to be atomic.
So I think you should use the first one, i.e use one lock/unlock pair call for whole function implementation.

Using fgets and strtok to read in data and create linked list

Need some help with reading in lines of data from a text file using the fgets and string tokenization commands, which will then be used to create a linked list. I've followed some examples I've found on Stack Overflow and other tutorial websites, but still cannot get the read function below to work properly in my program, it just causes it to crash. The data file has lines like this:
Zucchini, Squash, pound, 2.19, 45
Yellow, Squash, pound, 1.79, 15
Based on everything I've read, I believe I have the necessary code, but obviously I'm missing something. Also, I commented out one of the fields (the one for float price) as I'm not sure what to use to copy the float value from the data, as I cannot treat it as a string (the integer value right below it seems to let me get away with it in my compiler).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct for linked list node
struct produceItem
{
char produce[20];
char type[20];
char soldBy[20];
float price;
int quantityInStock;
struct produceItem *next;
};
// Function to read in data from file to
void read(struct produceItem **head)
{
struct produceItem *temp = NULL;
struct produceItem *right = NULL;
//char ch[3];
char line[50];
char *value;
FILE *data = fopen("RecitationFiveInput.txt", "r");
printf("Trying to open file RecitationFiveInput.txt\n");
if (data == NULL)
{
printf("Could not open file RecitationFiveInput.txt\n");
}
else
{
while(fgets(line, sizeof(line), data))
{
value = strtok(line, ", ");
strcpy(temp->produce, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->type, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->soldBy, strdup(value));
//value = strtok(NULL, ", ");
//strcpy(temp->price, strdup(value));
value = strtok(NULL, " \n");
strcpy(temp->quantityInStock, strdup(value));
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
right = *head;
while(right->next != NULL)
{
right = right->next;
}
right->next = temp;
}
}
printf("Successfully opened file RecitationFiveInput.txt\n");
}
fclose(data);
return;
}
// Function to display the nodes of the linked list that contains the data from the data file
void display(struct produceItem *head)
{
int value = 1;
struct produceItem *temp = NULL;
temp = head;
printf("=============================================================================\n");
printf(" Item # Produce Type Sold By Price In Stock\n");
printf("=============================================================================\n");
if(temp == NULL)
{
return;
}
else
{
while(temp != NULL)
{
printf(" %d %s %s %s %lf %d\n", value, temp->produce, temp->type, temp->soldBy, temp->price, temp->quantityInStock);
value++;
temp = temp->next;
if(temp == NULL)
{
break;
}
}
}
return;
}
//Main function
int main()
{
int input = 0;
struct produceItem *head = NULL;
while(1)
{
printf("\nList Operations\n");
printf("=================\n");
printf("1. Stock Produce Department\n");
printf("2. Display Produce Inventory\n");
printf("3. Reverse Order of Produce Inventory\n");
printf("4. Export Produce Inventory\n");
printf("5. Exit Program\n");
printf("Enter your choice: ");
if(scanf("%d", &input) <= 0)
{
printf("Enter only an integer.\n");
exit(0);
}
else
{
switch(input)
{
case 1:
read(&head);
break;
case 2:
display(head);
break;
case 3:
//function
break;
case 4:
//function
break;
case 5:
printf("You have exited the program, Goodbye!\n");
return 0;
break;
default:
printf("Invalid option.\n");
}
}
}
return 0;
}
Never mind everyone, found the issue. The crashes were due to me not allocating memory for the temp pointer in the read me function.

Memory not freed after calling free()

I have a short program that generates a linked list by adding nodes to it, then frees the memory allocated by the linked list.
Valgrind does not report any memory leak errors, but the process continues to hold the allocated memory.
I was only able to fix the error after I changed the memory allocated from sizeof(structure_name) to fixed number 512. (see commented code)
Is this a bug or normal operation?
Here is the code:
#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct llist_node {
int ibody;
struct llist_node * next;
struct llist_node * previous;
struct llist * list;
}llist_node;
typedef struct llist {
struct llist_node * head;
struct llist_node * tail;
int id;
int count;
}llist;
llist_node * new_lnode (void) {
llist_node * nnode = (llist_node *) malloc ( 512 );
// llist_node * nnode = (llist_node *) malloc ( sizeof(llist_node) );
nnode->next = NULL;
nnode->previous = NULL;
nnode->list = NULL;
return nnode;
}
llist * new_llist (void) {
llist * nlist = (llist *) malloc ( 512 );
// llist * nlist = (llist *) malloc ( sizeof(llist) );
nlist->head = NULL;
nlist->tail = NULL;
nlist->count = 0;
return nlist;
}
void add_int_tail ( int ibody, llist * list ) {
llist_node * nnode = new_lnode();
nnode->ibody = ibody;
list->count++;
nnode->next = NULL;
if ( list->head == NULL ) {
list->head = nnode;
list->tail = nnode;
}
else {
nnode->previous = list->tail;
list->tail->next = nnode;
list->tail = nnode;
}
}
void destroy_list_nodes ( llist_node * nodes ) {
llist_node * llnp = NULL;
llist_node * llnpnext = NULL;
llist_node * llnp2 = NULL;
if ( nodes == NULL )
return;
for ( llnp = nodes; llnp != NULL; llnp = llnpnext ) {
llnpnext = llnp->next;
free (llnp);
}
return;
}
void destroy_list ( llist * list ) {
destroy_list_nodes ( list->head );
free (list);
}
int main () {
int i = 0;
int j = 0;
llist * list = new_llist ();
for ( i = 0; i < 100; i++ ) {
for ( j = 0; j < 100; j++ ) {
add_int_tail ( i+j, list );
}
}
printf("enter to continue and free memory...");
getchar();
destroy_list ( list );
printf("memory freed. enter to exit...");
getchar();
printf( "\n");
return 0;
}
If by "the process continues to hold the allocated memory" you mean that ps doesn't report a decrease in the process's memory usage, that's perfectly normal. Returning memory to your process's heap doesn't necessarily make the process return it to the operating system, for all sorts of reasons. If you create and destroy your list over and over again, in a big loop, and the memory usage of your process doesn't grow without limit, then you probably haven't got a real memory leak.
[EDITED to add: See also Will malloc implementations return free-ed memory back to the system? ]
[EDITED again to add: Incidentally, the most likely reason why allocating 512-byte blocks makes the problem go away is that your malloc implementation treats larger blocks specially in some way that makes it easier for it to notice when there are whole pages that are no longer being used -- which is necessary if it's going to return any memory to the OS.]
I discovered the answer to my question here:
http://linuxupc.upc.es/~pep/OLD/man/malloc.html
The memory after expanding the heap can be returned back to kernel if the conditions configured by __noshrink are satisfied. Only then the ps will notice that the memory is freed.
It is important to configure it sometimes particularly when the memory usage is small, but the heap size is bigger than the main memory available. Thus the program trashes even if the required memory is less than the available main memory.

Resources