Unit - 4 list
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.
Array implementation
Here each element can be found through index number of array and is incremented by 1
When index is = 0
Then
Index=index+1
Then
Arr[index]=20;
In this way we can transverse each element of array by increment the index by 1 .
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];
Two ways:
Deletion in between
Traverse the array last and if the item is last of the array, then delete that
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
Int temp,position,item,n;
If(n==0){
Printf(“list is underflow”);
Return ;
If(item=arr[n-1])
N=n-1;
Return;
Temp=position-1;
While(temp<=n-1)
Arr[temp]=arr[temp+1];
Temp++;
}
N=n-1;
Advantage of list
Dis-advantage
A. Function: malloc ()
The C library function void *malloc(size_in_bytes) allocates the requested
memory and returns a pointer to it.
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]);
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.
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.
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.
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();
}