Unit 1 Notes
Unit 1 Notes
Abstract Data Types (ADTs) – List ADT – array-based implementation – linked list implementation
––singly linked lists- circularly linked lists- doubly-linked lists– applications of lists –Polynomial
Manipulation – All operation (Insertion, Deletion, Merge, Traversal)
Data:
1.Traversal
2.Search
3.Insertion
4.Deletion
CLASSIFICATION OF DATA STRUCTURES
Static Ds:
If a ds is created using static memory allocation, ie. ds formed when the number of data items
are known in advance ,it is known as static data static ds or fixed size ds.
Dynamic Ds:
If the ds is created using dynamic memory allocation i.e ds formed when the number of data
items are not known in advance is known as dynamic ds or variable size ds.
An abstract Data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form a example of an ADT. An ADT consists of data together with functions that
operate on that data.
Advantages/Benefits of ADT:
1.Modularity
2.Reuse
3.code is easier to understand
4.Implementation of ADTs can be changed without requiring changes to the program that uses the
ADTs.
If the element at position i is Ai, then its successor is Ai+1 and its predecessor is Ai-1
Various operations performed on List
Insertion and Deletion operation are expensive as it requires more data movements
Insert Operation:
Insert operation is used to insert an element at particular position in the existing list. Inserting the
element in the last position of an array is easy. But inserting the element at a particular position in
an array is quite difficult since it involves all the subsequent elements to be shifted one position
to the right.
Routine to insert an element in the array:
void Insert( )
{ int i,data,pos;
printf("\nEnter the data to be inserted:\t");
scanf("%d",&data);
printf("\nEnter the position at which element to be
inserted:\t"); scanf("%d",&pos); if (pos==n)
printf (“Array overflow”);
for(i = n-1 ; i >= pos-1 ; i--)
list[i+1] = list[i];
list[pos-1] = data; n=n+1;
Display();}
Consider an array with 5 elements [ max elements = 10 ]
10 20 30 40 50
If data 15 is to be inserted in the 2nd position then 50 has to be moved to next index position, 40
has to be moved to 50 position, 30 has to be moved to 40 position and 20 has to be moved to 30
position.
10 20 30 40 50
10 20 30 40 50
After this four data movement, 15 is inserted in the 2nd position of the array.
10 15 20 30 40 50
Deletion Operation:
Deletion is the process of removing an element from the array at any position.
Deleting an element from the end is easy. If an element is to be deleted from any particular position
,it requires all subsequent element from that position is shifted one position towards left.
Routine to delete an element in the array: void
Delete( )
{ int i, pos ; printf("\nEnter the position of the data to be
deleted:\t"); scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", list[pos-1]);
for(i=pos-1;i<n-1;i++) list[i]=list[i+1];
n=n-1;
Display();
}
Consider an array with 5 elements [ max elements = 10 ]
10 20 30 40 50
If data 20 is to be deleted from the array, then 30 has to be moved to data 20 position, 40 has to
be moved to data 30 position and 50 has to be moved to data 40 position.
After this 3 data movements, data 20 is deleted from the 2nd position of the array.
10 20 30 40 50
10 30 40 50
Search Operation:
Search( ) operation is used to determine whether a particular element is present in the list or not.
Input the search element to be checked in the list.
Routine to search an element in the array:
void Search( )
{
int search,i,count = 0;
printf("\nEnter the element to be
searched:\t"); scanf("%d",&search);
for(i=0;i<n;i++)
{ if(search == list[i])
count++;
}
if(count==0)
printf("\nElement not present in the list");
else
printf("\nElement present in the list");
}
1.create
2.Insert
3.Delete
4.Display
5.Search
6.Exit
Enter your choice: 1
1 2 3 4 5
1.create
2.Insert
3.Delete
4.Display
5.Search
6.Exit
Enter your choice: 2
3 1 2 3 4 5
3 1 2 4 5
1.create
2.Insert
3.Delete
4.Display
5.Search
6.Exit
Linked Lists:
A Linked list is an ordered collection of elements. Each element in the list is referred as a node.
Each node contains two fields namely,
Data field-The data field contains the actual data of the elements to be stored in the list
Next field- The next field contains the address of the next node in the list
DATA NEXT
L
Advantages of Linked list:
1.Insertion and deletion of elements can be done efficiently
2.It uses dynamic memory allocation
3.Memory utilization is efficient compared to arrays
Disadvantages of linked list:
1.Linked list does not support random access
2.Memory is required to store next field
3.Searching takes time compared to arrays
Types of Linked List
1. Singly Linked List or One Way List
2. Doubly Linked List or Two-Way Linked List
3. Circular Linked List
Dynamic allocation
The process of allocating memory to the variables during execution of the program or at run time
is known as dynamic memory allocation. C language has four library routines which allow this
function.
Dynamic memory allocation gives best performance in situations in which we do not know memory
requirements in advance. C provides four library routines to automatically allocate memory at the
run time.
To use dynamic memory allocation functions, you must include the header file stdlib.h.
malloc()
The malloc function reserves a block of memory of specified size and returns a pointer of
type void. This means that we can assign it to any type of pointer. The general syntax of
malloc() is ptr =(cast-type*)malloc(byte-size);
where ptr is a pointer of type cast-type. malloc() returns a pointer (of cast type) to an area of memory
with size byte-size.
calloc(): calloc() function is another function that reserves memory at the run time. It is normally
used to request multiple blocks of storage each of the same size and then sets all bytes to zero.
calloc() stands for contiguous memory allocation and is primarily used to allocate memory for
arrays. The syntax of calloc() can be given as: ptr=(cast-type*) calloc(n,elem-size);
The above statement allocates contiguous space for n blocks each of size elem-size bytes. The only
difference between malloc() and calloc() is that when we use calloc(), all bytes are
initialized to zero. calloc() returns a pointer to the first byte of the allocated region.
free():
The free() is used to release the block of memory.
Syntax:
The general syntax of the free()function is,
free(ptr);
where ptr is a pointer that has been created by using malloc() or calloc(). When memory is
deallocated using free(), it is returned back to the free list within the heap.
realloc():
At times the memory allocated by using calloc() or malloc() might be insufficient or in excess.
In both the situations we can always use realloc() to change the memory size already allocated
by calloc() and malloc(). This process is called reallocation of memory. The general syntax for
realloc() can be given as, ptr = realloc(ptr,newsize);
The function realloc() allocates new memory space of size specified by newsize to the pointer
variable ptr. It returns a pointer to the first byte of the memory block. The allocated new block may
be or may not be at the same region. Thus, we see that realloc() takes two arguments. The first is the
pointer referencing the memory and the second is the total number of bytes you want to reallocate.
A singly linked list is a linked list in which each node contains only one link field pointing to the
next node in the list
SLL
SLL with a Header
Basic operations on a singly-linked list are:
1. Insert – Inserts a new node in the list.
2. Delete – Deletes any node from the list.
3. Find – Finds the position( address ) of any node in the list.
4. FindPrevious - Finds the position( address ) of the previous node in the list.
5. FindNext- Finds the position( address ) of the next node in the list.
6. Display-display the date in the list
7. Search-find whether a element is present in the list or not
Declaration of Linked List void insert(int
X,List L,position P); void find(List
L,int X); void delete(int x , List L);
typedef struct node *position;
position L,p,newnode; struct node
{
int data; position
next;
};
Creation of the list:
This routine creates a list by getting the number of nodes from the user. Assume n=4 for
this example. void create()
{ int
i,n;
L=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the number of nodes to be
inserted\n"); scanf("%d",&n); printf("\n Enter the
data\n"); scanf("%d",&newnode->data); newnode-
>next=NULL; L=newnode; p=L; for(i=2;i<=n;i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newnode->data); newnode-
>next=NULL;
p->next=newnode; p=newnode;
}
}
Initially the list is empty
List L
Null
L
Insert(10,List L)- A new node with data 10 is inserted and the next field is updated to NULL.
The next field of previous node is updated to store the address of new node.
10 Null
L
P
Insert(20,L) - A new node with data 20 is inserted and the next field is updated to NULL.
The next field of previous node is updated to store the address of new node.
10 20 Null
L P
Insert(30,L) - A new node with data 30 is inserted and the next field is updated to NULL. The
next field of previous node is updated to store the address of new node.
10 20 30 Null
L P
Case 1:Routine to insert an element in list at the beginning
void insert(int X, List L, position p) {
p=L;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data); newnode-
>next=L;
L=newnode;
}
Case 2:Routine to insert an element in list at Position
This routine inserts an element X after the position P.
Void Insert(int X, List L, position p)
{ position newnode;
newnode =(struct node*) malloc( sizeof( struct node ));
if( newnode = = NULL )
Fatal error( “ Out of Space ” );
else
Insert(25,L, P) - A new node with data 25 is inserted after the position P and the next field is
updated to NULL. The next field of previous node is updated to store the address of new node.
Case 3:Routine to insert an element in list at the end of the list void
insert(int X, List L, position p)
{
p=L;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data); while(p-
>next!=NULL)
p=p->next;
newnode->next=NULL; p-
>next=newnode; p=newnode;
}
Routine to check whether a list is Empty
This routine checks whether the list is empty .If the lis t is empty it returns 1
10 40 20 30 Null
Find(List L, 20) - To find an element X traverse from the first node of the list and move to the
next with the help of the address stored in the next field until data is equal to X or till the end of
the list
10 40 20 30 Null
X P
Find Previous
It returns the position of its predecessor.
position FindPrevious (int X, List L)
{ position p; p =
L;
while( p -> next ! = NULL && p -> next -> data! = X )
p = p -> next;
return P;
}
{
count++;
p = p -> next;
}
print count;
}
void Delete_list(List L)
{ position P,temp;
P=L->next; L-
>next=NULL;
while(P!=NULL
)
{ temp=P->next;
free(P);
P=temp;
}
}
3
create(); 4
break;
case 2: display();
5
break;
case 3:
insert(); 1.create
break;
case 4: 2.display
find();
break;
case 5: delete(); 3.insert
break;
case 6: exit(0); 4.find
}
} 5.delete
3
Enter ur choice
1.first
2.middle
3.end
p=p->next;
}
if(count==0) printf("\n Element
Not present\n"); else
printf("\n Element present in the list \n\n");
}
void delete()
{ position
p,temp; int x;
p=L;
if(p==NULL)
{
printf("empty list\n");
} else
{
printf("\nEnter the data to be deleted\n");
scanf("%d",&x);
if(x==p->data) {
temp=p; L=p-
>next;
free(temp);
display();
} else
{
while(p->next!=NULL && p->next->data!=x)
{
p=p->next;
}
temp=p->next; p->next=p->next->next;
free(temp);
display();
}}
}
Advantages of SLL
DLL NODE
Initially the list is empty. Then assign the first node as head.
newnode->data=X; newnode->next=NULL;
newnode->prev=NULL;
L=newnode;
list.
If we add one more node in the list,then create a newnode and attach that node to the end of the
L->next=newnode; newnode->prev=L;
*Newnode; if(pos==1)
P=L;
L->next ->prev=Newnode
L->next = Newnode;
Newnode ->prev = L;
}
Routine to insert an element in a DLL any position :
{ struct node
*Newnode;
P->next ->prev=Newnode
P ->next = Newnode;
Newnode ->prev = P:
}
Routine to insert an element in a DLL at the end:
Enter ur option1
2.DELETE
3.DISPLAY
4.FIND
5.EXIT
3.DISPLAY
4.FIND
5.EXIT
Enter ur option 2
}
Enter the position of the
data to be deleted 2
if(pos==1) 5.EXIT
{ temp=L;
L=temp->next; L->prev=NULL;
Enter ur option 3
printf(“\nThe deleted element is %d”,temp->data); free(temp);
} else
{
P=L;
for(i=1;i<pos-1;i++) P=P->next; temp=P->next; The elements in the list
printf(“\nThe deleted element is %d”,temp->data); are
P->next=temp->next; temp->next->prev=P;
free(temp); 10 30
}}
} 1.INSERT
void display()
{ 2.DELETE
if(L==NULL)
printf(“\nNo of elements in the list”);
else { 3.DISPLAY
printf(“\nThe elements in the listare\n”); for(P=L;P!=NULL;P=P-
>next) 4.FIND
printf(“%d”,P->data);
}} 5.EXIT
void find()
{
Enter ur option4
int a,flag=0,count=0;
1.INSERT
2.DELETE
3.DISPLAY
4.FIND
5.EXIT
Enter ur option 4
if(L==NULL)
printf(“\nThe list is Enter the elements to be
empty”); else {
searched 30
printf(“\nEnter the elements to be searched”);
scanf(“%d”,&a);
for(P=L;P!=NULL;P=P->next)
{ count++; The element is found
if(P->data==a)
{ flag=1; The position is 2
printf(“\nThe element is found”);
printf(“\nThe position is %d”,count);
1.INSERT
break;
}}
if(flag==0) 2.DELETE
printf(“\nThe element is not found”);
}} 3.DISPLAY
4.FIND
5.EXIT
Enter ur option5
..
Advantages of DLL:
The DLL has two pointer fields. One field is prev link field and another is next link field.
Because of these two pointer fields we can access any node efficiently whereas in SLL only one
link field is there which stores next node which makes accessing of any node difficult.
Disadvantages of DLL:
The DLL has two pointer fields. One field is prev link field and another is next link field.
Because of these two pointer fields, more memory space is used by DLL compared to SLL
Circular Linked list is a linked list in which the pointer of the last node points to the first node.
Types of CLL:
CLL can be implemented as circular singly linked list and circular doubly linked list.
A Singly linked circular list is a linked list in which the last node of the list points to the first
node.
Declaration of node:
struct node
{ int data;
position
next;
};
void del_first(List L)
{ position temp;
temp=L->next; L-
>next=temp->next;
free(temp);
}
Routine to delete an element from the middle
CS6202 42 PDS-1NOTES
Routine to delete an element at the last position
void del_last(List L)
{
position p, temp;
p=L;
while(p->next->next!=L)
p=p->next; temp=p-
>next; p->next=L
free(temp);}
Doubly Linked circular list:
A doubly linked circular list is a doubly linked list in which the next link of the last node points to
the first node and prev link of the first node points to the last node of the list.
Declaration of node:
void del_first(List L)
{
position temp;
if(L->next!=NULL)
{
temp=L->next; L->next=temp->next;
temp->next->prev=L;
free(temp);
}
Routine to delete an element from the middle
void del_last(List L)
{
position p, temp;
p=L;
while(p->next!=L)
p=p->next; temp=p;
p->next->prev=L;
L->prev=p->prev;
free(temp);
}
Advantages of Circular linked List
Applications of List:
1.Polynomial ADT
2.Radix sort
3.Multilist
Polynomial Manipulation
Polynomial manipulations such as addition, subtraction & differentiation etc.. can be
performed using linked list
Declaration for Linked list implementation of Polynomial ADT
struct poly { int
coeff; int power;
struct poly *next;
}*list1,*list2,*list3;
{
ptr=head1; while(ptr-
>next!=NULL) ptr=ptr->next; ptr-
>next=newnode1;
}
return(head1);
}
Addition of two polynomials void
add()
{ poly *ptr1, *ptr2, *newnode ;
ptr1= list1; ptr2 = list2;
while( ptr1 != NULL && ptr2 != NULL )
{ newnode = (struct poly*)malloc( sizeof ( struct poly ));
ptr1=ptr1->next; ptr2=ptr2-
>next;
}
else
{
if(ptr1-power>ptr2-power)
{ newnode->coeff=ptr1->coeff;
newnode->power=ptr1-
>power;
newnode->next=NULL;
list3=create(list3,newnode); ptr1=ptr1->next;
}
else
{ newnode->coeff=-(ptr2->coeff);
newnode->power=ptr2->power;
newnode->next=NULL;
list3=create(list3,newnode); ptr2=ptr2->next;
}
}
}
Polynomial Differentiation:
void diff()
list3=create(list3,newnode); ptr1=ptr1->next;
}
}
Polynomial Multiplication void
mul()