Sei sulla pagina 1di 22

//Lab 2

//Data Structures
//** Array Operations **
//13/08/2015

printf("Given position is out of


bound.\n");
break;

//******************************
//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("Your options are:\n");


printf("1)Creation\n2)Insertion\n3)Deletion\n4)Searching\n5)Show
Array\n6)Exit\n");
printf(">> ");
scanf("%d",&ch);

}
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

printf("Given position is out of


bound.\n");

case 1:
if(n)
{

break;
}
for(i=pos;i<n;i++)
a[i]=a[i+1];
n--;

printf("Array already exists.\n");


break;
}
printf("Please enter the size of the

printf("Element Deleted.\n");
break;

Array(<50):\n");
do
{

//Search
scanf("%d",&n);
if(n>=50)
printf("Array Size cannot

case 4:
if(!n)
{

exceed 50, please re-enter:\n");


}while(n>=50);
printf("Now, enter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array Created.\n");
break;

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);

printf("Element not found!");


break;
//Array Display

pos-=1; //Converting the input position into arr


index
if(pos>=n)
{

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;

struct node *create(struct node *);


void display(struct node *);
struct node *insert(struct node*,int);
struct node *del(struct node*,int);

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);

//By : Bala Kumar


//***********************************
// Cirularly Linked List - Operations
//***********************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
struct node *create(struct node *);
void display(struct node *);
struct node *insert(struct node*,int);
struct node *del(struct node*,int);
void main()
{
int ch,pos;

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;

printf("START --> ");


ptr=start;
do
{

case 3:
printf("Please enter the position in which you

printf("%d --> ",ptr->data);


ptr=ptr->next;
}while(ptr!=start);
printf("END");

want to create a new node:\n");


scanf("%d",&pos);
start=insert(start,pos);
getch();
break;
case 4:
printf("Please enter the position of the node you
wish to delete:\n");

}
struct node *insert(struct node *start,int pos)
{
int ctr;
struct node *newnode,*ptr;

scanf("%d",&pos);
start=del(start,pos);
getch();
break;

newnode=(struct node*)malloc(sizeof(struct node));


printf("Enter the data: ");
scanf("%d",&newnode->data);

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++);

struct node *create(struct node *start)


{
struct node *newnode,*ptr;
int data;
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)
{
start=newnode;
newnode->next=start;
}
else
{
for(ptr=start;ptr->next!=start;ptr=ptr->next);

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;

//By: Bala Kumar


//*********************************
// Doubly Linked List - Operations
//*********************************

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:

struct node *create(struct node*);


void show(struct node *);
void display(struct node*);
struct node *del(struct node*,int);
struct node *insert(struct node*,int);
int search(struct node *,int);
void main()
{
%d.",index);

else

int ele,ch,pos,index;

printf("No results found.");


clrscr();
getch();
break;

do
{

case 6:
if(start==NULL)
{
printf("The list is empty.");
getch();
break;
}
show(start);
break;

printf("Your options are:\n");


printf("1) Creation\n2) Show All\n3) Deletion\n4)
Insertion\n5) Searching\n6) Navigate (Show an Element)\n7) Exit\n >> ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(start!=NULL)
{
printf("A list already exists. Please use
insert option to add new nodes.\n");
getch();
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);

newnode=(struct node*)malloc(sizeof(struct node));


printf("Now, please enter the data in the node:\n");
6
scanf("%d",&newnode->data);

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;

void show(struct node *start)


{
clrscr();
struct node *ptr=start;
int ctr=1,ch;
do
{
printf(">>> Data : %d | Index = %d >>>",ptr->data,ctr);
printf("\n<- (1) Previous | (2) Exit | (3) Next ->\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(ptr==start)
{
printf("This is the first element.");
getch();
break;
}
ptr=ptr->prev;
break;
case 2:
break;
case 3:
if(ptr->next==NULL)
{
printf("This is the last element.");
getch();
break;
}

ptr=ptr->next;
break;
}
clrscr();
}while(ch!=2);
}
//Lab 4
//Data Structures Lab
//By: Bala Kumar

int push(int ele)


{
if(top>=50)
return 0;
else
{
top++;
STACK[top]=ele;
return 1;
}
}
int pop()
{

//*******************************
// 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;

printf("No elements in stack.");


return;
}
printf("\n| |\n");

clrscr();
do
{

for(i=top;i>=0;i--)
{
printf("|-----|\n");
printf("| %3d |\n",STACK[i]);
}
printf("|_____|\n");

printf("Your options are:\n");


printf("1) Insertion\n2) Deletion\n3) Show Stack\n4) Exit\n
>> ");
scanf("%d",&ch);
}
switch(ch)
{
case 1:
printf("Please enter the element: ");
scanf("%d",&ele);
if(push(ele))
printf("Successfully Inserted.");
else
printf("Overflow!");

//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");

printf("1) Insertion\n2) Deletion\n3) Show Stack\n4) Exit\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);

void move(int n,char source,char dest,char spare)


{
if(n==1)
printf("Move one disk from %c to %c",source,dest);
else
{
move(n-1,source,spare,dest);
move(1,source,dest,spare);
move(n-1,spare,dest,source);
}
}

}
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

printf("Person not found!");


break;

to delete.\n");
break;
}

clrscr();
printf("Delete by:\na) Account ID\nb) Name\nc)

//Accounts Display

Date of Birth\n >> ");


fflush(stdin);
scanf("%c",&c);
printf("Enter the search term/ID: ");
fflush(stdin);
gets(ele);
index=search(ppl,size,ele,c);
if(index==-1)
{
printf("ID not found.");
break;
}
else
{
printf("-------------------------\n");
printf("Account %d\n",index+1);
printf("-------------------------\n");
printf("Account ID:

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.");
}

void create(accounts pl[],int n)


{
int i;
if(n>=30)
{
printf("You cant create accounts more than 30.\n");
return;
}

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;
}

int search(accounts pl[],int n,char ele[],char ch)


{
int i,elem;
switch(ch)
{

//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 *);

struct node *insert(struct node*,int);


struct node *del(struct node*,int);
struct node *delall(struct node *);
struct node *input(struct node*,int);

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
{

printf("No results matching found.");


getch();
break;

printf("Your options are:\n");


printf("1) Creation\n2) Display\n3) Insert\n4) Delete\n5)
Delete All\n6) Search\n7) Exit\n");
scanf("%d",&ch);

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);

Account ID\n >> ");


scanf("%d",&c);
printf("Enter the search term/ID: ");
fflush(stdin);
gets(ele);
pos=search(start,ele,c);

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

printf(" Account ID | Name


| DOB
| City
| Mobile
\n");
13
printf("------------------------------------------------------------------------\n");

}
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;

newnode=(struct node*)malloc(sizeof(struct node));


newnode=input(newnode,pos);

return newn;
}

if(pos==1)
{

struct node *create(struct node *start)


{
struct node *newnode,*ptr;
int id=0,n,i;

newnode->next=start;
start=newnode;
printf("Account inserted.");
}
else
{

printf("Please enter the number of accounts you wish to create:\n");


scanf("%d",&n);

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);

struct node *del(struct node *start,int pos)


{
int ctr;
struct node *ptr,*t;

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();

int validate(struct node *nod)


{
int i,ctr=0,ctr2=0;
char date[20],mobno[20],email[20];
//Validate Date
strcpy(date,nod->dob);
if(strlen(date)!=10)
return 1;
for(i=0;i<10;i++)
{
if(i==2&&date[i]!='/')
return 1;
if(i==5&&date[i]!='/')
return 1;
if(!isdigit(date[i])&&date[i]!='/')
return 1;
}
//Validate Mobile Number
strcpy(mobno,nod->mobno);
if(strlen(mobno)!=13)
return 2;
if(mobno[0]!='+')
return 2;
for(i=1;i<13;i++)
{
if(!isdigit(mobno[i]))
return 2;
}

//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;
}

struct node *input(struct node *newn,int r)


{

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)
{

struct node *create(struct node *start)


{
struct node *ptr,*newnode;
int ctr,n;
if(start!=NULL)
{
printf("Database already exists. Please use insert function.");
getch();
return start;
}
printf("Please enter the number of entries:\n");
scanf("%d",&n);
for(ctr=1;ctr<=n;ctr++)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode=input(newnode,ctr);
if(validate(newnode))
{
switch(validate(newnode))
{
case 1:
printf("\nInvalid Date.");
break;
case 2:
printf("\nInvalid Mobile Number.");
break;

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;

struct node *del(struct node *start,int pos)


{
struct node *ptr,*t;
int ctr;
if(pos==1)
{
t=start;
start=start->next;
start->prev=NULL;
ptr=start;
free(t);
}
else
{

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)--;

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);
break;
case 2:
for(ptr=start;strcmp(ptr->dob,ele)!=0&&ptr!=NULL;ptr=ptr>next);
break;
case 3:

printf("The given string isnt a palindrome.");

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;

void push(char ch)


{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));

}
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

// By: Bala Kumar


//*************************************************
//Singly Linked List - Operations by given element
//*************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
struct node *create(struct node *);
void display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *insert_after(struct node *);
struct node *insert_before(struct node *);
struct node *delete_first(struct node *);
struct node *delete_last(struct node *);
struct node *delete_value(struct node *);

struct node *search(struct node *,int);


case 7:
start=delete_first(start); 19
getch();
break;

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);

insert to add new nodes.");


getch();
break;

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;

struct node *create(struct node *start)


{
struct node *newnode,*ptr;
int data;
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);
ptr->next=newnode;
newnode->next=NULL;
}
scanf("%d",&data);
}
return start;

void display(struct node *start)


{
struct node *ptr;

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;
}

printf("Enter the data: ");


scanf("%d",&newnode->data);

printf("Enter the data in the node you want to delete: ");


scanf("%d",&ele);

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
{

printf("Enter the data for the new node:\n");


scanf("%d",&newnode->data);

printf("Your choices are:\n");


printf("1) Insertion\n2) Deletion\n3) Display\n4) Exit\n >> ");
scanf("%d",&ch);

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;

struct node *insert_before(struct node *start)


{
struct node *newnode,*ptr,*preptr;
int ele;

case 2:
del();
break;

newnode=(struct node*)malloc(sizeof(struct node));


printf("Please enter the data in the node before which you wish to insert
the new node:\n");
scanf("%d",&ele);

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);

printf("Now, please enter the data in the new node:\n");


scanf("%d",&newnode->data);
}
if(ptr==start)
{
start=newnode;
newnode->next=ptr;
printf("Node inserted at the beggining");
return start;
}
newnode->next=preptr->next;
preptr->next=newnode;
printf("Node Inserted.");
return start;
}

void insert(int ele)


{
if((rear+1)%MAX==front)
{
printf("Overflow!");
return;
}
else if(rear==-1)
front=rear=0;
else
rear=(rear+1)%MAX;
QUEUE[rear]=ele;
printf("Element inserted.");
}
void del()
{

// *************************************
// 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

Potrebbero piacerti anche