Anda di halaman 1dari 29

EC-319

PROGRAMMING
IV
Netaji Subhas Institute
Of Technology



Submitted by: SHUBHAM GUPTA
Roll No.: 141/EC/11
Section: ECE-3


PROGRAM-1
Generate a record of employees working in a firm using
pointers and structures.
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct emp
{
char *nam;
int num;
float sal;
};
void main()
{ struct emp *s, *s1;
clrscr();
int n;
void read(emp *, int);
void display(emp *, int);
cout<<"enter the number of employees"<<endl;
cin>>n;
s=s1;read(s,n);
s=s1;display(s,n);
getch();
}
void read(emp *t, int n)
{ int i;
for(i=0; i<n; i++)
{ cout<<"enter the employee's name"<<endl;
gets((t+i)->nam);
cout<<endl;
cout<<"enter the employee's id number"<<endl;
cin>>(t+i)->num; cout<<endl;
cout<<"enter the employee's salary"<<endl;
cin>>(t+i)->sal; cout<<endl;
}
}
void display(emp *s, int n)
{ int i;
for(i=0; i<n;i++)
{
cout<<"the employee's "<<i+1<<" record is:"<<endl;
cout<<"name:";
puts((s+i)->nam);
cout<<endl;
cout<<"id number:"<<(s+i)->num<<endl;
cout<<"salary:"<<(s+i)->sal<<endl; } }



PROGRAM-2
Generate an account book for an account holder in a
bank using pointers and structures.
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct bank
{
char *name;
int num;
float amt;
};
void main()
{ struct bank *s, *s1;
clrscr();
int n;
void read(bank *, int);
void display(bank *, int);
cout<<"enter the number of account holders"<<endl;
cin>>n;
s=s1;read(s,n);
s=s1;display(s,n);
getch();
}
void read(bank *t, int n)
{ int i;
for(i=0; i<n; i++)
{ cout<<"enter the account holder name"<<endl;
gets((t+i)->name);
cout<<endl;
cout<<"enter the holder's account number"<<endl;
cin>>(t+i)->num; cout<<endl;
cout<<"enter the holder's amount in bank"<<endl;
cin>>(t+i)->amt; cout<<endl;
}
}
void display(bank *s, int n)
{ int i;
for(i=0; i<n;i++)
{
cout<<"the holder's "<<i+1<<" record is:"<<endl;
cout<<"name:";
puts((s+i)->name);
cout<<endl;
cout<<"account number:"<<(s+i)->num<<endl;
cout<<"amount in account:"<<(s+i)->amt<<endl;
}
}


PROGRAM-3
Create a file of different records related to a person in a
file. Access the information from the file and display it on
the screen.
Code:
#include<fstream.h>
#include<conio.h>
void main()
{ clrscr();
ofstream fout("person"); //connect person file to output stream fout
char name[20],ch;
int age=0,i;
for(i=0;i<5;i++)
{ cout<<"PERSON"<<(i+1)<<"\tName:";
cin.get(name,20);
cout<<"\tAge:";
cin>>age;
cin.get(ch); //to empty input buffer
fout<<name<<'\n'<<age<<'\n';
}
fout.close();
ifstream fin("person"); //connect person file to input stream fin
fin.seekg(0); //to bring pointer at the file beginning
cout<<"\n";
cout<<"Reading from file...";
for(i=0;i<5;i++)
{ fin.get(name,20);
fin.get(ch);
fin>>age;
fin.get(ch);
cout<<"\nPerson NAME:"<<name;
cout<<"\nAGE:"<<age<<"\n";
}
fin.close();
getch();
}












