0% found this document useful (0 votes)
18 views16 pages

Lab Assignment 5

This document is a lab assignment submitted by Tarang Srivastava for a Computer Science course, focusing on string manipulation and dynamic memory allocation in C. It includes tasks such as checking for palindromes, understanding memory allocation functions (malloc, calloc, free, realloc), and constructing a singly linked list with various operations. Observations and time complexities for linked list operations are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

Lab Assignment 5

This document is a lab assignment submitted by Tarang Srivastava for a Computer Science course, focusing on string manipulation and dynamic memory allocation in C. It includes tasks such as checking for palindromes, understanding memory allocation functions (malloc, calloc, free, realloc), and constructing a singly linked list with various operations. Observations and time complexities for linked list operations are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB ASSIGNMENT 5: Strings

Submitted By:
Name: TARANG SRIVASTAVA
Roll No: 221210109
Branch: CSE SECTION:2
Semester: 2nd Sem
Group : 2

Submitted To: Dr. Chandra Prakash

Department of Computer Science and Engineering

NATIONAL INSTITUTE OF
TECHNOLOGY DELHI
2023
1. Write a program find whether a string is a palindrome or not.

Observation :
To check whether the string is palindrome or not we initialise two indexes one from
beginning of the array and one from the end of the array , and check weather the
element at both the indexes are same or not.
2. the malloc command details in Linux using man. Understand and write the difference
between malloc, calloc, free and realloc. Write a C program to demonstrate Dynamic
Memory Allocation using malloc(), calloc(), free() and realloc().
Observation: Observation –

malloc is a function in C that is used for dynamic memory allocation. It allocates a block of
memory of a given size and returns a pointer to the first byte of the block. The `malloc` function
takes a single argument, which is the number of bytes to allocate.
calloc is also used for dynamic memory allocation, but it initializes the allocated memory to
zero. The `calloc` function takes two arguments: the number of elements to allocate, and the size
of each element in bytes.
free is used to deallocate memory that was previously allocated using `malloc` or `calloc`. It
takes a single argument, which is a pointer to the memory block to be deallocated.
realloc is used to resize a previously allocated block of memory. It takes two arguments: a
pointer to the previously allocated memory block, and the new size of the block in bytes.
3.The size of for a structure is not always equal to the sum of sizeof of each individual member.
Illustrate it with the help of an example that calculate the size of an structure. Change the order
and check the updated size. Can you conclude something from this. (Hint: consider three
elements of structure; int, char, double)
Observation –

structure has changed its size. In this case, the size of the structure has decreased from 24 bytes
to 16 bytes, because the padding required for alignment is different when the members are in a
different order.
The size of for a struct is not always equal to the sum of size of each individual member. This is
because of the padding added by the compiler to avoid alignment issues. Padding is only added
when a structure member is followed by a member with a larger size or at the end of the structure

4 . Using dynamic variables and pointers write a C program to construct a singly linked list
consisting of the following information in each node; Roll - No (Integer), Name (Character
string) The operations to be supported are ;
a. LINSERT Inserting a node in the front of the list
b. LDELETE Deleting the node based on Roll - No
c. LSEARCH Searching a node based on Roll-No
d. LDISPLAY Displaying all the nodes in the list
e. Count no of nodes in the link list.
f. Sort as per the roll no.
g. Reverse the link list as per the roll no Find the Time complexity of each operation
Observation of 4th question :
Observation-

Time complexity in displaying a linked list = O(N)


Time complexity in inserting at head in linked list= O(1)
Time complexity in deletion at any index in linked list = O(N)
Time complexity in searching in linked list = O(N)
Time complexity in counting the number of nodes in linked list= O(N)
Time complexity in sorting linked list= O(N**2)
Time complexity in reversing linked list = O(N**2)

To sort the linked list I used the bubble sort method

You might also like