Bridge Course-DataStructures - Unit - 1
Bridge Course-DataStructures - Unit - 1
L Abstract Data Types (ADTs): List ADT, Arraybased implementation, Linked list
implementation, Singly linked lists, applications of lists: Polynomial Manipulation,
All operations (Insertion, Deletion, Merge, Traversal).
Data Structures
Data Structures is a way of organizing the data which also includes several operations
to perform on that data.
2
The basic operations performed are
Creation of List.
Insertion / Deletion of data from the List
Display all data in the List
Searching for a data in the list
Creation of List: Create operation is used to create the list with “n” number of elements.
If “n” exceeds the array’s maxsize, then elements cannot be inserted into the list.
Otherwise the array elements are stored in the consecutive array locations (i.e.) list [0],
list [1] and so on.
Insertion / Deletion of data in the List: Insert / Delete operation is used to Insert/ Deletean
element at particular position in the existing list. Inserting/ deleting the element in thelast
position of an array is easy.
But inserting/ Deleting 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 / left.
Display/ Traversal all data in the List: Traversal is the process of visiting the elements
in an array. Display( ) operation is used to display all the elements stored in the list from
the index 0 to n-1.
Searching for a data in the list: Search( ) operation is used to determine whether a
particular element is present in the list or not.
3
Procedure to create / insert / delete
Create Insert Delete
void Insert() void Delete( )
void Create() { int i,pos;
{ int i, data, pos; printf("Enter
{ int i; printf("Enter the
data to insert");scanf("%d",
printf("Enter number of position to delete”);
elements"); &data);
scanf("%d", &pos);
scanf("%d",&n); printf("Enter the position to
insert"); printf("The data
printf("Enter the array deleted is: %d",
elements"); scanf("%d", &pos); list[pos-1]);
for(i=0; i<n; i++) for(i = n-1; i >= pos-1 ; i--) for(i=pos-1;i<n-1;
{scanf("%d", &list[i]); } {list[i+1] = list[i];} i++)
// swap // {list[i]=list[i+1]; }
Display();
list[pos-1] = data; n=n-1;
}
n= n+1; Display(); } Display(); }
Procedure to display / search
Display Search
void Display() void Search()
{ { int search, i, count = 0;
int i; printf("Enter the element to be searched");scanf("%d",
printf("Elements in the &search);
array"); for(i=0; i<n; i++)
for(i=0;i<n;i++) { if(search == list[i])
printf("%d",list[i]); count++; }
} if(count==0)
printf("Element not present ");else
printf("Element present"); }
4
Linked List based implementation
Linked list is an ordered collection of elements. Each element in the list is referred
as a node. Each node contains two fields namely,
1. Data field-The data field contains the actual data of the elements to be stored in the
list.
2. Next / Pointer field- The next field contains the address of the next node in the list
The nodes in the linked list are not necessarily adjacent in memory.
5
}
6
7. Search - finds whether an element is present in the list or not
Insertion: The insertion into a singly linked list can be performed in 3 ways.
1. Insertion at beginning: It involves inserting any element at the front of the list.
Fig 1.6 Inserting a new element into a Singly Linked List at beginning
2. Insertion at end of the list: It involves insertion at the last of the linked list. It can
be done in 2 ways
a. The node is being added to an empty list (only one node in the list)
b. The node can be inserted as the last one.
Fig 1.7 Inserting a new element into a Singly Linked List at end of the list
7
3. Insertion at a given position (after specified node): It involves insertion after the
specified node of the linked list.
8
else temp=head;
{temp = head; for(i=0;i<loc;i++)
while (temp next != { temp = tempnext;
NULL) if(temp == NULL)
{temp = temp next; } {printf("can't insert");
tempnext = ptr; return; }
ptrnext = NULL; }
printf("Node inserted");} ptr next =
} tempnext;
} temp next = ptr;
printf("Node inserted");
}
}
Deletion: The deletion into a singly linked list can be performed in 3 ways.
1. Deletion at beginning: deleting an element at the front of the list.
3. Deletion at a given position (after specified node): It involves deletion after the
specified node of the linked list.
10
free(ptr); free(head); printf("Only for(i=0;i<loc;i++)
printf("Node deleted from the node of the list deleted {ptr1 = ptr;
begining ..."); ..."); }
ptr = ptrnext;
} else
if(ptr == NULL)
} {ptr = head;
{printf("Can't delete");
while(ptrnext != NULL)
return; }
{ptr1 = ptr;
}
ptr = ptr next; }
ptr1 next = next;
ptr1next = NULL;
free(ptr); printf("Deleted
free(ptr); node %d ", loc+1);
printf("Deleted Node from }
the last ...");
}
}
11
if(ptr == NULL) if(ptr == NULL)
{ printf("Empty List "); } {printf("Empty List"); }
else else
{ printf("printing values"); {printf("Enter item to search");
while (ptr!=NULL) scanf("%d",&item);
{printf("%d",ptrdata); while (ptr!=NULL)
ptr = ptr -> next; } {if(ptrdata == item)
} {printf("item found at location %d ",i+1);
} flag=0; } else { flag=1; }
i++; ptr = ptr -> next; }
if(flag==1)
{ printf("Item not found\n"); } } }
Applications of lists:
1. Polynomial ADT
2. Radix sort
3. Multilist
A polynomial p(x) is the expression in variable x which is in the form (axn + bxn-1 + ….
+ jx + k), where a, b, c …., k fall in the category of real numbers and 'n' is non negative
integer, which is called the degree of polynomial.
value 12
struct poly{
{ int coeff;
int power;
Creation of Polynomial:
{ poly*ptr;
if(head==NULL)
{ head=newnode;
return(head); }
13
else
{ ptr=head;
while(ptr-> next!=NULL)
ptr=ptr->next;
ptr->next=newnode; }
return(head);
}
Addition of Polynomials:
To add two polynomials we need to scan them once. If we find terms with the same
exponent in the two polynomials, then we add the coefficients; otherwise, we copy the
term of larger exponent into the sum and go on. When we reach at the end of one of the
polynomial, then remaining part of the other is copied into the sum.
• Add them.
Addition of Polynomials:
void add()
ptr1=list1;
ptr2=list2;
{ newnode=malloc(sizeof(struct poly));
14
if(ptr1->power==ptr2->power)
{ newnode->coeff = ptr1->coeff + ptr2->coeff;
newnode->power=ptr1->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr->next;
ptr2=ptr2->next;
}
else
{ 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; }
}
15
SUBTRACTION OF POLYNOMIALS:
(add this statement in the above program)
MULTIPLICATION OF POLYNOMIALS:
Multiplication of two polynomials however requires manipulation of each node
such that the exponents are added up and the coefficients are multiplied.
After each term of firstpolynomial is operated upon with each term of the second
polynomial,
Then the result has to be added up by comparing the exponents and adding the
coefficients for similar exponents and including terms as such with dissimilar
exponents in the result
void Mul()
ptr1=list1;
ptr2=list2;
return;
list3 = list2;
elsif(ptr2 ==NULL) // II polynomial does not exist
list3 =list1;
16
{ while(ptr1!=NULL)
{ newnode=malloc(sizeof(struct poly));
while(ptr2!=NULL)
newnode->power=ptr1->power + ptr2->power;
list3=create(list3,newnode);
ptr2=ptr2->next;
}
ptr2=list2;
ptr1=ptr1->next;
} }
}}
17