PROGRAM-4
Implement a circular linked list.
Code:
#include<stdio.h>
class Node
{
public:
int data;
Node* next;
Node(int n)
{data = n;
next = NULL;
}
};
void print(Node* start, int n)
{
if (n > 1)
{
printf("%d -> ", start -> data);
print(start -> next, n - 1);
}
else if (n == 1)
{
printf("%d -> start\n\n", start -> data);
}
}
int main()
{
int count = 0, n;
Node *start, *ins, *temp;
char ch;
printf("Enter Number To Be Inserted: ");
scanf("%d", &n);
start = new Node(n);
start -> next = start;
count++;
print(start, count);
printf("Do you want to enter more? (y/n): ");
getchar();
scanf("%c",&ch);
while( (ch == 'y') || (ch == 'Y') )
{
printf("Enter Number To Be Inserted: ");
scanf("%d", &n);
ins = new Node(n);
temp = start;
start = ins;
start -> next = temp;
count++;
print(start,count);
printf("Do you want to enter more? (y/n): ");
getchar();
scanf("%c",&ch);
}
return 0;
}
















PROGRAM-5
Transverse a tree(inorder, preorder, postorder).
Code:
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct tree_node
{ tree_node *left;
tree_node *right;
int data;
};
class bst
{ tree_node *root;
public:
bst()
{ root=NULL;
}
int isempty()
{ return(root==NULL);
}
void insert(int item);
void inordertrav();
void inorder(tree_node*);
void postordertrav();
void postorder(tree_node*);
void preordertrav();
void preorder(tree_node*);
};

