Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
//Data Structures
//** Array Operations **
//13/08/2015
//******************************
//By : Bala Kumar
//******************************
for(i=n;i>=pos;i--)
a[i+1]=a[i];
a[i]=ele;
n++;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
printf("Element Inserted.\n");
break;
void main()
{
int a[50],n=0,ch,ele,pos,flag=0,i;
clrscr();
//Deletion
case 3:
if(!n)
{
do
{
printf("Array is empty");
break;
}
printf("Please enter the position of the element
you want to delete:\n");
scanf("%d",&pos);
pos-=1; //Converting given pos into arr index
switch(ch)
{
if(pos>=n)
{
//Creation
case 1:
if(n)
{
break;
}
for(i=pos;i<n;i++)
a[i]=a[i+1];
n--;
printf("Element Deleted.\n");
break;
Array(<50):\n");
do
{
//Search
scanf("%d",&n);
if(n>=50)
printf("Array Size cannot
case 4:
if(!n)
{
printf("Array is empty");
break;
}
printf("Enter your search:\n");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
flag=1;
pos=i;
}
}
//Insertion
case 2:
if(!n)
{
printf("Array is empty");
break;
}
if(flag)
printf("Element found @ position %d
printf("Please enter the element you want to
insert:\n");
(First Match).",pos+1);
else
scanf("%d",&ele);
printf("Enter the position:\n");
scanf("%d",&pos);
case 5:
if(!n)
{
printf("Array is empty");
break;
}
printf("Array: ");
for(i=0;i<n-1;i++)
printf("%d,",a[i]);
printf("%d",a[n-1]);
break;
//Exit Loop
}*start=NULL;
void main()
{
int ch,pos;
case 6:
exit(0);
clrscr();
//Invalid Choice
default:
printf("Invalid Option.");
break;
do
{
printf("Your options are:\n");
printf("1) Creation\n2) Display\n3) Insert\n4) Delete\n5)
Exit\n");
scanf("%d",&ch);
}
getch();
clrscr();
fflush(stdin);
}while(ch!=6);
switch(ch)
{
case 1:
start=create(start);
break;
case 2:
display(start);
getch();
break;
case 3:
if(start==NULL)
{
printf("The list is empty. Please create
a Linkedlist first.");
getch();
break;
}
printf("Please enter the position in which you
want to create a new node:\n");
scanf("%d",&pos);
start=insert(start,pos);
getch();
break;
case 4:
if(start==NULL)
{
printf("The list is empty. There's
nothing to delete");
getch();
break;
}
printf("Please enter the position of the node you
wish to delete:\n");
scanf("%d",&pos);
start=del(start,pos);
getch();
break;
case 5:
exit(0);
//Lab 3
//Data Structures Lab
//Singly Linked List - Creation,Insertion,Deletion & Display.
//** By: Bala Kumar **
//** 20/08/2015 **
default:
printf("Invalid Choice");
getch();
break;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}
clrscr();
}while(ch!=5);
}
struct node *create(struct node *start)
{
struct node *newnode,*ptr;
int data;
if(start!=NULL)
{
printf("A Linkedlist already exists. Please use the insert
option to add new nodes.");
getch();
return start;
}
printf("Enter your data (enter -1 at the end):\n");
scanf("%d",&data);
while(data!=-1)
{
newnode=(struct node *)malloc(sizeof(struct node));
if(!newnode)
{
printf("Overflow!");
return NULL;
}
newnode->data=data;
if(start==NULL)
{
newnode->next=NULL;
start=newnode;
}
else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
for(ptr=start,ctr=2;ctr<pos&&ptr!=NULL;ptr=ptr->next,ctr++);
3
newnode->next=ptr->next;
ptr->next=newnode;
}
printf("Node inserted");
return start;
}
struct node *del(struct node *start,int pos)
{
int ctr;
struct node *ptr,*t;
if(pos==1)
{
t=start;
start=start->next;
free(t);
}
else
{
for(ptr=start,ctr=2;ctr<pos&&ptr->next!=NULL;ptr=ptr>next,ctr++);
if(ptr->next==NULL)
{
printf("Position out of bound.");
return start;
}
ptr->next=newnode;
newnode->next=NULL;
t=ptr->next;
ptr->next=(ptr->next)->next;
free(t);
}
scanf("%d",&data);
}
printf("Node deleted.");
return start;
}
return start;
}
}
void display(struct node *start)
{
struct node *ptr;
if(start==NULL)
{
printf("The Linked list is empty.");
return;
}
printf("START --> ");
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
printf("%d --> ",ptr->data);
}
printf("END");
}
struct node *insert(struct node *start,int pos)
{
int ctr;
struct node *newnode,*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d",&newnode->data);
if(pos==1)
{
newnode->next=start;
start=newnode;
}
else
//Lab 4
//Data Structures Lab
clrscr();
do
return start;
printf("Your options are:\n");
printf("1) Creation\n2) Display\n3) Insert\n4) Delete\n5)
Exit\n");
scanf("%d",&ch);
4
void display(struct node *start)
{
struct node *ptr;
switch(ch)
{
if(start==NULL)
{
printf("The Linked list is empty.");
return;
}
case 1:
start=create(start);
break;
case 2:
display(start);
getch();
break;
case 3:
printf("Please enter the position in which you
}
struct node *insert(struct node *start,int pos)
{
int ctr;
struct node *newnode,*ptr;
scanf("%d",&pos);
start=del(start,pos);
getch();
break;
case 5:
exit(0);
if(pos==1)
{
default:
printf("Invalid Choice");
break;
for(ptr=start;ptr->next!=start;ptr=ptr->next);
ptr->next=newnode;
newnode->next=start;
start=newnode;
}
}
else
{
clrscr();
}while(ch!=5);
}
for(ptr=start,ctr=2;ctr<pos&&ptr->next!=start;ptr=ptr>next,ctr++);
newnode->next=ptr->next;
ptr->next=newnode;
}
printf("Node inserted");
return start;
}
struct node *del(struct node *start,int pos)
{
int ctr;
struct node *ptr,*t;
if(pos==1)
{
t=start;
for(ptr=start;ptr->next!=start;ptr=ptr->next);
ptr->next=start->next;
start=start->next;
free(t);
}
else
{
for(ptr=start,ctr=2;ctr<pos&&ptr->next!=start;ptr=ptr>next,ctr++);
ptr->next=newnode;
newnode->next=start;
}
scanf("%d",&data);
}
if(ptr->next==start)
{
printf("Position out of bound.");
return start;
}
case 3:
5
if(start==NULL)
{
printf("The list is empty. There's
t=ptr->next;
ptr->next=(ptr->next)->next;
free(t);
}
printf("Node deleted.");
return start;
nothing to delte");
getch();
break;
}
}
printf("Please enter the position of the node you
with to delete:\n");
//Lab 4
//Data Structures Lab
scanf("%d",&pos);
start=del(start,pos);
getch();
break;
case 4:
printf("Please enter the position of the new
node:\n");
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
scanf("%d",&pos);
start=insert(start,pos);
getch();
break;
struct node
{
int data;
struct node *prev,*next;
}*start=NULL;
case 5:
if(start==NULL)
{
printf("The list is empty.");
getch();
break;
}
printf("Please enter your search : ");
scanf("%d",&ele);
index=search(start,ele);
if(index!=-1)
printf("Index of the entered element:
else
int ele,ch,pos,index;
do
{
case 6:
if(start==NULL)
{
printf("The list is empty.");
getch();
break;
}
show(start);
break;
case 7:
exit(0);
break;
default:
printf("Invalid Option");
break;
start=create(start);
break;
case 2:
if(start==NULL)
{
printf("The list is empty.");
getch();
break;
}
display(start);
getch();
break;
}
clrscr();
}while(ch!=7);
}
struct node *create(struct node *start)
{
struct node *newnode,*ptr;
int ele,ctr;
printf("Please enter the elements (-1 at the end):\n");
scanf("%d",&ele);
do
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(pos==1)
{
if(start==NULL)
{
start=newnode;
newnode->prev=NULL;
newnode->next=NULL;
}
else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
start->prev=newnode;
newnode->next=start;
newnode->prev=NULL;
start=newnode;
}
else
{
for(ctr=2,ptr=start;
ctr<pos&&ptr->next!=NULL;
ptr=ptr->next,ctr++);
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
newnode->next=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
if(newnode->next!=NULL)
(newnode->next)->prev=newnode;
}
scanf("%d",&ele);
}while(ele!=-1);
return start;
}
printf("Node inserted.");
return start;
}
void display(struct node *start)
{
struct node *ptr;
printf("START -> ");
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
printf("%d -> ",ptr->data);
}
printf("END");
}
int search(struct node *start, int ele)
{
struct node *ptr;
int ctr;
for(ctr=1,ptr=start;ptr->data!=ele&&ptr!=NULL;ptr=ptr->next,ctr++);
if(ptr!=NULL)
return ctr;
else
return -1;
}
struct node *del(struct node *start, int pos)
{
int ctr;
struct node *ptr,*t;
if(pos==1)
{
t=start;
(start->next)->prev=NULL;
start=start->next;
free(t);
}
else
{
for(ctr=2,ptr=start;ptr->next!=NULL&&ctr<pos;ptr=ptr>next,ctr++);
if(ptr->next==NULL)
{
printf("Position out of bound.");
return start;
}
t=ptr->next;
(t->next)->prev=ptr;
ptr->next=t->next;
free(t);
}
printf("Node deleted.");
return start;
}
struct node *insert(struct node *start, int pos)
{
int ctr;
struct node *newnode,*ptr;
ptr=ptr->next;
break;
}
clrscr();
}while(ch!=2);
}
//Lab 4
//Data Structures Lab
//By: Bala Kumar
//*******************************
// Stack - Using Array
//*******************************
if(top==-1)
return 0;
else
{
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
top--;
return 1;
}
}
int push(int);
int pop();
void display();
void display()
{
int i;
int STACK[50],top=-1;
if(top==-1)
{
void main()
{
int ele,i,ch;
clrscr();
do
{
for(i=top;i>=0;i--)
{
printf("|-----|\n");
printf("| %3d |\n",STACK[i]);
}
printf("|_____|\n");
//Lab 4
//Data Structures
//By : Bala Kumar
//***********************************
// STACK - Using Linked-List
//***********************************
break;
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
case 2:
if(pop())
printf("One item successfully
removed.");
else
printf("Underflow.");
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
getch();
clrscr();
}while(ch!=4);
}
struct node
{
int data;
struct node *next;
}*top=NULL;
int push(int);
int pop();
void display();
void main()
{
int ch,ele;
clrscr();
do
{
printf("Your options are:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Please enter the element :\n");
scanf("%d",&ele);
if(push(ele))
printf("Item successfully inserted.");
else
printf("Overflow");
break;
void display()
{
struct node *ptr;
printf("\n| |\n");
for(ptr=top;ptr!=NULL;ptr=ptr->next)
{
printf("|-----|\n");
printf("| %3d |\n",ptr->data);
}
printf("|_____|\n");
}
case 2:
if(pop())
printf("One element successfully
removed.");
else
//Lab 6
//Data Structures Lab
//By: Bala Kumar
printf("Underflow");
break;
case 3:
display();
break;
case 4:
exit(0);
}
getch();
clrscr();
}while(ch!=4);
//***********************************************
// Program to evaluate given postfix expression
//***********************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
float st[30];
int top=-1;
}
int push(int ele)
{
struct node *ptr,*newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(!newnode)
{
printf("Overflow");
return 0;
}
newnode->data=ele;
if(top==NULL)
{
top=newnode;
newnode->next=NULL;
}
else
{
newnode->next=top;
top=newnode;
}
return 1;
}
int pop()
{
struct node *ptr,*preptr;
if(top==NULL)
{
printf("Underflow!");
return 0;
}
else
{
ptr=top;
top=top->next;
free(ptr);
return 1;
float pop(float[]);
void push(float[],float);
float eval(char[]);
void main()
{
char postfix[20];
float val;
clrscr();
printf("Enter the expression:");
gets(postfix);
val=eval(postfix);
printf("The value of the given expression: %f",val);
getch();
}
float eval(char postf[])
{
int i=0;
float op1,op2,res;
char temp;
while(postf[i]!='\0')
{
if(isdigit(postf[i]))
{
push(st,(float)(postf[i]-'0'));
i++;
}
else if(postf[i]=='+'||postf[i]==''||postf[i]=='*'||postf[i]=='/'||postf[i]=='%')
{
op1=(char)pop(st);
op2=(char)pop(st);
switch(postf[i])
{
case '+':
res=op1+op2;
break;
case '-':
res=op2-op1;
break;
{
int n;
9
case '*':
res=op1*op2;
break;
clrscr();
printf("Please enter the number of discs:\n");
scanf("%d",&n);
case '/':
res=op2/op1;
break;
move(n,'A','C','B');
getch();
}
}
push(st,res);
i++;
}
else
{
printf("Invalid Expression");
getch();
exit(0);
}
}
return pop(st);
}
float pop(float st[])
{
char val;
//Assignment 2
//Data Structures Lab
//** A.Bala Kumar **
//13/08/2015
if(top==-1)
{
printf("Underflow!");
getch();
exit(0);
}
else
{
val=st[top];
top--;
return val;
}
}
void push(float st[],float ch)
{
if(top==30)
{
printf("Overflow");
getch();
exit(0);
}
else
{
top++;
st[top]=ch;
}
}
//********************************************
//Program: Managing accounts using structures
//********************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int accid;
char name[15];
char dob[11];
char city[12];
char mobno[15];
}accounts;
void create(accounts [],int);
void showall(accounts [],int);
void insert(accounts [],int *);
int search(accounts [],int,char [],char);
void del(accounts [],int *,int);
void main()
{
int ch,size=0,i,index;
accounts ppl[30];
char c,ele[20];
//Lab 6
//Data Structures
//By : Bala Kumar
//***************************************
// Program to illustrate Tower of Hanoi
//***************************************
#include<stdio.h>
#include<conio.h>
void move(int,char,char,char);
void main()
clrscr();
do
{
printf("Your choices are:\n");
printf("1) Creation\n2) Insertion\n3) Deletion\n4) Search\n5)
Show All\n6) Exit\n");
printf(">> ");
scanf("%d",&ch);
switch(ch)
{
//Creation
gets(ele);
index=search(ppl,size,ele,c);
10
if(index!=-1)
{
case 1:
printf("How many accounts do you want to
create?(<30)\n");
scanf("%d",&size);
printf("-------------------------\n");
printf("Account %d\n",index+1);
printf("-------------------------\n");
printf("Account ID:
create(ppl,size);
break;
//Insertion
case 2:
insert(ppl,&size);
break;
//Deletion
case 3:
%d\n",ppl[index].accid);
printf("Name: %s\n",ppl[index].name);
printf("Date of Birth:
%s\n",ppl[index].dob);
printf("City : %s\n",ppl[index].dob);
printf("Mobile Number :
%s\n",ppl[index].mobno);
break;
}
else
{
if(!size)
{
printf("The database is empty. Nothing
to delete.\n");
break;
}
clrscr();
printf("Delete by:\na) Account ID\nb) Name\nc)
//Accounts Display
case 5:
showall(ppl,size);
printf("\nPlease press a key to clear..");
break;
//Exit Option
case 6:
exit(0);
break;
default:
printf("Invalid Option.\n");
break;
}
getch();
fflush(stdin);
clrscr();
}while(ch!=6);
%d\n",ppl[index].accid);
printf("Name: %s\n",ppl[index].name);
printf("Date of Birth:
%s\n",ppl[index].dob);
printf("City : %s\n",ppl[index].dob);
printf("Mobile Number :
%s\n",ppl[index].mobno);
printf("-------------------------\n");
del(ppl,&size,index);
printf("Entry Deleted.");
}
break;
//Searching
case 4:
if(!size)
{
printf("The database is empty. There's
nothing to search.\n");
break;
}
clrscr();
printf("Search by:\na) Account ID\nb) Name\nc)
Date of Birth\n >> ");
fflush(stdin);
scanf("%c",&c);
printf("Enter the search term/ID: ");
fflush(stdin);
for(i=0;i<n;i++)
{
printf("----------------------------\n");
printf("Account %d\n",i+1);
printf("----------------------------\n");
printf("Account ID: ");
scanf("%d",&pl[i].accid);
printf("Name: ");
fflush(stdin);
gets(pl[i].name);
printf("Date of Birth (DD/MM/YYYY): ");
gets(pl[i].dob);
printf("City: ");
fflush(stdin);
gets(pl[i].city);
printf("Mobile Number : ");
gets(pl[i].mobno);
}
printf("Account(s) Created.\n");
case 'a':
elem=atoi(ele);
for(i=0;i<n;i++)
{
if(pl[i].accid==elem)
return i;
}
return -1;
}
void showall(accounts pl[],int n)
{
int i;
if(!n)
{
printf("Database is empty.\n");
return;
}
printf("------------------------------------------------------------------------\n");
printf(" Account ID | Name
| DOB
| City
| Mobile
Number \n");
printf("------------------------------------------------------------------------\n");
11
case 'b':
for(i=0;i<n;i++)
{
if(strcmp(ele,pl[i].name)==0)
return i;
}
return -1;
for(i=0;i<n;i++)
{
printf("%11d ",pl[i].accid);
printf("%14s ",pl[i].name);
printf("%10s ",pl[i].dob);
printf("%11s",pl[i].city);
printf(" %14s\n",pl[i].mobno);
}
case 'c':
for(i=0;i<n;i++)
{
if(strcmp(ele,pl[i].dob)==0)
return i;
}
return -1;
}
void insert(accounts pl[],int *si)
{
}
}
int m,i,n=*si;
void del(accounts pl[],int *n,int index)
{
int i,m=*n;
if(!*si)
{
printf("You have to create a db first.\n");
return;
for(i=index;i<m-1;i++)
{
pl[i].accid=pl[i+1].accid;
strcpy(pl[i].name,pl[i+1].name);
strcpy(pl[i].dob,pl[i+1].dob);
strcpy(pl[i].city,pl[i+1].city);
strcpy(pl[i].mobno,pl[i+1].mobno);
}
(*n)--;
}
printf("Enter the number of accounts you want to insert:\n");
scanf("%d",&m);
if(m+n>=30)
{
printf("No. of accounts exceed 30.\n");
return;
}
for(i=0;i<m;i++)
{
printf("----------------------------\n");
printf("Account %d\n",n+i+1);
printf("----------------------------\n");
printf("Account ID: ");
scanf("%d",&pl[n+i].accid);
printf("Name: ");
fflush(stdin);
gets(pl[n+i].name);
printf("Date of Birth (DD/MM/YYYY): ");
gets(pl[n+i].dob);
printf("City: ");
fflush(stdin);
gets(pl[n+i].city);
printf("Mobile Number : ");
gets(pl[n+i].mobno);
}
printf("Account(s) Inserted.\n");
(*si)+=m;
}
//Assignment - 3
//Data Structures Lab
//** By: Bala Kumar **
//*******************************************************
//Program : Managing Accounts using Singly Linked List
//*******************************************************
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int accid;
char name[15];
char dob[11];
char city[12];
char mobno[15];
struct node *next;
}*start=NULL;
int search(struct node *,char [],int);
struct node *create(struct node *);
void display(struct node *);
printf("Account ID:
%d\n",ptr->accid);
12
printf("Name
: %s\n",ptr>name);
printf("Date of Birth :
%s\n",ptr->dob);
printf("City : %s\n",ptr-
void main()
{
int ch,pos,c;
char ele[20];
struct node *ptr;
>city);
printf("Mobile Number :
%s\n",ptr->mobno);
printf("------------------------------\n");
start=del(start,pos);
clrscr();
}
else
do
{
case 1:
start=create(start);
break;
case 5:
if(start==NULL)
{
printf("The list is already empty.");
getch();
break;
}
case 2:
display(start);
getch();
break;
start=delall(start);
printf("List is cleared.");
getch();
break;
case 3:
if(start==NULL)
{
printf("The list is empty. Please create
case 6:
if(start==NULL)
{
printf("The list is empty. Please create
switch(ch)
{
a list first.");
one first.");
getch();
break;
getch();
break;
}
clrscr();
printf("Search by:\n");
printf("1) Name\n2) Date of Birth\n3) City\n4)
}
printf("Please enter the account id , where you
want to insert a new account :\n");
scanf("%d",&pos);
start=insert(start,pos);
getch();
break;
case 4:
if(start==NULL)
{
printf("There are no accounts in the list
to delete.Please create a list first.");
getch();
break;
}
if(pos!=-1)
{
for(ptr=start;ptr>accid!=pos&&ptr!=NULL;ptr=ptr->next);
printf("-------------------------------\n");
printf("Account ID: %d\n",ptr->accid);
printf("Name : %s\n",ptr->name);
printf("Date of Birth : %s\n",ptr->dob);
printf("City : %s\n",ptr->city);
printf("Mobile Number : %s\n",ptr-
clrscr();
printf("Delete by:\n");
printf("1) Name\n2) Date of Birth\n3) City\n4)
Account ID\n >> ");
scanf("%d",&c);
printf("Enter the search term/ID: ");
fflush(stdin);
gets(ele);
pos=search(start,ele,c);
if(pos!=-1)
{
for(ptr=start;ptr!=NULL&&pos!=ptr>accid;ptr=ptr->next);
printf("------------------------------\n");
>mobno);
printf("-------------------------------\n");
}
else
{
printf("No results found.");
}
getch();
break;
case 7:
exit(0);
default:
printf("Invalid Choice.");
break;
Number
}
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
clrscr();
}while(ch!=7);
printf("%11d ",ptr->accid);
printf("%14s ",ptr->name);
printf("%10s ",ptr->dob);
printf("%11s",ptr->city);
printf(" %14s\n",ptr->mobno);
}
struct node *input(struct node *newn,int id)
{
fflush(stdin);
printf("-----------------------------------\n");
printf("Account ID : %d\n",id);
newn->accid=id;
printf("Name : ");
gets(newn->name);
printf("Date of Birth : ");
gets(newn->dob);
printf("City : ");
gets(newn->city);
printf("Mobile Number: ");
gets(newn->mobno);
}
}
struct node *insert(struct node *start,int pos)
{
int ctr;
struct node *newnode,*ptr,*preptr;
return newn;
}
if(pos==1)
{
newnode->next=start;
start=newnode;
printf("Account inserted.");
}
else
{
for(ptr=start,ctr=2;ctr<pos&&ptr!=NULL;ptr=ptr->next,ctr++);
for(i=0;i<n;i++)
{
id++;
newnode=(struct node *)malloc(sizeof(struct node));
if(!newnode)
{
printf("Overflow!");
return NULL;
}
newnode->next=ptr->next;
ptr->next=newnode;
printf("Account inserted");
}
for(ptr=newnode->next;ptr!=NULL;ptr=ptr->next)
(ptr->accid)++;
return start;
newnode=input(newnode,id);
}
if(start==NULL)
{
newnode->next=NULL;
start=newnode;
}
else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
if(pos==1)
{
t=start;
start=start->next;
free(t);
printf("Account deleted.");
for(t=start;t!=NULL;t=t->next)
(t->accid)--;
ptr->next=newnode;
newnode->next=NULL;
}
}
return start;
}
void display(struct node *start)
{
struct node *ptr;
}
else
{
for(ptr=start,ctr=2;ctr<pos&&ptr->next!=NULL;ptr=ptr>next,ctr++);
if(start==NULL)
{
printf("The Linked list is empty.");
return;
}
if(ptr->next==NULL)
{
printf("Account ID doesnt exist.");
return start;
}
printf("------------------------------------------------------------------------\n");
t=ptr->next;
ptr->next=(ptr->next)->next;
free(t);
printf("Account deleted.");
14
for(t=ptr->next;t!=NULL;t=t->next)
(t->accid)--;
}
return start;
//Assignment 4
//Data Structures Lab
//By: Bala Kumar
}
int search(struct node *start,char ele[],int ch)
{
struct node *ptr;
int elem;
switch(ch)
{
case 1:
for(ptr=start;strcmp(ptr->name,ele)!=0&&ptr!=NULL;ptr=ptr>next);
if(ptr==NULL)
return -1;
else
return ptr->accid;
case 2:
for(ptr=start;strcmp(ptr->dob,ele)!=0&&ptr!=NULL;ptr=ptr>next);
if(ptr==NULL)
return -1;
else
return ptr->accid;
case 3:
for(ptr=start;strcmp(ptr->city,ele)!=0&&ptr!=NULL;ptr=ptr>next);
if(ptr==NULL)
return -1;
else
return ptr->accid;
case 4:
elem=atoi(ele);
for(ptr=start;elem!=ptr->accid&&ptr!=NULL;ptr=ptr->next);
if(ptr==NULL)
return -1;
else
return ptr->accid;
}
}
struct node *delall(struct node *start)
{
struct node *ptr,*preptr;
while(preptr!=start)
{
for(preptr=ptr=start;ptr->next!=NULL;ptr=ptr->next)
preptr=ptr;
free(ptr);
preptr->next==NULL;
}
//****************************************************
// Program: Managing accounts with Doubly Linked-list
//****************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct node
{
char regno[20];
int roll;
char name[15];
char dob[11];
char city[12];
char mobno[15];
char emailid[40];
struct node *prev;
struct node *next;
}*start=NULL;
struct node *create(struct node *);
void display(struct node *);
struct node *insert(struct node *,int);
struct node *input(struct node *,int);
struct node *del(struct node *,int);
void navigate(struct node *);
int validate(struct node *);
char *genRegNumber(int);
void main()
{
int ele,pos,ch,i,c;
char elem[20];
struct node *ptr;
clrscr();
do
{
printf("Your options are:\n");
printf("1) Creation\n2) Insertion\n3) Deletion\n4)
Navigate\n5) Show All\n6) Searching\n7) Exit\n >> ");
scanf("%d",&ch);
switch(ch)
{
case 1:
start=create(start);
break;
case 2:
printf("Please enter rollno. of the new entry:\n");
scanf("%d",&pos);
start=insert(start,pos);
getch();
break;
}
case 3:
if(start==NULL)
}while(ch!=7);
printf("The database is empty.There's
break;
char *getRegNumber(int n)
{
char regno[20]="MUJ",num[10];
15
nothing to delete.\n");
}
clrscr();
printf("Delete by:");
printf("1) Name\n2) Roll Number\n>> ");
scanf("%d",&c);
printf("Now, enter your search:\n");
fflush(stdin);
gets(elem);
pos=search(start,elem,c);
if(pos==-1)
{
printf("No results found.");
break;
}
start=del(start,pos);
getch();
break;
itoa(n,num,10);
switch(strlen(num))
{
case 1:
strcat(regno,"000");
strcat(regno,num);
break;
case 2:
strcat(regno,"00");
strcat(regno,num);
break;
case 3:
strcat(regno,"0");
strcat(regno,num);
break;
case 4:
navigate(start);
break;
case 5:
clrscr();
display(start);
getch();
break;
case 4:
strcat(regno,num);
break;
}
return regno;
}
case 6:
if(start==NULL)
{
printf("The database is empty.");
break;
}
clrscr();
printf("Search by:\n");
printf("1) Name \n2) Date of Birth\n3) City\n >>");
scanf("%d",&c);
printf("Enter your search:\n");
fflush(stdin);
gets(elem);
pos=search(start,elem,c);
if(pos==-1)
{
printf("No results found.");
break;
}
for(ptr=start;ptr!=NULL&&ptr->roll!=pos;ptr=ptr>next);
printf("-----------------------------------\n");
printf("Registration Number : %s\n",ptr->regno);
printf("Roll Number: %d\n",ptr->roll);
printf("Name : %s\n",ptr->name);
printf("Date of Birth : %s\n",ptr->dob);
printf("City : %s\n",ptr->city);
printf("Mobile Number: %s\n",ptr->mobno);
printf("Email ID :%s\n",ptr->emailid);
printf("-----------------------------------\n");
break;
case 7:
exit(0);
default:
printf("Invalid Option");
getch();
break;
}
clrscr();
//Validate Email
strcpy(email,nod->emailid);
for(i=0;i<strlen(email);i++)
{
if(email[i]=='@')
ctr++;
if(ctr>0&&email[i]=='.')
ctr2++;
case 3:
printf("\nInvalid Email ID.");
16
break;
}
getch();
printf("Please enter again:\n");
ctr--;
continue;
if(!isalpha(email[i])&&!isdigit(email[i])&&email[i]!='@'&&email[i]!='.'&&
email[i]!='_')
return 3;
}
}
if(ctr!=1)
return 3;
if(start==NULL)
{
start=newnode;
newnode->prev=NULL;
newnode->next=NULL;
}
else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
if(ctr2==0)
return 3;
return 0;
}
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
strcpy(newn->regno,getRegNumber(r));
newn->roll=r;
printf("----------------------------------------\n");
printf("Registration No. : %s\n",newn->regno);
printf("Roll No. : %d\n",newn->roll);
printf("Name : ");
fflush(stdin);
gets(newn->name);
printf("Date of Birth (DD/MM/YYYY) :");
gets(newn->dob);
printf("City : ");
gets(newn->city);
printf("Mobile Number (+91XXXXXXXXXX) :");
gets(newn->mobno);
printf("Email ID (xxxx@xxx.xxx) : ");
gets(newn->emailid);
return newn;
}
}
printf("Accounts Created.");
return start;
}
void display(struct node *start)
{
struct node *ptr;
if(start==NULL)
{
printf("Database is empty. Please create one first.");
return;
}
printf("--------------------------------------------------------------------------------");
printf(" Reg.No | Roll No | Name | DOB | City | Mobile
Number \n");
printf("--------------------------------------------------------------------------------\n");
}
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
printf("%7s ",ptr->regno);
printf(" %6d ",ptr->roll);
printf(" %12s ",ptr->name);
printf("%10s ",ptr->dob);
printf("%11s",ptr->city);
printf(" %14s\n",ptr->mobno);
}
}
struct node *insert(struct node *start, int pos)
{
struct node *ptr,*newnode;
int ctr;
char roll[20]="MUJ",n[10];
if(start==NULL)
{
printf("Database is empty. Please create obe furst");
return start;
}
newnode=(struct node *)malloc(sizeof(struct node));
newnode=input(newnode,pos);
if(validate(newnode))
{
switch(validate(newnode))
{
case 1:
printf("\nInvalid Date.");
break;
case 2:
printf("\nInvalid Mobile Number.");
break;
case 3:
printf("\nInvalid Email ID.");
break;
}
getch();
return start;
}
if(pos==1)
{
newnode->next=start;
start->prev=newnode;
newnode->prev=NULL;
start=newnode;
ptr=newnode;
}
else
{
strcpy(ptr->regno,getRegNumber(ptr->roll));
}
17
printf("Student deleted.");
return start;
}
void navigate(struct node *start)
{
int ch;
struct node *ptr=start;
clrscr();
do
{
printf("------------------------------------------\n");
printf(" Registration Number : %s\n",ptr->regno);
printf(" Roll Number : %d\n",ptr->roll);
printf(" Name : %s \n",ptr->name);
printf(" Date of Birth : %s \n",ptr->dob);
printf(" City : %s\n",ptr->city);
printf(" Mobile Number : %s\n",ptr->mobno);
printf(" Email ID : %s\n",ptr->emailid);
printf("------------------------------------------\n");
printf("<-- Prev(1) | Exit (0) | Next (2) -->\n >> ");
scanf("%d",&ch);
for(ctr=2,ptr=start;ctr<pos&&ptr->next!=NULL;ptr=ptr>next);
switch(ch)
{
newnode->next=ptr->next;
if(ptr->next!=NULL)
(ptr->next)->prev=newnode;
ptr->next=newnode;
newnode->prev=ptr;
case 1:
if(ptr==start)
{
printf("This is the first student.");
getch();
break;
}
ptr=ptr->prev;
break;
}
for(ptr=ptr->next;ptr!=NULL;ptr=ptr->next)
{
(ptr->roll)++;
strcpy(ptr->regno,getRegNumber(ptr->roll));
}
printf("Student inserted.");
return start;
case 0:
break;
case 2:
if(ptr->next==NULL)
{
printf("This is the last student.");
getch();
break;
}
ptr=ptr->next;
break;
}
clrscr();
}while(ch!=0);
}
for(ctr=2,ptr=start;ptr->next!=NULL&&ctr<pos;ptr=ptr>next,ctr++);
if(ptr->next==NULL)
{
printf("Position out of bound.");
return start;
}
t=ptr->next;
(t->next)->prev=ptr;
ptr->next=t->next;
free(t);
ptr=ptr->next;
}
for(;ptr!=NULL;ptr=ptr->next)
{
(ptr->roll)--;
for(ptr=start;strcmp(ptr->city,ele)!=0&&ptr!=NULL;ptr=ptr>next);
getch();
18
break;
case 4:
elem=atoi(ele);
for(ptr=start;elem!=ptr->roll&&ptr!=NULL;ptr=ptr->next);
break;
}
if(ptr==NULL)
return -1;
else
return ptr->roll;
if(!newnode)
{
printf("Overflow");
exit(0);
}
newnode->data=ch;
if(top==NULL)
{
top=newnode;
newnode->next=NULL;
}
else
{
newnode->next=top;
top=newnode;
}
//Assignment 5
//Data Structures Lab
//By: Bala Kumar
//******************************************************************
// Program to check if the given string is a palindrome using stack (LL)
//******************************************************************
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
char data;
struct node *next;
}*top=NULL;
}
char pop()
{
char ele;
struct node *ptr;
if(top==NULL)
{
printf("Underflow!");
getch();
exit(0);
}
else
{
ele=top->data;
ptr=top;
top=top->next;
free(ptr);
}
return ele;
void push(char );
char pop();
void main()
{
char str[20],strrev[20];
int i=0,j=0;
clrscr();
printf("Enter your string:\n");
gets(str);
while(str[i]!='\0')
{
if(!isalpha(str[i]))
{
for(j=i;str[j]!='\0';j++)
str[j]=str[j+1];
}
else
{
if(isupper(str[i]))
str[i]=tolower(str[i]);
push(str[i]);
i++;
}
}
j=0;
while(top!=NULL)
{
strrev[j]=pop();
j++;
}
strrev[j]='\0';
if(strcmp(str,strrev)==0)
printf("The given string is a palindrome.");
else
void main()
{
int ch,i,ele;
struct node *ptr;
clrscr();
case 8:
start=delete_last(start);
getch();
break;
do
{
case 9:
start=delete_value(start);
getch();
break;
printf("------------------------------------------------------------------------------\n");
printf("
Linked List\n");
printf("------------------------------------------------------------------------------\n");
printf("Your options are:\n");
printf("1) Creation\n2) Display\n3) Insert at the
Beginning\n4) Insert at the End\n5) Insert after the given node.\n6) Insert before
the given node\n7) Delete the first node\n8) Delete the last node\n9) Delete the
node with the given data\n10) Search\n11) Exit\n >> ");
scanf("%d",&ch);
case 10:
printf("Please enter the data in the node you're
searching for:\n");
scanf("%d",&ele);
ptr=search(start,ele);
if(ptr)
printf("Node found.");
else
printf("Node not found.");
getch();
break;
switch(ch)
{
case 1:
if(start!=NULL)
{
printf("A List already exists. Please use
case 11:
exit(0);
default:
printf("Invalid Option.");
getch();
break;
}
start=create(start);
break;
case 2:
display(start);
getch();
break;
}
clrscr();
}while(ch!=11);
}
case 3:
if(start==NULL)
{
printf("You have to create a linked-list
first.");
getch();
break;
}
start=insert_beg(start);
break;
case 4:
if(start==NULL)
{
printf("You have to create a linked-list
first.");
getch();
break;
}
start=insert_end(start);
break;
case 5:
start=insert_after(start);
getch();
break;
case 6:
start=insert_before(start);
getch();
break;
start=start->next;
free(ptr);
printf("Node Deleted.");
return start;
if(start==NULL)
{
printf("The Linked list is empty.");
return;
}
printf("START --> ");
for(ptr=start;ptr->next!=NULL;ptr=ptr->next)
{
printf("%d --> ",ptr->data);
}
printf("%d --> END",ptr->data);
}
struct node *delete_last(struct node *start)
{
struct node *ptr,*preptr;
if(!start)
{
printf("List empty. There's nothing to delete.");
return start;
}
for(preptr=ptr=start;ptr->next!=NULL;ptr=ptr->next)
preptr=ptr;
}
struct node *insert_beg(struct node *start)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(!newnode)
{
printf("Overflow!");
return start;
}
printf("Please enter the data:\n");
scanf("%d",&newnode->data);
20
preptr->next=NULL;
free(ptr);
printf("Node deleted.");
return start;
}
struct node *search(struct node *start,int ele)
{
struct node *ptr;
ptr=start;
printf("Node Inserted.");
getch();
if(ele==start->data)
return start;
do
{
ptr=ptr->next;
if(ptr->data==ele)
return ptr;
return start;
}while(ptr->next!=NULL);
newnode->next=start;
start=newnode;
}
return NULL;
struct node *insert_end(struct node *start)
{
struct node *newnode,*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
if(!newnode)
{
printf("Overflow!");
return start;
}
}
struct node *delete_value(struct node *start)
{
struct node *ptr,*preptr;
int ele;
if(!start)
{
printf("List empty. There's nothing to delete.");
return start;
}
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=newnode;
newnode->next=NULL;
printf("Node Inserted.");
getch();
return start;
}
struct node *delete_first(struct node *start)
{
struct node *ptr;
ptr=start;
if(!start)
{
printf("List empty. There's nothing to delete.");
return start;
for(preptr=ptr=start;ptr->data!=ele&&ptr!=NULL;ptr=ptr->next)
preptr=ptr;
if(!ptr) //Condition if ptr reaches the end and the node isnt found
{
printf("Node not found.");
return start;
}
else if(ptr==start) //Condition if the node is the first.
{
start=start->next;
free(ptr);
printf("First node deleted.");
return start;
}
preptr->next=ptr->next;
free(ptr);
printf("Node Deleted.");
return start;
}
struct node *insert_after(struct node *start)
{
struct node *ptr,*preptr,*newnode;
int ele;
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter the data in the node after which you want to insert the
new node:\n");
scanf("%d",&ele);
for(preptr=ptr=start;preptr->data!=ele&&preptr!=NULL;ptr=ptr->next)
preptr=ptr;
if(preptr==NULL)
{
printf("A node matching the entered data not found.\n");
return start;
}
//*************************************
21
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int QUEUE[MAX];
int front=-1,rear=-1;
void insert(int);
void display();
void del();
void main()
{
int ele,ch,i;
clrscr();
do
{
newnode->next=preptr->next;
preptr->next=newnode;
printf("Node inserted");
return start;
switch(ch)
{
case 1:
printf("Please enter the element:\n");
scanf("%d",&ele);
insert(ele);
break;
case 2:
del();
break;
case 3:
display();
break;
for(preptr=ptr=start;ptr->data!=ele&&ptr!=NULL;ptr=ptr->next)
preptr=ptr;
case 4:
exit(0);
if(ptr==NULL)
{
printf("Node not found.");
return start;
}
default:
printf("Invalid Option.");
break;
}
getch();
clrscr();
}while(ch!=4);
// *************************************
// Circular Queue
if(rear==-1)
{
printf("Underflow!");
return;
}
else if(front==rear)
front=rear=-1;
else
{
front=(front+1)%MAX;
}
printf("An item deleted.");
}
void display()
{
int i;
printf("FRONT --> ");
for(i=front;i!=rear;i=(i+1)%MAX)
printf("%d --> ",QUEUE[i]);
printf("%d --> REAR",QUEUE[rear]);
}
22