Professional Documents
Culture Documents
Data Structure Lab Programs
Data Structure Lab Programs
In computer science, a stack is a last in, first out(LIFO) abstract data type and
data structure. A stack can have any abstract data type as an element, but is
characterized by only two fundamental operations, the push and the pop.
(or)
A collection of items in which only the most recently added item may be
removed. The latest added item is at the top. Basic operations are push and pop.
Often top and is Empty are available, too. Also known as "last-in, first-out" or
LIFO.
Push:
The push operation adds to the top of the list, hiding any items already on the
stack, or initializing the stack if it is empty.
Pop:
The pop operation removes an item from the top of the list, and returns this
value to the caller. A pop either reveals previously concealed items, or results in
an empty list.
Stack Applications:
1. Expression evaluation.
Alogarithm steps:
1. Expression evaluation.
Alogarithm steps:
ii) Read the stack element and store it in top's data area.
b) Otherwise assign top as temp (i.e. top=temp, bring the top to top position)
c) Assign temp as temp's link. (i.e. temp=temp->link, bring the temp to top's
previous position).
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
/* Node decleration */
struct node
{
int data;
struct node *link; //to maintain the link other nodes
};
struct node *top,*temp;
void create();
void push();
void pop();
void display();
/* create function create the head node */
void create()
{
printf("\nENTER THE FIRST ELEMENT: ");
top=(struct node *)malloc(sizeof(struct node));
scanf("%d",&top->data);
top->link=NULL;
temp=top;
}
void pop()
{
if(temp==NULL)
{
printf("\nSTACK IS EMPTY\n");
}
else
{
top=temp;
printf("\nDELETED ELEMENT IS %d\n",temp->data);
temp=temp->link;
free(top);
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n\n 1.CREATE \n 2.PUSH \n 3.POP \n 4.EXIT \n");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
push();
display();
break;
case 3:
pop();
display();
break;
case 4:
exit(0);
}
}
}
SAMPLE INPUT AND OUTPUT:
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE : 1
ENTER THE FIRST ELEMENT : 10
10
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 2
ENTER THE NEXT ELEMENT: 30
10
30
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
DELETED ELEMENT IS 30
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
DELETED ELEMENT IS 10
STACK
1. CREATE
2. PUSH
3. POP
4. EXIT
ENTER YOUR CHOICE: 3
STACK IS EMPTY.
Definition:
A variant of a linked list in which each item has a link to the previous item as well as the next.
This allows easily accessing list items backward as well as forward and deleting any item in
constant time.
Also known as two-way linked list, symmetrically linked list.
A doubly linked list is a linked list in which each node has three fields namely data field,
forward link (Flink) and backward link (Blink). Flink points to the successor node in the list
whereas Blink points to the predecessor node.
Advantages:
Disadvantages :
Alogrithm steps:
1. Create the new node and allocate memory for the new node.
1. Create the new node and allocate memory for the new node
5. Assign new->Flink=current
1. Create the new node and allocate memory for the new node
1. Create the new node and allocate memory for the new node.
4. Current=current->Flink.
4. Current=current->Flink.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define NULL 0
struct linkedlist
{
int item;
struct linkedlist *right,*left;
};
typedef struct linkedlist node;
void main()
{
node *start,*end;
int choice;
int menu(void);
node *create(node **lastnode);
void display(node *first,node *last);
void insert(node **first,node **last);
void del(node **first,node **last);
clrscr();
printf("\n DOUBLY LINKED LIST");
printf("\n ******************");
do
{
printf("\n\nMain menu");
printf("\n\n1.Create \n2.Insert \n3.Delete \n4.Display \n5.Exit");
choice =menu();
switch(choice)
{
case 1:
printf("\n Enter the data(-999 to stop):");
start=create(&end);
continue;
case 2:
insert(&start,&end);
printf("\n");
continue;
case 3:
del(&start,&end);
printf("\n");
continue;
case 4:
display(start,end);
printf("\n");
continue;
case 5:
exit(0);
default:
printf("\n\nINVALID CHOICE...");
}
}while(1);
}
int menu()
{
int choice;
do
{
printf("\n Enter your choice:");
scanf("%d",&choice);
if(choice<1||choice>5)
printf("\n Wrong choice");
}while(choice<1||choice>5);
printf("\n");
return(choice);
}
Main menu
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:1
Main menu
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:2
Main menu
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:4
Forward traversal
5 20 10 15 20
Backward traversal
20 15 10 20 5
Main menu
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:3
Main menu
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:4
Forward traversal
20 10 15 20
Backward traversal
20 15 10 20
Implementation Of Binary Search :
Binary search:
Definition:
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of
the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.
Advantages:
Disadvantages:
1. Binary search can interact poorly with the memory hierarchy (i.e. caching), because
of its random-access nature.
2. Before searching the elements shorting is necessary.It take some additional
overhead to machine.
Algorithm steps:
Step 4: Look at the element in the middle. If the key is equal to that, the search is
finished.
Step 5: If the key is less than the middle element, do a binary search on the first half.
Step 6: If it's greater, do a binary search of the second half.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],i,j,temp,s,n,low,mid,high;
clrscr();
printf("\nEnter the Limilt : ");
scanf("%d",&n);
printf("\n\nEnter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\nSorted list");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
printf("\n\nEnter the elements to be searched : ");
scanf("%d",&s);
high=n-1;
low=0;
while(low<=high)
{
mid=(low+high)/2;
if(s>a[mid])
low=mid+1;
else if(s<a[mid])
high=mid-1;
else if(s==a[mid])
{
printf("\n\nThe element %d is found",s);
getch();
exit(0);
}
}
printf("\n\nThe element %d is not found",s);
getch();
}
Sorted list
1
2
3
4
5
Infix Expression:
Notation in which the operator separates its operands. Eg (a + b) * c. Infix notation requires
the use of brackets to specify the order of evaluation.
Postfix Expression:
Reverse Polish Notation or Suffix Notation in which the operator follows its operands. Eg a +
b * c represented as abc*+.
Algorithm steps:
3. If the scanned character is an operand, add it to the Postfix string. If the scanned
character is an operator and if the stack is empty push the character to stack.
4. If the scanned character is an Operator and the stack is not empty, compare the
precedence of the character with the element on top of the stack (top Stack). If top
Stack has higher precedence over the scanned character pop the stack else push
the scanned character to stack. Repeat this step as long as stack is not empty and
6. After all characters are scanned, we have to add any character that the stack may
have to the Postfix string. If stack is not empty add top Stack to Postfix string and
Pop the stack. Repeat this step as long as stack is not empty.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40
// Remainder precedence.
#define REMP 2
#define NONE 9
void main()
{
char ch;
do
{
top=-1;
printf("\nEnter an infix expression\n");
fflush(stdin);
gets(infix);
infixtopostfix();
printf("\ninfix = %s\npost fix =%s\n",infix,postfix);
printf("\nDo you wish to continue\n");
ch=getche();
}while(ch=='Y' || ch=='y');
}
void infixtopostfix(void)
{
int i,p,l,type,prec;
char next;
i=p=0;
l=strlen(infix);
while(i<l)
{
type=gettype(infix[i]);
switch(type)
{
case LP:
push(infix[i]);
break;
case RP:
while((next=pop())!='(')
postfix[p++]=next;
break;
case OPERAND:
postfix[p++]=infix[i];
break;
case OPERATOR:
prec=getprec(infix[i]);
while(top>-1 && prec <= getprec(stack[top]))
postfix[p++]=pop();
push(infix[i]);
break;
}
i++;
}
while(top>-1)
postfix[p++]=pop();
postfix[p]='\0';
}
char pop(void)
{
if(top<=-1)
{
printf("\nStack is empty\n");
exit(0);
}
else
return(stack[top--]);
}
Infix = 3*6+4*2/5
Post fix =36*42*5/+
Sorting:
Sorting algorithm is an algorithm that puts elements of a list in a certain order . The most-
used orders are numerical order and lexicographical order.
Computaional complexcity.
Memory utilization
No.of comparisions
1. Internal sorting
2. External sorting
Internal sorting:
Heap sort:
Heapsort (method) is a comparison-based sorting algorithm, and is part of the selection sort
family. Although somewhat slower in practice on most machines than a good implementation
of quicksort, it has the advantage of a worst-case (n log n) runtime. Heapsort is an in-place
algorithm, but is not a stable sort.
In heap sort the array is interpreted as a binary tree. This method has 2 phases.
2.It has the advantages of worst case O(N log N) running time.
Limitations:
Alogrithm steps:
1) Structrue Property:
For any element in array position I,the left child is in position 2i,the right child is
in 2i+1,(ie) the cell after the left child.
The value in the parent node is smaller than or equal to the key value of any of
its child node.Build the heap,apply the heap order propery starting from the right. most non-
leaf node at the bottom level.
The array elements aer stored using deletemin operation,which gives the elements
arranged in descending order.
#include<stdio.h>
#include<conio.h>
int hsort[25],n,i;
void adjust(int,int);
void heapify();
void main()
{
int temp;
clrscr();
printf("\n\t\t\t\tHEAP SORT");
printf("\n\t\t\t\t**** ****\n\n\n");
printf("\nenter no of elements:");
scanf("%d",&n);
printf("\nenter elements to be sorted\n\n");
for(i=1;i<=n;i++)
scanf("%d",&hsort[i]);
heapify();
for(i=n;i>=2;i--)
{
temp=hsort[1];
hsort[1]=hsort[i];
hsort[i]=temp;
adjust(1,i-1);
}
printf("\nSORTED ELEMENT\n\n");
for(i=1;i<=n;i++)
printf("%d\n",hsort[i]);
getch();
}
void heapify()
{
int i;
for(i=n/2;i>=1;i--)
adjust(i,n);
}
void adjust(int i,int n)
{
int j,element;
j=2*i;
element=hsort[i];
while(j<=n)
{
if((j<n)&&(hsort[j]<hsort[j+1]))
j=j++;
if(element>=hsort[j])
break;
hsort[j/2]=hsort[j];
j=2*j;
}
hsort[j/2]=element;
}
Enter No Of Elements:5
5
4
3
2
1
Sorted Element
1
2
3
4
5