void bst::insert(int item)
{ tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{ tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{ parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p; } }
void bst::inordertrav()
{ inorder(root);
}
void bst::inorder(tree_node *ptr)
{ if(ptr!=NULL)
{ inorder(ptr->left);
cout<<" "<<ptr->data<<" ";
inorder(ptr->right);
}
}
void bst::postordertrav()
{ postorder(root);
}
void bst::postorder(tree_node *ptr)
{ if(ptr!=NULL)
{ postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->data <<" ";
}
}
void bst::preordertrav()
{ preorder(root);
}
void bst::preorder(tree_node *ptr)
{ if(ptr!=NULL)
{ cout<<" "<<ptr->data <<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void main()
{ clrscr();
bst b;
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20);
cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
getch(); }







PROGRAM-6
Generate a B-Tree for a given input.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
#define MIN 2
struct btreeNode
{
int val[MAX + 1], count;
struct btreeNode *link[MAX + 1];
};
struct btreeNode *root;
/* creating new node */
struct btreeNode * createNode(int val, struct btreeNode *child)
{
struct btreeNode *newNode;
newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
newNode->val[1] = val;
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
}

/* Places the value in appropriate position */
void addValToNode(int val, int pos, struct btreeNode *node,
struct btreeNode *child) {
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
}
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
}

/* split the node */
void splitNode (int val, int *pval, int pos, struct btreeNode *node,
struct btreeNode *child, struct btreeNode **newNode) {
int median, j;

if (pos > MIN)
median = MIN + 1;
else
median = MIN;

*newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
j = median + 1;
while (j <= MAX) {
(*newNode)->val[j - median] = node->val[j];
(*newNode)->link[j - median] = node->link[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;

if (pos <= MIN) {
addValToNode(val, pos, node, child);
} else {
addValToNode(val, pos - median, *newNode, child);
}
*pval = node->val[node->count];
(*newNode)->link[0] = node->link[node->count];
node->count--;
}

/* sets the value val in the node */
int setValueInNode(int val, int *pval,
struct btreeNode *node, struct btreeNode **child) {

int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
}

if (val < node->val[1]) {
pos = 0;
} else {
for (pos = node->count;
(val < node->val[pos] && pos > 1); pos--);
if (val == node->val[pos]) {
printf("Duplicates not allowed\n");
return 0;
}
}
if (setValueInNode(val, pval, node->link[pos], child)) {
if (node->count < MAX) {
addValToNode(*pval, pos, node, *child);
} else {
splitNode(*pval, pval, pos, node, *child, child);
return 1;
}
}
return 0;
}

/* insert val in B-Tree */
void insertion(int val) {
int flag, i;
struct btreeNode *child;

flag = setValueInNode(val, &i, root, &child);
if (flag)
root = createNode(i, child);
}

/* copy successor for the value to be deleted */
void copySuccessor(struct btreeNode *myNode, int pos) {
struct btreeNode *dummy;
dummy = myNode->link[pos];

for (;dummy->link[0] != NULL;)
dummy = dummy->link[0];
myNode->val[pos] = dummy->val[1];

}

/* removes the value from the given node and rearrange values */
void removeVal(struct btreeNode *myNode, int pos) {
int i = pos + 1;
while (i <= myNode->count) {
myNode->val[i - 1] = myNode->val[i];
myNode->link[i - 1] = myNode->link[i];
i++;
}
myNode->count--;
}

/* shifts value from parent to right child */
void doRightShift(struct btreeNode *myNode, int pos) {
struct btreeNode *x = myNode->link[pos];
int j = x->count;

while (j > 0) {
x->val[j + 1] = x->val[j];
x->link[j + 1] = x->link[j];
}
x->val[1] = myNode->val[pos];
x->link[1] = x->link[0];
x->count++;

x = myNode->link[pos - 1];
myNode->val[pos] = x->val[x->count];
myNode->link[pos] = x->link[x->count];
x->count--;
return;
}

/* shifts value from parent to left child */
void doLeftShift(struct btreeNode *myNode, int pos) {
int j = 1;
struct btreeNode *x = myNode->link[pos - 1];

x->count++;
x->val[x->count] = myNode->val[pos];
x->link[x->count] = myNode->link[pos]->link[0];

x = myNode->link[pos];
myNode->val[pos] = x->val[1];
x->link[0] = x->link[1];
x->count--;

while (j <= x->count) {
x->val[j] = x->val[j + 1];
x->link[j] = x->link[j + 1];
j++;
}
return;
}

/* merge nodes */
void mergeNodes(struct btreeNode *myNode, int pos) {
int j = 1;
struct btreeNode *x1 = myNode->link[pos], *x2 = myNode->link[pos -
1];

x2->count++;
x2->val[x2->count] = myNode->val[pos];
x2->link[x2->count] = myNode->link[0];

while (j <= x1->count) {
x2->count++;
x2->val[x2->count] = x1->val[j];
x2->link[x2->count] = x1->link[j];
j++;
}

j = pos;
while (j < myNode->count) {
myNode->val[j] = myNode->val[j + 1];
myNode->link[j] = myNode->link[j + 1];
j++;
}
myNode->count--;
free(x1);
}

/* adjusts the given node */
void adjustNode(struct btreeNode *myNode, int pos) {
if (!pos) {
if (myNode->link[1]->count > MIN) {
doLeftShift(myNode, 1);
} else {
mergeNodes(myNode, 1);
}
} else {
if (myNode->count != pos) {
if(myNode->link[pos - 1]->count > MIN) {
doRightShift(myNode, pos);
} else {
if (myNode->link[pos + 1]->count > MIN) {
doLeftShift(myNode, pos + 1);
} else {
mergeNodes(myNode, pos);
}
}
} else {
if (myNode->link[pos - 1]->count > MIN)
doRightShift(myNode, pos);
else
mergeNodes(myNode, pos);
}
}
}

/* delete val from the node */
int delValFromNode(int val, struct btreeNode *myNode) {
int pos, flag = 0;
if (myNode) {
if (val < myNode->val[1]) {
pos = 0;
flag = 0;
} else {
for (pos = myNode->count;
(val < myNode->val[pos] && pos > 1); pos--);
if (val == myNode->val[pos]) {
flag = 1;
} else {
flag = 0;
}
}
if (flag) {
if (myNode->link[pos - 1]) {
copySuccessor(myNode, pos);
flag = delValFromNode(myNode->val[pos], myNode-
>link[pos]);
if (flag == 0) {
printf("Given data is not present in B-
Tree\n");
}
} else {
removeVal(myNode, pos);
}
} else {
flag = delValFromNode(val, myNode->link[pos]);
}
if (myNode->link[pos]) {
if (myNode->link[pos]->count < MIN)
adjustNode(myNode, pos);
}
}
return flag;
}


/* search val in B-Tree */
void searching(int val, int *pos, struct btreeNode *myNode) {
if (!myNode) {
return;
}

if (val < myNode->val[1]) {
*pos = 0;
} else {
for (*pos = myNode->count;
(val < myNode->val[*pos] && *pos > 1); (*pos)--);
if (val == myNode->val[*pos]) {
printf("Given data %d is present in B-Tree", val);
return;
}
}
searching(val, pos, myNode->link[*pos]);
return;
}

/* B-Tree Traversal */
void traversal(struct btreeNode *myNode) {
int i;
if (myNode) {
for (i = 0; i < myNode->count; i++) {
traversal(myNode->link[i]);
printf("%d ", myNode->val[i + 1]);
}
traversal(myNode->link[i]);
}
}

int main()
{
clrscr();
int val, ch;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Searching\t4. Traversal\n");
printf("5. Exit\nEnter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter your input:");
scanf("%d", &val);
insertion(val);
break;
case 2:
printf("Enter the element to search:");
scanf("%d", &val);
searching(val, &ch, root);
break;

case 3:
traversal(root);
break;


case 4:
exit(0);
default:
printf("U have entered wrong option!!\n");
break;
}
printf("\n");
}
}

PROGRAM-7

Generate a graph for a given input.

Code:
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

void create(); // For creating a graph
void dfs(); // For Deapth First Search(DFS) Traversal.
void bfs(); // For Breadth First Search(BFS) Traversal.

struct node // Structure for elements in the graph
{
int data,status;
struct node *next;
struct link *adj;
};

struct link // Structure for adjacency list
{
struct node *next;
struct link *adj;
};

struct node *start,*p,*q;
struct link *l,*k;

int main()
{
int choice;
clrscr();
create();
while(1)
{
cout<<"

-----------------------";
cout<<"
1: DFS
2: BSF
3: Exit
Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
dfs();
break;
case 2:
bfs();
break;
case 3:
exit(0);
break;
default:
cout<<"
Incorrect choice!
Re-enter your choice.";
getch();
}
}
return 0;
}

