Implementation of Singly Linked List
Implementation of Singly Linked List
AIM:
ALGORITHM:
Step 1: Create a list of element one linked to the other using create function
Step 2: To insert any new element to the list, we have to get the position
Step 3: After create the list if we have to delete any element we can do it by
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int i;
struct node;
struct node
int e;
pos next;
};
void deletelist(list L)
pos p,tmp;
p=L->next;
L->next=NULL;
while(p!=NULL)
tmp=p->next;
free(p);
p=tmp;
}
}
list makeempty(list L)
if(L!=NULL)
deletelist(L);
L=malloc(sizeof(struct node));
L->next=NULL;
return L;
int isempty(list L)
return L->next==NULL;
int islast(pos p)
return p->next==NULL;
pos p;
p=L->next;
while(p!=NULL&&p->e!=x)
p=p->next;
return p;
}
pos tmpcell;
tmpcell=malloc(sizeof(struct node));
tmpcell->e=x;
tmpcell->next=p->next;
p->next=tmpcell;
pos header(list L)
{return L;
pos advance(pos p)
{return p->next;
int retrieve(pos p)
{return p->e;
{pos p=header(L);
do
p=advance(p);
printf("%d",retrieve(p));
}while(!islast(p));
printf("\n");
int main()
{list L;
pos p;
int i;
clrscr();
L=makeempty(L);
p=header(L);
for(i=0;i<10;i++)
insert(i,p);
printlist(L);
p=advance(p);
scanf("%d" ,&i);
if(find(i,L)!=NULL)
printf("Element found");
else
p=header(L);
deletelist(L);
if(isempty(L))
getch();
return 0;
OUTPUT:
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
Element found
RESULT:
Thus the c program for implementation of linked list was executed and the
Date:
AIM:
ALGORITHM:
Step 1: Create a list of element one linked to the other using create function
Step 2: To insert any new element to the list, we have to get the position
Step 3: After create the list if we have to delete any element we can do it by
#include<stdio.h>
#include<stdlib.h>
struct node
elementtype element;
position Next,Prev;
};
list
makeempty(list L)
if(L!=NULL)
deletelist(L);
L=malloc(sizeof(struct node));
if(L==NULL)
printf("Out of Memory!");
L->Next=NULL;
L->Prev=NULL;
return L;
}
int isempty(list L)
return P->Next==NULL;
position P;
P=L->Next;
P=P->Next;
return P;
void
delete(elementtype X,list L)
position P;
P=find(X,L);
P->Next->Prev=P->Prev;
P->Prev->Next=P->Next;
free(P);
}
void
position tempcell;
tempcell=malloc(sizeof(struct node));
if(tempcell==NULL)
printf("Out of space!!!");
tempcell->element=X;
tempcell->Next=P->Next;
tempcell->Prev=P;
P->Next->Prev=tempcell;
P->Next=tempcell;
int deletelist(list L)
position P,tmp;
P=L->Next;
L->Next=NULL;
L->Prev=NULL;
while(P!=NULL)
tmp=P->Next;
free(P);
P=tmp;
return 0;
position
header(list L)
return L;
position
first(list L)
return L->Next;
position
advance(position P)
return P->Next;
elementtype retrieve(position P)
return P->element;
}
void
printlist(const list L)
position P=header(L);
if(isempty(L))
printf("Empty List\n");
else
do
P=advance(P);
printf("%d",retrieve(P));
while(!islast(P,L));
printf("\n");
main()
list L;
position P;
int i;
clrscr();
L=makeempty(NULL);
P=header(L);
printlist(L);
for(i=0;i<10;i++)
insert(i,L,P);
printlist(L);
P= advance(P);
for(i=0;i<10;i+=2)
delete(i,L);
printf("Finished deletion\n");
printlist(L);
scanf("%d",&i);
if(find(i,L)!=NULL)
printf("Element found");
else
deletelist(L);
getch();
return 0;
}
OUTPUT:
Empty list
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
Finished Deletion
13579
Element found
RESULT:
Thus the c program for implementation of linked list was executed and the
Date:
AIM:
ALGORITHM:
STEP 1: Create a function. Get the input from user of the coefficients and
the power and create node for it
STEP 2: Continue step1 until the user press a key other than y or Y
STEP 4: Create a function named poly. Check for the greater number
between numbers having the same number.
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link
int coeff,pow;
};
char ch;
do
scanf("%d",&node->coeff);
scanf("%d",&node->pow);
node=node->next;
node->next=NULL;
ch=getch();
}
while(ch=='y'||ch=='Y');
while(node->next!=NULL)
printf(" %dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf(" +");
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
else
poly->pow=poly1->pow;
poly->coeff=(poly1->coeff)+(poly2->coeff);
poly1=poly1->next;
poly2=poly2->next;
poly=poly->next;
poly->next=NULL;
while(poly1->next||poly2->next)
if(poly1->next)
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
poly=poly->next;
poly->next=NULL;
void main()
char ch;
clrscr();
do
creat(poly1);
creat(poly2);
printf("\n 1st No.");
show(poly1);
show(poly2);
polyadd(poly1,poly2,poly);
show(poly);
ch=getch();
while(ch=='y'||ch=='Y');
OUTPUT
Enter Coef: 2
Enter Power : 3
Continue (y/n)
Enter Coef: 52
Enter Power : 2
Continue (y/n)
Enter Coef: 52
Enter Power : 3
Continue (y/n)
Enter Coef: 10
Enter Power : 2
Continue (y/n)
Added Polynomial:
RESULT
Date:
AIM:
ALGORITHM:
STEP 1: Start
insert an element
STEP 3: If the stack is not full push the element onto the stack
STEP 4: Repeat the Step 3 until delimiter or end appears. Check for
STEP 6: ELSE pop out the two contents at the top from the top and
STEP 8: Stop
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#define operand(x) (x>='a'&&x<='z'||x>='A'&&x<='Z')
void infixtopostfix();
int priority(char);
void push(char);
char pop();
int top=-1;
char infix[20],stack[20],postfix[20];
void main()
{
clrscr();
printf("enter the expression");
scanf("%s",infix);
infixtopostfix();
printf("%s",postfix);
getch();
}
void infixtopostfix()
{
int j,l=0;
char x,y;
for(j=0;(x=infix[j])!='\0';j++)
{
if(operand(x))
postfix[l++]=x;
else if(x=='(')
push(x);
else if(x==')')
{
while((y=pop())!='(')
postfix[l++]=y;
}
else
{
while(priority(stack[top])>=priority(x)&&stack[top]!='(')
postfix[l++]=pop();
push(x);
}
}
while(top>=0)
postfix[l++]=pop();
}
void push(char n)
{
stack[++top]=n;
}
int priority(char x)
{
int y;
y=(x=='('?3:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:-1);
return y;
}
char pop()
{
char n;
n=stack[top];
top--;
return(n);
}
OUTPUT:
ab+cd/-
RESULT:
Thus the conversion from infix expression to postfix was executed and the
output was verified.
Ex.No:
Date:
AIM:
ALGORITHM:
STEP 1: Create a queue and check whether the queue is empty or not
STEP 4: Create two functions to delete the element from queue from
STEP 7: Stop
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#define SIZE 20
typedef struct dq_t
{
int front,rear;
int item[SIZE];
}deque;
void create(deque *);
void display(deque *);
void insert_rear(deque *, int);
void insert_front(deque *, int);
int delete_front(deque *, int);
int delete_rear(deque *, int);
void main()
{
int x,data,ch;
deque DQ;
clrscr();
create(&DQ);
printf(“\n\t\t Program shows working of double ended queue”);
do
{
printf(“\n\t\t Menu”);
printf(“\n\t\t 1: insert at rear end”);
printf(“\n\t\t 2: insert at front end”);
printf(“\n\t\t 3: delete from front end”);
printf(“\n\t\t 4: delete from rear end”);
printf(“\n\t\t 5: exit. “);
printf(“\n\t\t Enter choice :”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
if (DQ.rear >= SIZE)
{
printf(“\n Deque is full at rear end”);
continue;
}
else
{
printf(“\n Enter element to be added at rear end :”);
scanf(“%d”,&data);
insert_rear(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 2:
if (DQ.front <=0)
{
printf(“\n Deque is full at front end”);
continue;
}
else
{
printf(“\n Enter element to be added at front end :”);
scanf(“%d”,&data);
insert_front(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 3:
x = delete_front(&DQ,data);
if (DQ.front==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 4:
x = delete_rear(&DQ,data);
if (DQ.rear==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 5: printf(“\n finish”); return;
}
}while(ch!=5);
getch();
}
void create(deque *DQ)
{
DQ->front=0;
DQ->rear =0;
}
void insert_rear(deque *DQ, int data)
{
if ((DQ->front == 0) &&(DQ->rear == 0))
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
else
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
}
int delete_front(deque *DQ, int data)
{
if ((DQ->front == 0) && (DQ->rear == 0))
{
printf(“\n Underflow”);
return(0);
}
else
{
data = DQ->item[DQ->front];
printf(“\n Element %d is deleted from front :”,data);
DQ->front = DQ->front +1;
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty (front end)”);
}
return data;
}
void insert_front(deque *DQ, int data)
{
if(DQ->front > 0)
{
DQ->front = DQ->front-1;
DQ->item[DQ->front] = data;
}
}
int delete_rear(deque *DQ, int data)
{
if (DQ->rear == 0)
{
printf(“\n Underflow”);
return(0);
}
else
{
DQ->rear = DQ->rear -1;
data = DQ->item[DQ->rear];
printf(“\n Element %d is deleted from rear:”,data);
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty(rear end)”);
}
return data;
}
void display(deque *DQ)
{
int x;
for(x=DQ->front;xrear;x++)
{
printf(“%d\t”,DQ->item[x]);
}
printf(“\n\n”);
}
OUTPUT:
RESULT:
Thus the program for deque operation was executed and the output
was verified.
Ex.No:
Date:
AIM:
ALGORITHM:
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>
#define size 20
char data;
}btree;
btree *stack[size];
int top;
void main()
btree *root;
char exp[80];
clrscr();
scanf("%s",exp);
top=-1;
root=create(exp);
inorder(root);
preorder(root);
postorder(root);
getch();
btree *temp;
int pos;
char ch;
btree *pop();
pos=0;
ch=exp[pos];
while(ch!='\0')
temp=(btree*)malloc(sizeof(btree));
temp->left=temp->right=NULL;
temp->data=ch;
if(isalpha(ch))
push(temp);
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
temp->right=pop();
temp->left=pop();
push(temp);
else
printf("Invalid character\n");
pos++;
ch=exp[pos];
temp=pop();
return(temp);
if(top+1>=size)
printf("\nstack is Full");
top++;
stack[top]=node;
}
btree *pop()
btree *node;
if(top==-1)
printf("\nStack is empty");
node=stack[top];
top--;
return(node);
btree *temp;
temp=root;
if(temp!=NULL)
inorder(temp->left);
printf("%c",temp->data);
inorder(temp->right);
} }
btree *temp;
temp=root;
if(temp!=NULL)
{
printf("%c",temp->data);
preorder(temp->left);
preorder(temp->right);
} }
btree *temp;
temp=root;
if(temp!=NULL)
postorder(temp->left);
postorder(temp->right);
printf("%c",temp->data);
} }
OUTPUT:
Enter the postfix expression:ab*cd/+
a*b+c/d
+*ab/cd
ab*cd/+
RESULT:
Ex.No:
Date:
AIM:
To write a c program to implement binary search tree
ALGORITHM:
Step 1: Start
Step 3: Write a function to find the min element and the maximum element
inorder traversal
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct treenode
{ int element;
};
if(t==NULL)
if(t==NULL)
exit(0);
else
t->element=x;
t->left=t->right=NULL;
else
if(x<t->element)
t->left=insert(x,t->left);
else
if(x>t->element)
t->right=insert(x,t->right);
return t;
position findmin(searchtree t)
if(t==NULL)
return NULL;
else
if(t->left==NULL)
return t;
else
return findmin(t->left);
position findmax(searchtree t)
if(t==NULL)
return NULL;
else
if(t->right==NULL)
return t;
else
return findmax(t->right);
position temp;
if(t==NULL)
else
if(x<t->element)
t->left=rem(x,t->left);
else
if(x>t->element)
t->right=rem(x,t->right);
else
if(t->left&&t->right)
temp=findmin(t->right);
t->element=temp->element;
t->right=rem(t->element,t->right);
}
else
temp=t;
if(t->left==NULL)
t=t->right;
else
if(t->right==NULL)
t=t->left;
free(temp);
return t;
if(head==NULL)
return;
if(head->left!=NULL)
intrav(head->left);
printf("%d\t",head->element);
if(head->right!=NULL)
intrav(head->right);
}
void main()
int n,i,dat,ch;
searchtree t=NULL;
position node;
clrscr();
printf("Enter no of elements:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&dat);
t=insert(dat,t);
intrav(t);
do
printf("\n\n");
printf("\n ****MENU****\n");
scanf("%d",&ch);
switch(ch)
scanf("%d",&dat);
t=insert(dat,t);
break;
scanf("%d",&dat);
t=rem(dat,t);
break;
case 3:node=findmin(t);
break;
case 4:node=findmax(t);
break;
case 5:intrav(t);
break;
case 6:exit(0);
}while(ch!=6);
getch();
OUTPUT:
Enter no of elements:
45
15
33
2 7 15 33 45
****MENU****
6 -> Exit
2 7 15 33 45
****MENU****
****MENU****
6 -> Exit
6 -> Exit
****MENU****
6 -> Exit
2 7 15 18 45
RESULT:
Thus the implementation of Binary search tree is done and the program
is executed.
Ex.No:
Date:
Algorithm:
1. Create a class AVl tree and declare all its member variables and functions
2. Get the user option if tge choice is 1, create a binary tree, with the values
and considering the Balance factor
4. If the balance factor is not equal to 1, 0,-1 then we have to rotate the
tree one time left or right or Double rotate
6. If the option is 3, get the element to be searched and display whether the
element is present or Not.
Program coding:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
typedef struct node *pos;
typedef struct node *avltree;
avltree t;
struct node
{
int element;
avltree left;
avltree right;
int height;
};
avltree insert(int,avltree);
int max(int,int);
int height(pos p);
pos singlerotatewithleft(pos);
pos singlerotatewithright(pos);
pos doublerotatewithleft(pos);
pos doublerotatewithright(pos);
void display(avltree);
void main()
{
int ch,ele;
clrscr();
t=NULL;
do
{
printf("\nMENU\n");
printf("\n1. Insertion\n");
printf("\n2. Display\n");
printf("\n3. Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to be inserted:");
scanf("%d",&ele);
t=insert(ele,t);
printf("\nThe element is inserted");
break;
case 2: printf("\nThe elements in the AVL TREE are:");
display(t);
break;
case 3: exit(0);
break;
}
}while(ch<4);
getch();
}
int height(pos p)
{
if(p==NULL)
return -1;
else
return p->height;
}
OUTPUT:
MENU
1. Insertion
2. Display
3. Exit
MENU
1. Insertion
2. Display
3. Exit
MENU
1. Insertion
2. Display
3. Exit
MENU
1. Insertion
2. Display
3. Exit
MENU
1. Insertion
2. Display
3. Exit
RESULT:
Thus the C program To Implement Insertion in AVL Trees was written and
verified.