Page 1
//Lab 2//Data Structures//** Array Operations **//13/08/2015
//******************************//By : Bala Kumar//******************************
#include<stdio.h>#include<conio.h>#include<stdlib.h>
void main(){
int a[50],n=0,ch,ele,pos,flag=0,i;clrscr();
do{
printf("Your options are:\n");printf("1)Creation\n2)Insertion\n3)Deletion\n4)Searching\
n5)Show Array\n6)Exit\n");printf(">> ");scanf("%d",&ch);
switch(ch){
//Creation
case 1:if(n){
printf("Array already exists.\n");break;
}printf("Please enter the size of the Array(<50):\
n");do{
scanf("%d",&n);if(n>=50)
printf("Array Size cannot 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;
//Insertion
case 2:if(!n){
printf("Array is empty");break;
}
printf("Please enter the element you want to insert:\n");
scanf("%d",&ele);
printf("Enter the position:\n");scanf("%d",&pos);
pos-=1; //Converting the input position into arr index
if(pos>=n){
printf("Given position is out of bound.\n");
break;}
for(i=n;i>=pos;i--)a[i+1]=a[i];
a[i]=ele;n++;
printf("Element Inserted.\n");break;
//Deletion
case 3:if(!n){
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
if(pos>=n){
printf("Given position is out of bound.\n");
break;}for(i=pos;i<n;i++)
a[i]=a[i+1];n--;
printf("Element Deleted.\n");break;
//Search
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;
}}
if(flag)printf("Element found @ position %d
(First Match).",pos+1);else
printf("Element not found!");
break;
//Array Display
case 5:if(!n){
printf("Array is empty");
Page 2
break;}printf("Array: ");for(i=0;i<n-1;i++)
printf("%d,",a[i]);printf("%d",a[n-1]);break;
//Exit Loop
case 6:exit(0);
//Invalid Choice
default:printf("Invalid Option.");break;
}getch();clrscr();fflush(stdin);
}while(ch!=6);}
//Lab 3//Data Structures Lab//Singly Linked List - Creation,Insertion,Deletion & Display.//** By: Bala Kumar **//** 20/08/2015 **
#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;
clrscr();
do{
printf("Your options are:\n");printf("1) Creation\n2) Display\n3) Insert\n4) Delete\n5)
Exit\n");scanf("%d",&ch);
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);
default:printf("Invalid Choice");getch();break;
}
clrscr();}while(ch!=5);
}
struct node *create(struct node *start)
Page 3
{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);
ptr->next=newnode;newnode->next=NULL;
}scanf("%d",&data);
}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
{for(ptr=start,ctr=2;ctr<pos&&ptr!=NULL;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;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;
}
t=ptr->next;ptr->next=(ptr->next)->next;free(t);
}printf("Node deleted.");return start;
}
//Lab 4//Data Structures Lab
//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;
clrscr();
do
Page 4
{printf("Your options are:\n");printf("1) Creation\n2) Display\n3) Insert\n4) Delete\n5)
Exit\n");scanf("%d",&ch);
switch(ch){
case 1:start=create(start);break;
case 2:display(start);getch();break;
case 3: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: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);
default:printf("Invalid Choice");break;
}
clrscr();}while(ch!=5);
}
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);
ptr->next=newnode;newnode->next=start;
}scanf("%d",&data);
}
return start;}
void display(struct node *start){
struct node *ptr;
if(start==NULL){
printf("The Linked list is empty.");return;
}
printf("START --> ");
ptr=start;do{
printf("%d --> ",ptr->data);ptr=ptr->next;
}while(ptr!=start);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){
for(ptr=start;ptr->next!=start;ptr=ptr->next);ptr->next=newnode;newnode->next=start;start=newnode;
}else{
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++);
if(ptr->next==start){
printf("Position out of bound.");return start;
Page 5
}
t=ptr->next;ptr->next=(ptr->next)->next;free(t);
}printf("Node deleted.");return start;
}
//Lab 4//Data Structures Lab
//By: Bala Kumar
//*********************************// Doubly Linked List - Operations//*********************************
#include<stdio.h>#include<conio.h>#include<stdlib.h>
struct node{
int data;struct node *prev,*next;
}*start=NULL;
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(){
int ele,ch,pos,index;
clrscr();
do{
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;
}
start=create(start);break;
case 2:if(start==NULL){
printf("The list is empty.");getch();break;
}display(start);getch();break;
case 3:if(start==NULL){
printf("The list is empty. There's nothing to delte");
getch();break;
}
printf("Please enter the position of the node you with to delete:\n");
scanf("%d",&pos);
start=del(start,pos);getch();break;
case 4:printf("Please enter the position of the new
node:\n");scanf("%d",&pos);
start=insert(start,pos);getch();break;
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: %d.",index);
elseprintf("No results found.");
getch();break;
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;
}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);
Page 6
do{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(start==NULL){
start=newnode;newnode->prev=NULL;newnode->next=NULL;
}else{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=newnode;newnode->prev=ptr;newnode->next=NULL;
}scanf("%d",&ele);
}while(ele!=-1);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");
}
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;
newnode=(struct node*)malloc(sizeof(struct node));printf("Now, please enter the data in the node:\n");scanf("%d",&newnode->data);
if(pos==1){
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++);
newnode->next=ptr->next;ptr->next=newnode;newnode->prev=ptr;if(newnode->next!=NULL)
(newnode->next)->prev=newnode;}printf("Node inserted.");return start;
}
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;}
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;
}
Page 7
ptr=ptr->next;
break;}clrscr();
}while(ch!=2);
}//Lab 4//Data Structures Lab
//By: Bala Kumar
//*******************************// Stack - Using Array//*******************************
#include<stdio.h>#include<stdlib.h>#include<conio.h>
int push(int);int pop();void display();
int STACK[50],top=-1;
void main(){
int ele,i,ch;
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: ");scanf("%d",&ele);if(push(ele))
printf("Successfully Inserted.");else
printf("Overflow!");
break;
case 2:if(pop())
printf("One item successfully removed.");
elseprintf("Underflow.");
break;
case 3:display();break;
case 4:exit(0);break;
}getch();clrscr();
}while(ch!=4);}
int push(int ele){
if(top>=50)return 0;
else{
top++;STACK[top]=ele;return 1;
}}
int pop(){
if(top==-1)return 0;
else{
top--;return 1;
}}
void display(){
int i;
if(top==-1){
printf("No elements in stack.");return;
}printf("\n| |\n");
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//***********************************
#include<stdio.h>#include<conio.h>#include<stdlib.h>
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");
Page 8
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;
case 2:if(pop())
printf("One element successfully removed.");
elseprintf("Underflow");
break;
case 3:display();break;
case 4:exit(0);
}getch();clrscr();
}while(ch!=4);}
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;
}}
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");
}
//Lab 6//Data Structures Lab//By: Bala Kumar
//***********************************************// 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;
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 '-':
Page 9
res=op2-op1;break;
case '*':res=op1*op2;break;
case '/':res=op2/op1;break;
}push(st,res);i++;
}else{
printf("Invalid Expression");getch();exit(0);
}}return pop(st);
}
float pop(float st[]){
char val;
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;
}}
//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()
{int n;
clrscr();
printf("Please enter the number of discs:\n");scanf("%d",&n);
move(n,'A','C','B');getch();
}
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);
}}
//Assignment 2//Data Structures Lab//** A.Bala Kumar **//13/08/2015
//********************************************//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];
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
Page 10
case 1:printf("How many accounts do you want to
create?(<30)\n");scanf("%d",&size);
create(ppl,size);break;
//Insertioncase 2:insert(ppl,&size);break;
//Deletioncase 3:
if(!size){
printf("The database is empty. Nothing to delete.\n");
break;}
clrscr();printf("Delete by:\na) Account ID\nb) Name\nc)
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: %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;
//Searchingcase 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);
gets(ele);index=search(ppl,size,ele,c);if(index!=-1){
printf("-------------------------\n");printf("Account %d\n",index+1);printf("-------------------------\n");printf("Account ID: %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{
printf("Person not found!");break;
}
//Accounts Display
case 5:showall(ppl,size);printf("\nPlease press a key to clear..");break;
//Exit Optioncase 6:exit(0);break;
default:printf("Invalid Option.\n");break;
}getch();fflush(stdin);clrscr();
}while(ch!=6);}
void create(accounts pl[],int n){
int i;if(n>=30){
printf("You cant create accounts more than 30.\n");return;
}
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);
}
Page 11
printf("Account(s) Created.\n");}
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");
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);
}}
void insert(accounts pl[],int *si){
int m,i,n=*si;
if(!*si){
printf("You have to create a db first.\n");return;
}
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){
case 'a':elem=atoi(ele);for(i=0;i<n;i++){
if(pl[i].accid==elem)return i;
}return -1;
case 'b':for(i=0;i<n;i++){
if(strcmp(ele,pl[i].name)==0)return i;
}return -1;
case 'c':for(i=0;i<n;i++){
if(strcmp(ele,pl[i].dob)==0)return i;
}return -1;
}
}
void del(accounts pl[],int *n,int index){
int i,m=*n;
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)--;
}
//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 *);
Page 12
struct node *insert(struct node*,int);struct node *del(struct node*,int);struct node *delall(struct node *);struct node *input(struct node*,int);
void main(){
int ch,pos,c;char ele[20];struct node *ptr;
clrscr();
do{
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);
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 list first.");
getch();break;
}
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;
}
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");
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->mobno);
printf("-------------------------------\n");start=del(start,pos);
}else
printf("No results matching found.");getch();break;
case 5:if(start==NULL){
printf("The list is already empty.");getch();break;
}
start=delall(start);printf("List is cleared.");getch();break;
case 6:if(start==NULL){
printf("The list is empty. Please create one first.");
getch();break;
}clrscr();printf("Search 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->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-
>mobno);printf("-------------------------------\n");
}else{
printf("No results found.");}getch();break;
case 7:exit(0);
Page 13
default:printf("Invalid Choice.");break;
}
clrscr();}while(ch!=7);
}
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);
return newn;}
struct node *create(struct node *start){
struct node *newnode,*ptr;int id=0,n,i;
printf("Please enter the number of accounts you wish to create:\n");scanf("%d",&n);
for(i=0;i<n;i++){
id++;newnode=(struct node *)malloc(sizeof(struct node));if(!newnode){
printf("Overflow!");return NULL;
}
newnode=input(newnode,id);
if(start==NULL){
newnode->next=NULL;start=newnode;
}else{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=newnode;newnode->next=NULL;
}}return start;
}
void display(struct node *start){
struct node *ptr;
if(start==NULL){
printf("The Linked list is empty.");return;
}
printf("------------------------------------------------------------------------\n");
printf(" Account ID | Name | DOB | City | Mobile Number \n");
printf("------------------------------------------------------------------------\n");
for(ptr=start;ptr!=NULL;ptr=ptr->next){
printf("%11d ",ptr->accid);printf("%14s ",ptr->name);printf("%10s ",ptr->dob);printf("%11s",ptr->city);printf(" %14s\n",ptr->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);
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++);
newnode->next=ptr->next;ptr->next=newnode;printf("Account inserted");
}
for(ptr=newnode->next;ptr!=NULL;ptr=ptr->next)(ptr->accid)++;
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);printf("Account deleted.");for(t=start;t!=NULL;t=t->next)
(t->accid)--;}else{
for(ptr=start,ctr=2;ctr<pos&&ptr->next!=NULL;ptr=ptr->next,ctr++);
if(ptr->next==NULL){
printf("Account ID doesnt exist.");return start;
}
t=ptr->next;
Page 14
ptr->next=(ptr->next)->next;free(t);printf("Account deleted.");
for(t=ptr->next;t!=NULL;t=t->next)(t->accid)--;
}
return start;}
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;
elsereturn ptr->accid;
case 2:for(ptr=start;strcmp(ptr->dob,ele)!=0&&ptr!=NULL;ptr=ptr-
>next);
if(ptr==NULL)return -1;
elsereturn ptr->accid;
case 3:for(ptr=start;strcmp(ptr->city,ele)!=0&&ptr!=NULL;ptr=ptr-
>next);
if(ptr==NULL)return -1;
elsereturn ptr->accid;
case 4:elem=atoi(ele);for(ptr=start;elem!=ptr->accid&&ptr!=NULL;ptr=ptr->next);
if(ptr==NULL)return -1;
elsereturn 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;
}
}
//Assignment 4//Data Structures Lab//By: Bala Kumar
//****************************************************// 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)
Page 15
{printf("The database is empty.There's
nothing to delete.\n");break;
}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;
case 4:navigate(start);break;
case 5:clrscr();display(start);getch();break;
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();
}while(ch!=7);}
char *getRegNumber(int n){
char regno[20]="MUJ",num[10];
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:strcat(regno,num);break;
}return regno;
}
int validate(struct node *nod){
int i,ctr=0,ctr2=0;
char date[20],mobno[20],email[20];
//Validate Datestrcpy(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 Numberstrcpy(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 Emailstrcpy(email,nod->emailid);
for(i=0;i<strlen(email);i++){
if(email[i]=='@')
Page 16
ctr++;if(ctr>0&&email[i]=='.')
ctr2++;if(!isalpha(email[i])&&!isdigit(email[i])&&email[i]!
='@'&&email[i]!='.'&&email[i]!='_')return 3;
}
if(ctr!=1)return 3;
if(ctr2==0)return 3;
return 0;}
struct node *input(struct node *newn,int r){
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 ([email protected] ) : ");gets(newn->emailid);return newn;
}
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;
case 3:
printf("\nInvalid Email ID.");break;
}getch();printf("Please enter again:\n");ctr--;continue;
}
if(start==NULL){
start=newnode;newnode->prev=NULL;newnode->next=NULL;
}else{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=newnode;newnode->prev=ptr;newnode->next=NULL;
}}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.");
Page 17
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{
for(ctr=2,ptr=start;ctr<pos&&ptr->next!=NULL;ptr=ptr->next);
newnode->next=ptr->next;if(ptr->next!=NULL)
(ptr->next)->prev=newnode;ptr->next=newnode;newnode->prev=ptr;
}for(ptr=ptr->next;ptr!=NULL;ptr=ptr->next){
(ptr->roll)++;strcpy(ptr->regno,getRegNumber(ptr->roll));
}printf("Student inserted.");return start;
}
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{
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)--;strcpy(ptr->regno,getRegNumber(ptr->roll));
}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);
switch(ch){
case 1:if(ptr==start){
printf("This is the first student.");getch();break;
}ptr=ptr->prev;break;
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);
}
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:
Page 18
for(ptr=start;strcmp(ptr->city,ele)!=0&&ptr!=NULL;ptr=ptr->next);
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;}
//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;
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
printf("The given string isnt a palindrome.");getch();
}
void push(char ch){
struct node *newnode;newnode=(struct node*)malloc(sizeof(struct node));
if(!newnode){
printf("Overflow");exit(0);
}
newnode->data=ch;if(top==NULL){
top=newnode;newnode->next=NULL;
}else{
newnode->next=top;top=newnode;
}}
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;
}
// 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 *);
Page 19
struct node *search(struct node *,int);
void main(){
int ch,i,ele;struct node *ptr;clrscr();
do{
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);
switch(ch){
case 1:if(start!=NULL){
printf("A List already exists. Please use insert to add new nodes.");
getch();break;
}start=create(start);break;
case 2:display(start);getch();break;
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;
case 7:start=delete_first(start);getch();break;
case 8:start=delete_last(start);getch();break;
case 9:start=delete_value(start);getch();break;
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;
case 11:exit(0);
default:printf("Invalid Option.");getch();break;
}
clrscr();}while(ch!=11);
}
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;
Page 20
}
void display(struct node *start){
struct node *ptr;
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 *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);
newnode->next=start;start=newnode;
printf("Node Inserted.");getch();
return start;}
struct node *insert_end(struct node *start){
struct node *newnode,*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
if(!newnode){
printf("Overflow!");return start;
}
printf("Enter the data: ");scanf("%d",&newnode->data);
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;
}
start=start->next;free(ptr);printf("Node Deleted.");return start;
}
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;
preptr->next=NULL;free(ptr);printf("Node deleted.");return start;
}
struct node *search(struct node *start,int ele){
struct node *ptr;
ptr=start;
if(ele==start->data)return start;
do{
ptr=ptr->next;if(ptr->data==ele)
return ptr;
}while(ptr->next!=NULL);
return NULL;}
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 in the node you want to delete: ");scanf("%d",&ele);
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);
Page 21
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;
}
printf("Enter the data for the new node:\n");scanf("%d",&newnode->data);
newnode->next=preptr->next;preptr->next=newnode;printf("Node inserted");return start;
}
struct node *insert_before(struct node *start){
struct node *newnode,*ptr,*preptr;int ele;
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);
for(preptr=ptr=start;ptr->data!=ele&&ptr!=NULL;ptr=ptr->next)preptr=ptr;
if(ptr==NULL){
printf("Node not found.");return start;
}
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;
}
// *************************************// Circular Queue
//*************************************
#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("Your choices are:\n");printf("1) Insertion\n2) Deletion\n3) Display\n4) Exit\n >> ");scanf("%d",&ch);
switch(ch){
case 1:printf("Please enter the element:\n");scanf("%d",&ele);insert(ele);break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Invalid Option.");break;
}getch();clrscr();
}while(ch!=4);}
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(){
if(rear==-1){
Page 22
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]);
}