void create() // Creating a graph
{
int dat,flag=0;
start=NULL;
cout<<"
Enter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
{
start=p;
q=p;
flag++;
}
else
{
q->next=p;
q=p;
}
}
p=start;
while(p!=NULL)
{
cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
flag=0;
while(1)
{
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
cout<<"

-------------------------";
return;
}


// Deapth First Search(DFS) Traversal.
void bfs()
{
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
qu[0]=p->data;
p->status=1;
while(1)
{
if(qu[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==qu[j])
break;
p=p->next;
}
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
cout<<"

Breadth First Search Results
";
cout<<"
---------------------------
";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
getch();
return;
}


// Breadth First Search(BFS) Traversal.
void dfs()
{
int stack[25],top=1;
cout<<"

Deapth First Search Results
";
cout<<"
---------------------------
";
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
cout<<stack[top]<<" ";
top--;
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
k=k->adj;
}
}
getch();
return;
}









PROGRAM-8

Write a program for evaluating a given arithmetic exp.
Code:
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1; /* Global declarations */

push(int elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}

int pop()
{ /* Function for POP operation */
return(s[top--]);
}

main()
{ /* Main Program */
char pofx[50],ch;
int i=0,op1,op2;
printf("\n\nRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
printf("\n Given Postfix Expn: %s\n",pofx);
printf("\n Result after Evaluation: %d\n",s[top]);
}

Anda mungkin juga menyukai