0% found this document useful (0 votes)
10 views

Unit - 4 list

List Note

Uploaded by

headofthetable06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit - 4 list

List Note

Uploaded by

headofthetable06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit -4 List

A linear list is an ordered set consisting of a number of elements to which addition and deletion can be made. A
linear list display the relationship of physical ardency. The first element of a list is called the heard of list and the
last list is called the tail of the list. The next element of the head of the list is its successes. The previous element to
the tail is called its predecessor. Clearly a head does not have as predecessor and a tail does not have successor.
Any other of the list has both one successor and one predecessor.

Operation perform in list

A) Traversing an array list


B) Searching an element in the list
C) Insertion of an element in the list
D) Deletion of an element in the list

Array implementation

Traversing an array list:

Here each element can be found through index number of array and is incremented by 1

When index is = 0

Then

Arr[0]=10 ;determines 1st element in array.

Index=index+1

Then

Arr[index]=20;

In this way we can transverse each element of array by increment the index by 1 .

Searching in an array list:

For searching an element, we first traverse the array list and while traversing we compare each element of array
with the given element.

For(i=0;i<n;i++)
{

If(intem==arr[i])

Return arr[i];

Insertion of an element in the list:

Two ways:

i) Insertion at the end


ii) Insertion in between
i) Insertion at the end :-

set the array index to next of last value


arr[index]=value;
ii) Insertion in between :
Shift right one position all array elements from the last array
Elemnet to the array before which we want to insert the element
Int item, position;
If(n==max)
{
Printf(“list is overflow”);
Return;
}
If(position > n+1)
{
Printf(“\n enter valid position”);
Return ;
}
If(position == n+1)
{
Arr[n]=item;
N=n+1;
Return;
}
Insertion in between
Temp=n-1;
While(temp>=position-1)
{
Arr[temp+1]=arr[temp];
Temp--;
}
Arr[position-1]=item;
N=n+1;
Deletion of an element in the list

Deletion of the last element.

Deletion in between

Deletion of the last element

Traverse the array last and if the item is last of the array, then delete that

Element and decrease the total number of element by 1.

First traverse the array list and compare array element with the element

Which to be deleted, then shift left one position from the next element to the

Last element of array and decrease the total no of element by 1.

Int temp,position,item,n;

If(n==0){

Printf(“list is underflow”);

Return ;

If(item=arr[n-1])

Printf(“\n deletion is last item”);

N=n-1;

Return;

Temp=position-1;

While(temp<=n-1)

Arr[temp]=arr[temp+1];

Temp++;
}

N=n-1;

Advantage of list

a) Easy to compute the address of the array through the index.


b) Easy to creating and accessing element of array.
c) Easy to access random value.

Dis-advantage

a) Use of continuous list which is time consuming


b) As array size is declared we can not take more element of the array size.
c) If the elements are less than the size of an array then wastage of memory.

To overcome this problem, we use linked list or DLL.

Dynamic memory allocation

Dynamic Memory Allocation


The programmer uses the in-build function of stdlib.h for dynamic memory allocation. The
function calloc() and malloc() used to create the space for array, structure, and union dynamically.
The difference in malloc and calloc is that malloc does not set the memory to zero whereas calloc
sets allocated memory to zero. If there is no reason to initialize the array to zero, then the use of
either calloc() or malloc() is acceptable. Generally, the malloc() takes less time.

A. Function: malloc ()
The C library function void *malloc(size_in_bytes) allocates the requested
memory and returns a pointer to it.

Syntax: void *malloc (size_in_bytes);


This function returns a pointer to the allocated memory, or NULL if the request fails.

Example-1: malloc () for string function


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char *str;
str = (char *) malloc(15);

strcpy(str, "lumbini ");

printf("String = %s \n", str );

str = (char *) realloc(str, 25);


strcat(str, "college");

printf("String = %s ", str );

free(str);

getch();
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int *num;
int i,j,n,temp;
printf("\n enter the value of n");
scanf("%d",&n);
num = (int *) malloc(n);
printf("\n enter %d numbers",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);

printf("\n sorted numbers are ");


for(i=0;i<n;i++)
printf(" %d",num[i]);

getch();
}
B. Function: calloc()
The C library function allocates the requested memory and returns a pointer to it.

Syntax:
void *calloc (number_of_blocks, size_of_each_block_in_bytes);

This function returns a pointer to the allocated memory, or NULL if the request fails.

For example: a = calloc (n, sizeof (int)); This allocates contiguous space in
memory for an array of n-elements with each element having 2-byte memory (since for int). Here
‘a’ is an array variable.

For example, an int array of 10 elements can be allocated as follows:


int * array = (int *) calloc (10, sizeof (int));

Note that this function can also malloc, written as follows.


int * array = (int *) malloc (sizeof (int) * 10);

Example-1: calloc () function Output:


#include <stdio.h> Number of elements to be entered:3
#include <stdlib.h> Enter 3 numbers:
#include <conio.h> 22
int main() 55
14
{ The numbers entered are: 22 55 14
int i, n;
int *a;
printf("Number of elements to be entered:");
scanf("%d",&n);
a = (int*)calloc(n, sizeof(int));
printf("Enter %d numbers:\n",n);
for( i=0 ; i < n ; i++ )
{
scanf("%d",&a[i]);
}
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ ) {
printf("%d ",a[i]);
}
getch();
return(0);
}
Example-2: calloc () function to allocate memory storage space dynamically.
#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
int main()
{
int num;
int *ptr;
printf("Enter the number of type int to allocate: ");
scanf("%d", &num);
ptr = (int*)calloc(num, sizeof(int));
if (ptr != NULL)
puts("Memory allocation was successful.");
else
puts("Memory allocation failed."); getch();
return(0);
}

Output:
Enter the number of type int to allocate: 100
Memory allocation was successful.
Enter the number of type int to allocate: 99999999
Memory allocation failed.

realloc(void *ptr, size_t size) :

The c library function void realloc() attempts to resize the memory block pointed to by ptr that was
previously allocated with a call to malloc() or calloc() function.

void free(void *ptr) :

The c library function void free() deallocates the memory previously created by malloc() or realloc() or
calloc() functions.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *ptr;
int n;
printf("enter the size of memroy");
scanf("%d",&n);
ptr=(char *) malloc(n);
strcpy(ptr,"lict");
printf("enter again the size of memroy");
scanf("%d",&n);
ptr=(char *) realloc(ptr,n);
strcat(ptr,".edu.np");
printf("\n entire string is %s",ptr);
free(ptr);
getch();
}

You might also like