Professional Documents
Culture Documents
Different kinds of data structures are suited to different kinds of applications, and some are
highly specialized to specific tasks. For example, B-trees are particularly well-suited for
implementation of databases, while compiler implementations usually use hash tables.
Data structures are used in almost every program or software system. Data structures provide
a means to manage huge amounts of data efficiently, such as large databases. Usually,
efficient data structures are a key to designing efficient algorithms. Some formal design
methods and programming languages emphasize data structures, rather than algorithms, as
the key organizing factor in software design.
Data structures are generally based on the ability of a computer to fetch and store data at any
place in its memory, specified by an address a bit string that can be itself stored in memory
and manipulated by the program. Thus the struct and array data structures are based on
computing the addresses of data items with arithmetic operations; while the linked data
structures are based on storing addresses of data items within the structure itself.
The implementation of a data structure usually requires writing a set of procedures that create
and manipulate instances of that structure. The efficiency of a data structure cannot be
analyzed separately from those operations. This observation motivates the theoretical concept
of an abstract data type, a data structure that is defined indirectly by the operations that may
be performed on it.
Common data structures include: array, linked list, hash-table, heap, Tree (Binary Tree, Btree, red-black tree, trie), stack, and queue.
Array-Based Implementations
Recall that
o an array is a named collection of homogeneous items
o An items place within the collection is called an index
If there is no ordering on the items in the container, we call the container unsorted
Linked Implementation
Lists
List operations
o Create itself
o Insert an item
o Delete an item
o Print itself
o Know the number of items it contains
Sorting
Selection Sort
List of names
o Put them in alphabetical order
Continue this cycle until all the names on the original list have been
crossed out and written
onto the second list, at which point the second list is sorted
A slight adjustment to this manual approach does away with the need to duplicate
space
o As you cross a name off the original list, a free space opens up
o Instead of writing the minimum value on a second list, exchange it with the
value currently in the position where the crossed-off item should go
Bubble Sort
A selection sort that uses a different scheme for finding the minimum value
o Starting with the last list element, we compare successive pairs of elements,
swapping whenever the bottom element of the pair is smaller than the one
above it
Searching
A sequential search of a list begins at the beginning of the list and continues until the
item is found or the entire list has been searched
Binary Search
o If the item for which we are searching is less than the item in the middle, we
know that the item wont be in the second half of the list
o Once again we examine the middle element (which is really the item 25% of
the way into the list)
o The process continues with each comparison cutting in half the portion of the
list where the item might be
Stacks
A stack is an abstract data type in which accesses are made at only one end
o LIFO, which stands for Last In First Out
o The insert is called Push and the delete is called Pop
Queues
A Queue is an abstract data type in which items are entered at one end and removed
from the other end
o FIFO, for First In First Out
o Like a waiting line in a bank or supermarket
o No standard queue terminology
A fast way to search a sorted array is to use a binary search. The idea is to look at the element
in the middle. If the key is equal to that, the search is finished. If the key is less than the
middle element, do a binary search on the first half. If it's greater, do a binary search of the
second half.
Pseudocode:
BinarySearch(array, size, key)
BEGIN
low = 0
high = size
while (low + 1 < high)
test = (low + high)/2
if (array[test] > key)
high = test
else
low = test
endwhile
if (array[low] = = key
return low
else
return -1
END
C Program:
1.
#include<stdio.h>
2.
#include<conio.h>
3.
void main()
4.
5.
int a[10],n,i;
6.
int low,high,mid,key;
7.
clrscr();
8.
9.
scanf(%d",&n);
printf(enter the array elements in ascending or
descending order:" );
10.
11.
for(i=0;i<n;i++)
12.
scanf(%d",&a[i]);
13.
14.
low=0;
15.
high=9;
16.
mid=(low+high)/2;
17.
18.
scanf(%d",&key);
19.
20.
if(key<a[mid])
21.
high=mid-1;
22.
23.
else
24.
low=mid+1;
25.
mid=(low+high)/2;
26.
27.
If(a[mid]==key)
{
28.
29.
30.
31.
else
{
32.
33.
34.
35.
getch();
36.
BEGIN
for i = 1 to n-1
min_subsrcipt = i
min _value = array[i]
for j = i + 1 to n - 1
if (array[j] < min_value)
min_subscript = j
min_value = array[j]
endif
endfor
array[min_subscript] = array[i]
array[i] = min_value
endfor
END
Implementation
void
selectionSort(int numbers[], int array_size) { int i, j; int min, temp; for (i = 0; i <
array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] <
numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min];
numbers[min] = temp; } }
Pseudocode:
BubbleSort
BEGIN
for i = n - 1 to 1
for j = 1 to i
if (array[j] > array[j+1])
swap(array[j], array[j+1])
endif
endfor
endfor
END
C Program:
1. #include<stdio.h>
2. #include<conio.h>
3.
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}//end for 1.
}//end function.
void main()
int a[100],n,i;
clrscr();
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.
%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
} //end program.
/*
--------SAMPLE OUTPUT---------------------
Enter integer value for element no.3 : -67
-----------------------------------------
*/
}l;
int
int
int
int
list[MAX];
element;//new element to be inserted
pos; //position of the element to be inserted or deleted
length; //total no of elements
case 2:
l.length = 0;
create();
break;
if (islistfull() != true)
{
printf("\tEnter the New element : ");
scanf("%d", &element);
printf("\tEnter the Position : ");
scanf("%d", &pos);
insert(element, pos);
}
else
{
insert");
continue...");
getch();
}
break;
case 3:
to be deleted :
if (islistempty() != true)
{
printf("Enter the position of element
");
scanf("%d", &pos);
delet(pos);
}
else
{
printf("List is Empty.");
printf("\nPress any key to
continue...");
getch();
}
break;
case 4:
l.length);
case 5:
");
case 6:
case 7:
display();
break;
exit(0);
break;
default:
printf("Invalid Choice");
printf("\nPress any key to continue...");
getch();
}
}
//function to display the list of elements
int menu()
{
int ch;
clrscr();
printf("\n\t\t********************************************\n");
l.list[pos-1] = element;
l.length++;
}
else
return false;
}
//function to check whether the list is empty or not
boolean islistempty(void)
{
if (l.length == 0)
return true;
else
return false;
}
Sample Output:
1. Create
2. Insert
3. Delete
4. Count
5. Find
6. Display
7. Exit
Enter your Choice: 1
Enter an element: 25
To insert another element press 1: 1
Enter an element: 50
To insert another element press 1: 0
Enter your Choice: 6
Element in the List are
1. 25
2. 50
Enter your Choice: 2
Enter an element: 5
Enter the position: 1
Element in the List are
1. 25
2. 50
Enter your Choice: 3
Enter the position: 3
Enter your Choice: 6
Element in the List are
1. 5
2. 25
Enter your Choice: 4
Number of Elements: 2
Enter your Choice: 7
# define MAX 5
int
int
int
int
queue_arr[MAX];
rear = -1;
front = -1;
count = 0;
int main()
{
int choice;
while(1)
{
printf("1.Enqueue\n");
printf("2.Dequeue\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
enqueue();
break;
case 2 :
dequeue();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main()*/
enqueue()
{
int added_item;
if (count==MAX)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
if (rear==MAX)
rear=0;
queue_arr[rear] = added_item ;
count++;
}
}/*End of insert()*/
dequeue()
{
if (front == -1 || count==0)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
if (front==MAX)
front=0;
count--;
}
}/*End of del() */
display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of display() */
queue_arr[MAX];
rear = -1;
front = -1;
count = 0;
int main()
{
int choice;
while(1)
{
printf("1.Enqueue\n");
printf("2.Dequeue\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
enqueue();
break;
case 2 :
dequeue();
case 3:
break;
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main()*/
enqueue()
{
int added_item;
if (count==MAX)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
count++;
}
}/*End of insert()*/
dequeue()
{
if (count == 0)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
count--;
if (count == 0)
front = -1;
else
for(i=0; i<rear; i++)
queue_arr[i] = queue_arr[i+1]
rear=rear-1;
}
}/*End of del() */
display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of display() */
pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/