Vous êtes sur la page 1sur 152

DATA STRUCTURES

AND
ALGORITHMS

PROGRAM NO: 1

DATE: 14/05/2014
Implement Array ADT for one and two dimensions

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
class array
{
int ind1,ind2,value,*a,size1,i,size2,j,size;
public:
array(int j, int range[],int initvalue)
{
if(j==1)
{
size1=range[0];
a= new int[size1];
for(i=0;i<size1;i++)
a[i]=initvalue;
}
2

else if(j==2)
{
size1=range[0];
size2=range[1];
a=new int[size1*size2];
for(i=0;i<size1*size2;i++);
a[i]=initvalue;
}
}
int retrieve(int ind1)
{
return(a[ind1-1]);
}
void store(int ind1 ,int x)
{
if(ind1>size1)
cout<<"Out of size\n";
else
a[ind1-1]=x;
}
void display()
{
for(i=0;i<size1;i++)
cout<<a[i]<<"\n";
}
void disp()
{
for(i=0;i<size1*size2;i++)
cout<<a[i]<<"\n";
}
int retrieve(int ind1,int ind2)
{
if((ind1>size1)||(ind2>size2))

cout<<"Out of size\n";
else
size=ind2+ind1*size2;
return(a[size]);
}
void store(int ind1 ,int ind2,int x)
{
if((ind1>size1)||(ind2>size2))
cout<<"Out of size\n";
else
size=ind2+ind1*size2;
a[size]=x;
}
};
int main()
{
int ind1,x,t,j,ind2;
int myarr[2]={3,3};
cout<<"Select dimension (1D/2D): ";
cin>>j;
array ob1(1,myarr,0);
array ob2(2,myarr,0);
int ch=1,a;
while(ch==1)
{
cin>>a;
switch (a)
{
case 1:
if(j==1)
{
cout<<"Enter index and value of x: ";

cin>>ind1;
cin>>x;
ob1.store(ind1,x);
}
else
{
cout<<"Enter two index: ";
cin>>ind1;
cin>>ind2;
cout<<"Enter value: ";
cin>>x;
ob2.store(ind1,ind2,x);
}
break;
case 2:if(j==1)
{
cout<<"Enter the index: ";
cin>>ind1;
t=ob1.retrieve(ind1);
cout<<"Retrieved value is "<<t<<"\n";
}
else
{
cout<<"Enter the two index: ";
cin>>ind1;
cin>>ind2;
t=ob2.retrieve(ind1,ind2);
cout<<''Retrieved value is ''<<t<<"\n";
}
break;
case 3:
if(j==1)
ob1.display();

else
ob2.disp();
break;
case 4: break;
default: cout<<"Invalid choice\n";
}
cout<<"Press 1 to continue else press 0: ";
cin>>ch;
}
return 0;
}

OUTPUT

PROGRAM NO: 2

DATE: 14/05/2014

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
#define MaxTerm 10
using namespace std;
class SparseMatrix;
class MatrixTerm
{
public:
friend class SparseMatrix;
int row,col,value;
};
class SparseMatrix
{
int rows,cols,t;
8

public:
MatrixTerm smArray[MaxTerm];
SparseMatrix(int r,int c)
{
int i,j,k,n;
rows=r;
cols=c;
t=0;
k=1;
cout<<"Enter the elements: ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>n;
if(n!=0)
{
smArray[k].row=i;
smArray[k].col=j;
smArray[k].value=n;
k++;
t++;
}
}
}
smArray[0].row=rows;
smArray[0].col=cols;
smArray[0].value=t;
cout<<"Number of non zero values: "<<t<<"\n";
}
void display()
{
int i,j,k=1;

for(i=0;i<smArray[0].row;i++)
{
for(j=0;j<smArray[0].col;j++)
{
if((smArray[k].row==i)&&(smArray[k].col==j))
{
cout<<smArray[k].value<<"\t";
k++;
}
else
{
cout<<"0\t";
}
}
cout<<"\n";
}
}
{
int i,j,s;
MatrixTerm sum[MaxTerm];
i=j=s=1;
while((i<=t)&&(j<=sm2.t))
{
if(smArray[i].row==sm2.smArray[j].row)
{
if(smArray[i].col==sm2.smArray[j].col)
{
sum[s].row=smArray[i].row;
sum[s].col=smArray[i].col;
sum[s].value=smArray[i].value+sm2.smArray[j].value;
i++;
j++;

10

if(sum[s].value!=0)
s++;
}
else
{
if(smArray[i].col<sm2.smArray[j].col)
{
sum[s].row=smArray[i].row;
sum[s].col=smArray[i].col;
sum[s].value=smArray[i].value;
i++;
s++;
}
else
{
sum[s].row=sm2.smArray[j].row;
sum[s].col=sm2.smArray[j].col;
sum[s].value=sm2.smArray[j].value;
j++;
s++;
}
}
}
else
if(smArray[i].row<sm2.smArray[j].row)
{
sum[s].row=smArray[i].row;
sum[s].col=smArray[i].col;
sum[s].value=smArray[i].value;
i++;
s++;
}
else

11

{
sum[s].row=sm2.smArray[j].row;
sum[s].col=sm2.smArray[j].col;
sum[s].value=sm2.smArray[j].value;
j++;
s++;
}
}
if(i<=t)
{
for(int p=i;p<=t;p++)
{
sum[s].row=smArray[p].row;
sum[s].col=smArray[p].col;
sum[s].value=smArray[p].value;
s++;
}
}
else if(j<=sm2.t)
{
for(int p=j;p<=sm2.t;p++)
{
sum[s].row=sm2.smArray[p].row;
sum[s].col=sm2.smArray[p].col;
sum[s].value=sm2.smArray[p].value;
s++;
}
}
sum[0].row=rows;
sum[0].col=cols;
sum[0].value=s-1;
for(i=0;i<s;i++)

12

{
cout<<sum[i].row<<"\t"<<sum[i].col<<"\t"<<sum[i].value<<"\n";
}
}
};
int main()
{
int row1,col1,row2,col2,ch=1,x;
cout<<"Enter the size of first matrix: ";
cin>>row1>>col1;
cout<<"Enter the size of second matrix: ";
cin>>row2>>col2;
SparseMatrix sm1(row1,col1);
SparseMatrix sm2(row2,col2);
while(ch==1)
{
cin>>x;
switch(x)
{
case 1:
cout<<"\nFirst Matrix\n";
sm1.display();
cout<<"\nSecond Matrix\n";
sm2.display();
break;
case 2:
break;
default:
cout<<"Invalid entry\n";
}
cout<<"Press 1 to continue else press 0: ";

13

cin>>ch;
}
return 0;
}

OUTPUT

14

PROGRAM NO: 3
DATE: 21/05/2014
Write a program to add two polynomials using Array.

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
class polynomial
{
int n;
struct term
{
int coeff,exp;
}t[20];
public:
polynomial();
void polyappend(int c,int e);
void polydisplay();
};
polynomial::polynomial()
{
for(int i=0;i<20;i++)
{
15

t[i].coeff=0;
t[i].exp=0;
}
n=0;
}
void polynomial::polyappend(int c,int e)
{
t[n].coeff=c;
t[n].exp=e;
n++;
}
{
int i=0,j=0,k=0;
if(((p1.t[0].coeff)==0)&&((p2.t[0].coeff)==0))
{
return;
}
while((i<p1.n)&&(j<p2.n))
{
if((p1.t[i].exp)>=(p2.t[j].exp))
{
if((p1.t[i].exp)==(p2.t[j].exp))
{
t[k].coeff=(p1.t[i].coeff)+(p2.t[j].coeff);
t[k].exp=p1.t[i].exp;
i++;
j++;
}
else
{
t[k].coeff=p1.t[i].coeff;
t[k].exp=p1.t[i].exp;

16

i++;
}
}
else
{
t[k].coeff=p2.t[j].coeff;
t[k].exp=p2.t[j].exp;
j++;
}
k++;
}
while(i<p1.n)
{
t[k].coeff=p1.t[i].coeff;
t[k].exp=p2.t[i].exp;
i++;
k++;
}
while(j<p2.n)
{
t[k].coeff=p2.t[j].coeff;
t[k].exp=p2.t[j].exp;
j++;
k++;
}
n=k;
}
void polynomial::polydisplay()
{
for(int i=0;i<n;i++)
{
if(t[i].exp==0)
{

17

cout<<t[i].coeff;
}
else
{
cout<<t[i].coeff<<"x^"<<t[i].exp;
}
if((i+1)!=n)
{
cout<<"+";
}
}
}
int main()
{
polynomial p1,p2,p3;
int c,e,ch;
do
{
cout<<"\nEnter the coefficient & exponent of the polynomial p1: ";
cin>>c>>e;
p1.polyappend(c,e);
cout<<"\nPress 1 to continue else press 0\n";
cin>>ch;
}
while(ch!=0);
do
{
cout<<"\nEnter the coefficient & exponent of the polynomial p2: ";
cin>>c>>e;
p2.polyappend(c,e);
cout<<"\nPress 1 to continue else press 0\n";

18

cin>>ch;
}
while(ch!=0);
cout<<"\nPolynomial p1: ";
p1.polydisplay();
cout<<"\nPolynomial p2: ";
p2.polydisplay();
cout<<"\nSum of polynomial,p1+p2: ";
p3.polydisplay();
cout<<endl;
return(0);
}

OUTPUT

19

PROGRAM NO: 4

DATE: 21/05/2014
Convert a Infix expression to its postfix equivalent using stack

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<string.h>
using namespace std;
class postfix
{
char infix[20],stack[20];
int top;
public:
postfix()
{
top=-1;
}
{
int len,i;
char ch;
cout<<"\nEnter the infix expression: ";
cin>>infix;
cout<<"\nPostfix expression is\n";

20

len=strlen(infix);
for(i=0;infix[i]!='\0';i++)
{
ch=infix[i];
test(ch);
}
while(top!=-1)
{
cout<<stack[top--];
}
cout<<"\n";
}
void test(char ch)
{
char s;
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))
{
cout<<ch;
}
else if(ch=='(')
{
stack[++top]=ch;
}
else if(ch==')')
{
while(stack[top]!='(')
{
cout<<stack[top--];
}
top--;
}
else
{

21

if(top==-1)
{
stack[++top]=ch;
}
else
{
if(stack[top]=='(')
{
stack[++top]=ch;
}
else
{
s=stack[top];
while(priority(ch)<=priority(s) )
{
cout<<stack[top--];
s=stack[top];
}
stack[++top]=ch;
}
}
}
}
int priority(char c)
{
if(c=='+'||c=='-')
{
return 2;
}
else if(c=='*'||c=='/')
{
return 3;
}

22

else if(c=='(')
{
return 1;
}
}
};
int main()
{
postfix p;
return 0;
}

OUTPUT

23

PROGRAM NO: 5

DATE: 04/06/2014
Convert a Infix expression to prefix equivalent using stack

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<string.h>
using namespace std;
class Prefix
{
char infix[20],stack[20],prefix[20],p[20];
int top,k;
public:
Prefix()
{
top=-1;
k=0;
}
{
int j=0;
int len,i;

24

char ch;
cout<<"\nEnter the infix expression: ";
cin>>infix;
cout<<"\nPrefix expression is\n";
len=strlen(infix);
for(i=len-1;i>=0;i--)
{
p[j]=infix[i];
j++;
}
p[j]='\0';
strcpy(infix,p);
for(i=0;i<len;i++)
{
ch=infix[i];
test(ch);
}
while(top!=-1)
{
prefix[k++]=stack[top--];
}
prefix[k]='\0';
for(i=len;i>=0;i--)
{
cout<<prefix[i];
}
cout<<"\n";
}
void test(char ch)
{
char s;
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9'))
{

25

prefix[k++]=ch;
}
else if(ch==')')
{
stack[++top]=ch;
}
else if(ch=='(')
{
while(stack[top]!=')')
{
prefix[k++]=stack[top--];
}
top--;
}
else
{
if(top==-1)
{
stack[++top]=ch;
}
else
{
if(stack[top]=='(')
{
stack[++top]=ch;
}
else
{
s=stack[top];
while(priority(ch)<=priority(s) )
{
prefix[k++]=stack[top--];
s=stack[top];

26

}
stack[++top]=ch;
}
}
}
}
int priority(char c)
{
if(c=='+'||c=='-')
{
return 2;
}
else if(c=='*'||c=='/')
{
return 3;
}
else if(c==')')
{
return 1;
}
}
};
int main()
{
Prefix p;
return 0;
}

27

OUTPUT

28

PROGRAM NO: 6

DATE: 04/06/2014
Evaluate a Postfix expression by using stack

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#define size 20
using namespace std;
class postfix
{
char a[size];
int val[size];
int top;
public:
postfix(char b[size])
{
top=-1;
strcpy(a,b);
}
void push(int num)
{
29

if(top==size-1)
{
cout<<"Stack is full\n";
return;
}
a[++top]=num;
}
int pop()
{
int num;
if(top==-1)
{
cout<<"Stack empty\n";
return NULL;
}
num=a[top];
top--;
return num;
}
void eval(int len)
{
cout<<"\nEnter the values of each variable: ";
for(int j=0;j<len;j++)
cin>>val[j];
int j=0;
for(int i=0;i<strlen(a);i++)
{
if(isalpha(a[i]))
{
push(val[j]);
j++;
}

30

else
{
int x=pop();
int y=pop();
int z;
switch(a[i])
{
case '+':z=y+x;
push(z);
break;
case '-':z=y-x;
push(z);
break;
case '*':z=y*x;
push(z);
break;
case '/':z=y/x;
push(z);
break;
case '^':z=pow(y,x);
push(z);
break;
default:cout<<"\nInvalid operator"<<a[i];
exit(0);
}
}
}
cout<<"\nResult is "<<pop();
}
};
int main()
{

31

int len=0;
char a[size];
cout<<"\nEnter postfix expression: ";
cin>>a;
postfix s(a);
for(int i=0;i<strlen(a);i++)
{
if(isalpha(a[i]))
len++;
}
s.eval(len);
cout<<endl;
}

OUTPUT

32

PROGRAM NO: 7
DATE: 11/06/2014
Read a 2D array(matrix) from user and store it in column major order and retrieve
the elements back by using its indices

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
class array
{
int a[10],b[10][10];
int row,col;
public:
{
cout<<"Enter the size of matrix: ";
cin>>row>>col;
cout<<"Enter the matrix: ";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>b[i][j];
a[j*row+i]=b[i][j];
33

}
}
}
void retrieve(int x)
{
cout<<"\nElement at index "<<x<<" is ";
cout<<a[x-1];
}
};
int main()
{
array arr;
int x,ch=1;
while(ch==1)
{
cout<<"\nEnter index to retrieve: ";
cin>>x;
arr.retrieve(x);
cout<<"\nPress 1 to continue retrieve element else press 0: ";
cin>>ch;
}
return 0;
}

OUTPUT

34

PROGRAM NO:8

DATE: 11/06/2014
Implement a Singly Linked List to insert & delete nodes from begin, middle and end
of the list

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
using namespace std;
class List;
class Node
{
int data;
public:
friend class List;
};
class List
{
35

public:
List()
{
}
void insertlast();
void insertfront();
void insertmiddle();
void deletefront();
void deleteend();
void deletemiddle();
void display();
};
void List::insertlast()
{
Node *temp= new Node;
cout<<"Enter the data: ";
cin>>temp->data;
else
{
{
}
}
}
void List::insertfront()

36

{
Node *temp=new Node;
cout<<"Enter the data: ";
cin>>temp->data;
else
{
}
}
void List::insertmiddle()
{
int num;
Node *temp=new Node;
cout<<"Enter the data: ";
cin>>temp->data;
else
{
cout<<"After which number you want to insert?: ";
cin>>num;
{
if(node->data==num)
{

37

}
}
}
}
void List:: deletefront()
{
else
{
}
delete(node);
}
void List:: deletemiddle()
{
int num;
Node *temp;
{
cout<<"Enter the data you want to delete: ";
cin>>num;
cout<<"List contains only one node\n";
else
{
{
if(node->data==num)

38

{
delete(node);
break;
}
temp=node;
}
}
}
}
void List:: deleteend()
{
else
{
Node *temp;
{
temp=node;
}
}
delete(node);
}
void List::display()
{
cout<<"List is empty\n";

39

else
{
cout<<temp->data;
{
cout<<"\t"<<temp->data;
}
}
}
int main()
{
int a=1,ch;
List l;
while(a==1)
{
cout<<"\nOptions\n1.Insert at Beginning\n2.Insert at Middle\n3.Insert at
End\n4.Delete from Beginning\n5.Delete from Middle\n6.Delete from
cin>>ch;
switch(ch)
{
case 1:
l.insertfront();
break;
case 2:
l.insertmiddle();
break;
case 3:
l.insertlast();
break;
case 4:
l.deletefront();

40

break;
case 5:
l.deletemiddle();
break;
case 6:
l.deleteend();
break;
case 7:
l.display();
break;
case 8: exit(0);
default:cout<<"Invalid Choice\n";
}
cout<<"\nPress 1 to continue else press 0: ";
cin>>a;
}
return 0;
}

41

OUTPUT

42

43

44

45

PROGRAM NO: 9
DATE: 11/06/2014
Write a program to create a sorted one way linked list with n nodes. Extend the
program to insert a new node at appropriate location so that order does not get disturbed.

CLASS DIAGRAM

PROGRAM
#include <iostream>
using namespace std;
int a=0;
struct node
{
node *next;
int data;
class List
{
public:
void create(int x)
{
r=new node;
r->data=x;
r->next=NULL;
46

if(a==0)
{
p->next=NULL;
a++;
}
else
{
q=p;
if(p->data>r->data)
{
r->next=p;
}
else if(p->data<r->data)
{
while(p!=NULL&&q->data<r->data)
{
q=p;
p=p->next;
if(p==NULL)
{
q->next=r;
r->next=NULL;
}
else if(r->data<p->data)
{
q->next=r;
r->next=p;
break;
}

47

}
}
}
}
void traverse()
{
while(t!=NULL)
{
cout<<t->data<<"\t";
t=t->next;
}
}
};
int main()
{
List l;
int i=0,n,x;
cout<<"\nEnter the number of nodes: ";
cin>>n;
while(i<n)
{
cout<<"\nEnter value of node: ";
cin>>x;
l.create(x);
l.traverse();
i++;
}
}

48

OUTPUT

49

PROGRAM NO:10
DATE: 18/06/2014
Implement a Circular Singly linked list for inserting & deleting nodes.

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
{
struct node
{
int data;
}*p;
public:
{
p=NULL;
}
{
if(p==NULL)
{
return;

50

}
node *temp=p,*q;
{
q=temp;
delete q;
}
delete temp;
}
{
node *temp;
temp=new node;
temp->data=num;
if(p==NULL)
{
p=temp;
}
else
{
}
}
int del()
{
node *temp=new node;
int item;
if(p==NULL)
{
return NULL;

51

}
item=temp->data;
p=NULL;
else
delete temp;
return item;
}
void display()
{
if(p==NULL)
{
cout<<"List is empty\n";
return;
}
while(temp!=p)
{
cout<<temp->data<<"\t";
}
cout<<p->data;
cout<<"\n\n";
}
};
int main()
{
int ch,num;
do
{

52

cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter the element: ";
cin>>num;
break;
}
case 2:
{
num=l.del();
if(num==0)
{
break;
}
else
{
cout<<"Deleted element: "<<num;
break;
}
}
case 3:
{
l.display();
break;
}
case 4:break;
}

53

}
while(ch!=4);
return 0;
}

OUTPUT

54

55

PROGRAM NO: 11
DATE: 18/05/2014

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include <cstdlib>
using namespace std;
class node
{
public:
int data;
node* next;
};
class Stack
{
public:
Stack(int max)
{
top=NULL;
maxnum=max;
count=0;
}
void push(int element)
56

{
if(count==maxnum)
cout<<"Stack is full\n";
else
{
node *newTop=new node;
if(top==NULL)
{
newTop->data=element;
newTop->next=NULL;
top=newTop;
count++;
}
else
{
newTop->data=element;
newTop->next=top;
top=newTop;
count++;
}
}
}
void pop()
{
if(top==NULL)
cout<<"Stack is empty";
else
{
node*old=top;
top=top->next;
count--;
delete(old);
}

57

}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
private:
node *top;
int count;
int maxnum;
int stackData;
};
int main(int argc, char** argv)
{
int a=1,ch,b;
Stack *s = new Stack(5);
while(a==1)
{
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element: ";
cin>>b;
s->push(b);
break;
case 2:
s->pop();

58

break;
case 3:
s->print();
break;
case 4:exit(0);
break;
}
cout<<"\nPress 1 to continue else press 0: ";
cin>>a;
}
return 0;
}

OUTPUT

59

60

PROGRAM NO: 12
DATE: 02/07/2014

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include <cstdlib>
using namespace std;
struct node
{
int data;
};
class queue
{
struct node *front ,*rear,*ptr;
int size;
public:
queue(int max)
{
size=max;
rear=NULL;
front=NULL;
}
61

void enqueue(int);
void dqueue();
void display();
};
void queue::enqueue(int data)
{
ptr=new node;
if(rear==NULL)
{ front=ptr;
}
else
{
}
rear=ptr;
rear->data=data;
cout<<"\n";
}
void queue::dqueue()
{
int item;
if(front==NULL)
cout<<"Queue is empty\n";
else
{
item=front->data;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else

62

{
ptr=front;
delete ptr;
}
cout<<"Deleted element: "<<item;
}
}
void queue::display()
{
ptr=front;
if(front==NULL)
cout<<"Queue is empty\n";
else
{
cout<<"Elements in the queue are: ";
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
}
}
}
int main()
{
queue q(10);
int ch,data,a=1;
while(a==1)
{
cout<<"Options\n";
cin>>ch;
switch(ch)

63

{
case 1:
cout<<"Enter the element: ";
cin>>data;
q.enqueue(data);break;
case 2:
q.dqueue();break;
case 3:
q.display();break;
case 4:
exit(0);
break;
default:
cout<<"Invalid option\n";
}
cout<<"\nPress 1 to continue else press 0: ";
cin>>a;
}
return 0;
}

64

OUTPUT

65

PROGRAM NO: 13
DATE: 02/07/2014

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
int data;
friend class DblList;
};
class DblList
{
public:
DblList()
66

{
}
void insert(Node *,Node *);
void Insert();
void del();
void DeleteNode(Node *);
void display();
};
void DblList::Insert()
{
Node *temp=new Node;
cout<<"Enter the data: ";
cin>>temp->data;
else
{
int num;
cout<<"After which element you are going to add: ";
cin>>num;
Node *node;
do
{
if(node->data==num)
{
insert(temp, node);
break;
}
}

67

}
}
void DblList::insert(Node *NewNode, Node *CurrentNode)
{
}
void DblList :: del()
{
else
{
int num;
cout<<"Which element you want to delete?: ";
cin>>num;
Node *node;
do
{
if(node->data==num)
{
DeleteNode(node);
break;
}
}
}
}

68

void DblList::DeleteNode(Node *x)

{
delete x;
}
void DblList::display()
{
cout<<"List is empty\n";
else
{
cout<<"List is \n";
{
cout<<"\t"<<temp->data;
}
}
}
int main()
{
DblList d;
int a=1,ch;
while(a==1)
{
cin>>ch;
switch(ch)
{
case 1:
d.Insert();

69

break;
case 2:
d.del();
break;
case 3:
d.display();
break;
case 4:
exit(0);
break;
default:
cout<<"Invalid choice\n";
break;
}
cout<<"\nPress 1 to Continue else Press 0: ";
cin>>a;
}
}

70

OUTPUT

71

72

PROGRAM NO: 14
DATE: 02/07/2014
Implement different traversal on Binary Tree

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
class BTree
{
public:
char a[100];
void create(int);
void preorder(int);
void inorder(int);
void postorder(int);
};
void BTree::create(int i)
{
char ch;
cout<<"\nEnter the values: ";
cin>>a[i];
cout<<"\nDo you want to create left child for the "<<a[i]<<" :(y/n)?: ";
cin>>ch;
if(ch=='y')
create(i*2);
73

else
a[i*2]='`';
cout<<"\nDo you want to create right child for the "<<a[i]<<" :(y/n)?: ";
cin>>ch;
if(ch=='y')
create(i*2+1);
else
a[i*2+1]='`';
}
void BTree::preorder(int i)
{
cout<<"\t"<<a[i];
if(a[i*2]!='`')
preorder(i*2);
if(a[i*2+1]!='`')
preorder(i*2+1);
}
void BTree::inorder(int i)
{
if(a[i*2]!='`')
inorder(i*2);
cout<<"\t"<<a[i];
if(a[i*2+1]!='`')
inorder(i*2+1);
}
void BTree::postorder(int i)
{
if(a[i*2]!='`')
postorder(i*2);
if(a[i*2+1]!='`')
postorder(i*2+1);
cout<<"\t"<<a[i];
}

74

int main()
{
BTree t;
t.create(1);
cout<<"\nPreorder traversal: ";
t.preorder(1);
cout<<"\nInorder traversal: ";
t.inorder(1);
cout<<"\nPostorder traversal: ";
t.postorder(1);
cout<<"\n";
return 0;
}

OUTPUT

75

PROGRAM NO: 15
DATE: 09/07/2014
Implement a menu driven program to perform the following operations on a binary
search tree:
1. Construct a BST (Construction begins from an empty tree)
2. Insert element(s) into a non empty BST
3. Search for an element in a BST
4. Retrieve elements of the BST in the sorted order

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
node *right;
node *left;
int data;
};
class BST
{
struct node *root,*p;

76

public:
BST()
{
root=NULL;
p=NULL;
}
void create();
void find(int item);
void insert(int item);
void display(node *ptr);
void display();
};
void BST::find(int item)
{
if(root==NULL)
{
cout<<"\nTree is empty\n";
return;
}
node *ptr=root;
int flag=0;
while(ptr!=NULL&&flag==0)
{
if(item<ptr->data)
ptr=ptr->left;
else if(item==ptr->data)
flag=1;
else
ptr=ptr->right;
}
if(flag==1)
77

cout<<"\nKey found at "<<ptr;

else
cout<<"\nKey does not exist\n";
}
void BST::create()
{
int value,c=0,n;
cout<<"Enter the limit: ";
cin >>n;
while (c < n)
{
if (root == NULL)
{
root = new node;
cout<<"Enter value of root node: ";
cin>>root->data;
root->right=NULL;
root->left=NULL;
}
else
{
p = root;
cout<<"Enter value of node: ";
cin>>value;
while(true)
{
if (value < p->data)
{
if (p->left == NULL)
{
p->left = new node;
78

p = p->left;
p->data = value;
p->left = NULL;
p->right = NULL;
cout<<"Element inserted in left\n";
break;
}
else if (p->left != NULL)
{
p = p->left;
}
}
else if (value > p->data)
{
if (p->right == NULL)
{
p->right = new node;
p = p->right;
p->data = value;
p->left = NULL;
p->right = NULL;
cout<<"Element inserted in right\n";
break;
}
else if (p->right != NULL)
{
p = p->right;
}
}
}
}
79

c++;
}
}
void BST::insert(int item)
{
node *ptr1=root,*ptr2=NULL,*start;
int flag=0;
while(ptr1!=NULL&&flag==0)
{
if(item<ptr1->data)
{
ptr2=ptr1;
ptr1=ptr1->left;
}
else if(item==ptr1->data)
{
flag=1;
}
else
{
ptr2=ptr1;
ptr1=ptr1->right;
}
}
if(ptr1==NULL)
{
node *temp;
temp=new node;
temp->data=item;
temp->left=NULL;
80

temp->right=NULL;
if(root==NULL)
{
root=start=temp;
}
if(ptr2!=NULL)
{
if(ptr2->data<item)
ptr2->right=temp;
else
ptr2->left=temp;
}
}
}
void BST::display()
{
display(root);
}
void BST::display(node *ptr)
{
if(root==NULL)
{
cout<<"\nTree is empty\n";
return;
}
if(ptr!=NULL)
{
display(ptr->left);
cout<<"\t"<<ptr->data;
display(ptr->right);
}
81

}
int main()
{
BST t;
int ch,n;
do
{
cout<<"\nOptions\n1.Create\n2.Insert\n3.Search\n4.Display\n5.Exit\nEnter
cin>>ch;
switch(ch)
{
case 1: t.create();
break;
case 2:cout<<"\nEnter value for the node: ";
cin>>n;
t.insert(n);
break;
case 3:cout<<"\nEnter node to be searched: ";
cin>>n;
t.find(n);
break;
case 4:t.display();
break;
case 5:exit(0);
}
}
while(ch!=5);
}

82

OUTPUT

83

84

PROGRAM NO: 16
DATE: 10/07/2014
Write a program to implement Insertion sort, Selection Sort, Bubble Sort for any
arbitrary set of elements (menu driven).

CLASS DIAGRAM

PROGRAM
#include<iostream>
#include<stdlib.h>
using namespace std;
class sort
{
int a[20],n,m;
public:
{
cout<<"Enter the limit: ";
cin>>n;
cout<<"Enter the elements: ";
for(int i=0;i<n;i++)
cin>>a[i];
}
void bubble()
85

{
for(int i=0;i<=n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
void insertion()
{
int pos=0,key=0;
for(int i=1;i<n;i++)
{
key=a[i];
pos=i;
while((pos>0)&&(a[pos-1]>key))
{
a[pos]=a[pos-1];
pos=pos-1;
a[pos]=key;
}
}
}
int min(int i)
{
m=i;
for(int j=i+1;j<n;j++)

86

if(a[j]<a[m])
m=j;
return m;
}
void selection()
{
int t;
for(int i=0;i<n;i++)
{
m=min(i);
t=a[i];
a[i]=a[m];
a[m]=t;
}
}
void display()
{
cout<<"\nElements after sorting\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
};
int main()
{
sort S;
int ch,c=1;
while(c!=0)
{
cin>>ch;
switch(ch)
{
case 1:cout<<"\nBubble Sort\n";

87

S.bubble();
S.display();
break;
case 2:cout<<"\nInsertion Sort\n";
S.insertion();
S.display();
break;
case 3:cout<<"\nSelection Sort\n";
S.selection();
S.display();
break;
case 4: exit(0);
}
cout<<"\nPress 1 to continue else press 0: \n";
cin>>c;
}
return 0;
}

88

OUTPUT

89

PROGRAM NO: 17
DATE: 10/07/2014
Write a program to implement Quick Sort algorithm

CLASS DIAGRAM

PROGRAM
#include<iostream>
using namespace std;
class quick
{
int a[10];
int n;
public :
void sort(int x,int y);
int partition(int *b,int x,int y);
void display();
quick();
};
{
n=l;
cout<<"Enter the elements: ";
for(int i=0;i<n;i++)
{
cin>>a[i];
90

}
}
quick::quick()
{
for(int i=0;i<10;i++)
{
a[i]=0;
n=0;
}
}
int quick::partition(int *b,int x,int y)
{
int t;
int l=x+1;
int ri=y;
int p=b[x];
while(ri>=l)
{
while(b[l]<p)
{
l++;
}
while(b[ri]>p)
{
ri--;
}
if(ri>l)
{
int temp=b[l];
b[l]=b[ri];
b[ri]=temp;
}
}

91

t=b[x];
b[x]=b[ri];
b[ri]=t;
return ri;
}
void quick::sort(int x,int y)
{
if(x<y)
{
int i=partition(a,x,y);
sort(x,i-1);
sort(i+1,y);
}
}
void quick::display()
{
cout<<"\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\n";
}
int main()
{
quick q;
int n;
cout<<"Enter the limit of the array: ";
cin>>n;
cout<<"\nBefore sorting";
q.display();
q.sort(0,(n-1));
cout<<"\nAfter sorting";
q.display();

92

return 0;
}

OUTPUT

93

LINUX

94

SHELL PROGRAMMING

95

PROGRAM NO: 1
DATE: 14/05/2014
Write a shell script to find the greatest among n numbers.

PROGRAM
#!/bin/sh
echo "Enter size of an array"
echo "Enter the numbers"
for((i=0;i<n;i++))
do
done
greatest=\${a[0]}
for((i=0;i<n;i++))
do
if [ \${a[\$i]} -gt \$greatest ]
then
greatest=\${a[\$i]}
fi
done
echo "Greatest number in an array is \$greatest"

OUTPUT

96

PROGRAM NO: 2
DATE: 14/05/2014
Write a shell script to concatinate two strings and find the length of the resultant
string.

PROGRAM
#!/bin/sh
echo Enter first string:
echo Enter second string:
s3=\$s1\$s2
len=`echo \$s3 | wc -c`
len=`expr \$len - 1`
echo Concatinated string is \$s3 of length \$len

OUTPUT

97

PROGRAM NO: 3
DATE: 14/05/2014
Write a shell script to find the net salary of an employee, who DA=5% of basic pay,
HRA =10% of basic pay and other allowance= 5% of basic pay.

PROGRAM
#!/bin/bash
echo "Enter the Basic Pay"
hra=`echo \$bp \* 0.1 | bc`
da=`echo \$bp \* 0.05 | bc`
oa=`echo \$bp \* 0.05 | bc`
ta=`echo \$bp + \$hra + \$da + \$oa | bc`
echo "Total Salary : \$ta"

OUTPUT

98

PROGRAM NO: 4
DATE: 21/05/2014
Check whether a file exist , if its readable display the contents on the console or if its
writable append contents to the file then display the contents on the console.

PROGRAM
#!/bin/bash
echo enter filename
if [ -f \$file ]
then
echo \$file is present in current directory
if [ -r \$file ]
then
cat \$file
else
fi
if [ -w \$file ]
then
echo \$file is writable
cat >> \$file
echo The contents of file after appending
cat \$file
else
echo \$file is not writable
fi
else
echo \$file is not present in current directory
fi

99

OUTPUT

100

PROGRAM NO: 5
DATE: 21/05/2014
Print all the prime no's between 1 to n. Where n is the user input from keyboard.

PROGRAM
#!/bin/bash
echo Enter the limit
echo Prime numbers are
for (( i=2;i<=n;i++ ))
do
flag=0
for ((j=2;j<=i/2;j++ ))
do
temp=`expr \$i % \$j`
if [ \$temp -eq 0 ]
then
flag=1
fi
done
if [ \$flag -eq 0 ]
then
echo \$i
fi
done

OUTPUT

101

PROGRAM NO: 6
DATE: 21/05/2014
Find the factorial of a given number as command line argument.

PROGRAM
#!/bin/bash
if [ \$# -ne 1 ]
then
echo no sufficient command line argument
else
i=1
f=1
while [ \$i -le \$1 ]
do
f=`expr \$f \* \$i`
i=`expr \$i + 1`
done
echo "The factorial of \$1 is : \$f"
fi

OUTPUT

102

PROGRAM NO: 7
DATE: 04/06/2014
Print Fibonacci Series.

PROGRAM
#!/bin/bash
echo "Enter the limit"
num1=0
num2=1
echo "Fibonacci series:"
echo \$num1
echo \$num2
count=2
while [ \$count -lt \$num ]
do
num3=`expr \$num1 + \$num2`
echo \$num3
num1=\$num2
num2=\$num3
count=`expr \$count + 1`
done

OUTPUT

103

PROGRAM NO: 8
DATE: 11/06/2014
Print Armstrong number between 1 to 500

PROGRAM
#!/bin/bash
i=1
while((i<=500))
do
c=\$i
d=\$i
b=0
a=0
while((c>0))
do
a=\$((c%10))
b=\$((b + a*a*a))
c=\$((c/10))
done
if((b==d))
then
echo "\$i"
fi
i=\$((i+1))
done

OUTPUT

104

PROGRAM NO: 9
DATE: 11/06/2014
Write a shell script to create a file that contains the output of ls command and count
how many no. of c and c++ files are available in a directory.

PROGRAM
#!/bin/sh
ls>newfile
echo "Number of c++ files "
find *.cpp < newfile | wc -l
echo "Number of c files "
find *.c < newfile | wc -l

OUTPUT

105

PROGRAM NO: 10
DATE: 11/06/2014
Output of who should be displayed on the screen with value of total no. of users who
have logged in displayed at the bottom of the list.

PROGRAM
#!/bin/sh
who
who | wc -l

OUTPUT

106

PROGRAM NO: 11
DATE: 18/06/2014
Write a menu driven shell script to
a) Create an empty file
b) add content to an empty file
c) display file content on the screen
d) append content to an existing writable file.

PROGRAM
#!/bin/bash
select

in

Create_an_empty_file

Append_content_to_an_existing_writable_file
do
case \$a in
Create_an_empty_file) touch empty;;
Display_file_content) cat empty;;
Append_content_to_an_existing_writable_file) cat >>empty;;
esac
done

OUTPUT

107

Display_file_content

PROGRAM NO: 12
DATE: 18/06/2014
Write a Shell program to sort numbers stored in a file in ascending order.

PROGRAM
#!/bin/sh
echo -n "Enter Filename : "
if [ ! -f \$file ]
then
echo "\$file not a file!"
exit 1
fi
echo Contents of file
cat \$file
echo Sorted order
sort -n \$file

OUTPUT

108

PROGRAM NO: 13
DATE: 02/07/2014
Write a shell script to sort in descending order. The numbers should be given as shell
arguments.

PROGRAM
#!/bin/sh
if [ \$# -eq 0 ]
then
echo "Enter the arguments"
exit
fi
>a
for i in \$*
do
echo "\$i" >> a
done
echo "Sorted data"
sort -nr a

OUTPUT

109

PROGRAM NO: 14
DATE: 02/07/2014
Write a shell script to sort an array of numbers.

PROGRAM
#!/bin/bash
touch Sorting
echo Enter the limit
echo Enter the elements
for (( i=0;i<n;i++ ))
do
echo \${num[\$i]} >> Sorting
done
echo Elements in sorted order
sort -n Sorting
rm Sorting

OUTPUT

110

PROGRAM NO: 15
DATE: 02/07/2014
Read the Name and marks obtained by a student and calculate the grade/division
obtained.

PROGRAM
#!/bin/bash
echo Enter marks in 5 subjects
read a b c d e
tot=`expr \$a + \$b + \$c + \$d + \$e `
echo Name: \$name
echo Total Mark: \$tot
if [ \$tot -ge 450 ]
then
elif [ \$tot -ge 350 -a \$tot -lt 450 ]
then
elif [ \$tot -ge 250 -a \$tot -lt 350 ]
then
else
echo FAILED
fi

111

OUTPUT

112

PROGRAM NO: 16
DATE: 09/07/2014
Write a program to determine whether a given year is leap year or not. ( Use logical
operator -a and -o )

PROGRAM
#!/bin/sh
echo Enter the year
a=`expr \$year % 4`
b=`expr \$year % 100`
c=`expr \$year % 400`
if [ \$a -eq 0 -a \$b -ne 0 -o \$c -eq 0 ]
then
echo \$year is leap year
else
echo \$year is not leap year
fi

OUTPUT

113

PROGRAM NO: 17
DATE: 09/07/2014
Shell Script to create a menu driven calculator

PROGRAM
#!/bin/bash
echo CALCULATOR
select var in Add Sub Mul Div Exit
do
case \$var in
c=`expr \$a + \$b`
echo Result=\$c;;
Sub)echo Enter two numbers
c=`expr \$a - \$b`
echo Result=\$c;;
Mul)echo Enter two numbers
c=`expr \$a \* \$b`
echo Result=\$c;;
Div)echo Enter two numbers
c=`expr \$a / \$b`
echo Result=\$c;;
Exit) exit;;
*)echo Invalid expression;;
esac
done

114

OUTPUT

115

PROGRAM NO: 18
DATE: 10/07/2014
Write a shell script to sort the given numbers in descending order using Bubble sort.

PROGRAM
#!/bin/bash
echo Enter the limit
echo Enter the numbers
for (( i = 0; i <\$n; i++ ))
do
done
for (( i = 0; i <\$n ; i++ ))
do
for (( j = \$i; j <\$n; j++ ))
do
if [ \${a[\$i]} -lt \${a[\$j]} ]
then
t=\${a[\$i]}
a[\$i]=\${a[\$j]}
a[\$j]=\$t
fi
done
done
echo -e "Sorted Numbers in Descending Order"
for (( i=0; i <\$n; i++ ))
do
echo \${a[\$i]}
done

116

OUTPUT

117

PROGRAM NO: 19
DATE: 10/07/2014
Write a shell script to add records to a file called prisoner.dat. The fields contained
are to be separated by - .All the fields validation are as follows :
prisoner code : should be between 1000 and 5000
name : should not be blank
duration of sentence: should be greater than 0

PROGRAM
#!/bin/bash
echo Enter prisoner code
echo Enter the name
echo Enter duration of sentence
if [ \$pc -ge 1000 -a \$pc -le 5000 ]
then
if [ \$name ]
then
if [ \$d -gt 0 ]
then
echo \$pc>>prisoner.dat
echo "-">>prisoner.dat
echo \$name>>prisoner.dat
echo "-">>prisoner.dat
echo \$d>>prisoner.dat
echo "-">>prisoner.dat
else
echo "Duration of sentence should be >0"

118

fi
else
echo "Name cannot be null"
fi
else
echo Prisoner code must be between 1000 and 2000
fi

OUTPUT

119

PROGRAM NO: 20
DATE: 11/07/2014
Write a shell script to keep on accepting lines of text and write the text into a data
file until the user inputs "end". The script should count the number of lines input and
display them. [Don't use wc]

PROGRAM
#!/bin/bash
echo Enter a string then Type end to exit
count=0
while [ \$a != "end" ]
do
echo \$a>tempfile
cat tempfile>>file
count=`expr \$count + 1`
done
echo Linecount: \$count
echo Contents of file is
cat file

OUTPUT

120

PROGRAM NO: 21
DATE: 11/07/2014
Write a Shell script to generate the following series :
1,3,2,4,3,5,4,6 .........100

PROGRAM
#!/bin/bash
x=1
f=0
echo -n 1
while [ \$x != 100 ]
do
if [ \$f == 0 ]
then
x=`expr \$x + 2`
echo -n " \$x "
f=1
else
x=`expr \$x - 1`
f=0
echo -n " \$x "
fi
done
echo " "

121

OUTPUT

122

PROGRAM NO: 22
DATE: 11/07/2014
Write a Unix shell script that accepts an Option number, and the names of two files
as command line arguments. Depending on the option the following option should be
taken.
Option no : Action to be taken
1 Copy first file to second file
2 Concatenate the files
3 Rename 1st file as 2nd file
4 Display the number of lines in both the files

PROGRAM
#!/bin/bash
ch=0
if [ \$# -ne 2 ]
then
echo Insufficient arguments
else
echo Select an option
select var in COPY CONCATINATE RENAME DISPLAY_NUMBER_OF_LINES EXIT
do
case \$var in
COPY)cp \$1 \$2
echo CONTENTS IN FIRST FILE
cat \$1
echo " "
echo CONTENTS IN SECOND FILE
cat \$2 ;;
CONCATINATE)touch file3
cat \$1 \$2 >file3
echo CONCATINATED FILE IS
123

cat file3
rm file3 ;;
RENAME)mv \$1 \$2
echo FILE RENAMED ;;
DISPLAY_NUMBER_OF_LINES)len1=`cat \$1 |wc -l`
len2=`cat \$2 |wc -l`
echo Number of lines in first file is \$len1
echo Number of lines in second file is \$len2 ;;
EXIT)exit
esac
done
fi

OUTPUT

124

PROGRAM NO: 23
DATE: 11/07/2014
Write a menu driven shell program which prints the following :
1) Current date in MM/DD/YY format.
2) The message "No. of users logged in : " with total number of currently
logged in users
3) The message "Personal Status :" with your own logname, port number,

PROGRAM
#!/bin/bash
echo Select an option
select var in Current_date Users_logged_in Personal_status EXIT
do
case \$var in
Current_date)echo Current date :
DATE=`date +%m/%d/%y`
echo \$DATE ;;
Users_logged_in)echo Number of users logged in :
who|wc -l ;;
Personal_status)echo Personal status :
who am i ;;
EXIT)exit ;;
esac
done

125

OUTPUT

126

USER MANAGEMENT

127

DATE : 04/06/2014
1. INSTALLING LINUX OS
Installing From CD/DVD

An ISO file is a CD image file that will need to be burned before you can use it.

2. Burn the ISO file

3. Boot from the disc
Once you have finished burning the disc, restart your computer and choose to boot from the
disc.

4. Try Ubuntu before installing

Once you boot from the disc, you will be given the option to try Ubuntu without installing
it. The operating system will run from the disc, and you will have a chance to explore the layout of
the operating system.

128

5. Install Ubuntu
Your computer will need at least 4.5 GB of free space.

6. Set up the wireless connection

If your computer is not connected to the internet via Ethernet, you can configure your
wireless connection in the next step.

7. Choose what to do with your existing operating system

If you have Windows installed on your system, you will be given a couple options on how
youd like to install Ubuntu. You can either install it along side your previous Windows
installation, or you can replace your Windows installation with Ubuntu.

If you are installing Ubuntu alongside Windows, you can use the slider to adjust how much
space you would like to designate for Ubuntu.

129

If you are connected to the Internet, this should be done automatically. Verify that the
timezone displayed is correct, and then click the Continue button.

You can choose from a list of options, or click the Detect Keyboard Layout button to have
Ubuntu automatically pick the correct

Enter your name, the name of the computer (which will be displayed on the network),
choose a user name, and come up with a password. You can choose to have Ubuntu automatically
log you in, or require your user name and password when it starts.

12. Wait for the installation process to complete.

Once you choose your login info, the installation will begin. During setup, various tips for
using Ubuntu will be displayed on the screen. Once it is finished, you will be prompt to restart the
computer and Ubuntu will be reloaded.

Using the Windows Installer

3. Click Install to start

130

DATE : 04/06/2014
2. CREATE A BOOTABLE CD FOR UBUNTU OS
Step 1: Download ISO image of Ubuntu 14.04 from Internet (32 or 64 bit version)
Step 2: Insert the CD and select Brasero disk burner from dash home.
Step 3: Click on burn as image option.
Step 4: Select the image to write and also select the disk
Step 5: Click create image

131

DATE : 04/06/2014
3. CREATE A INSTALLED LINUX OS USING A PENDRIVE

Step 1: Download ISO image of Ubuntu 14.04 from Internet (32 or 64 bit version)
Step 2: Right click the pendrive and select format option
Step 3: Install Gparted Partion Editor from Ubuntu software center
Step 4: Open Gparted Partion Editor software and select USB from the drop down menu at the
top.
Step 5: From partition menu select umount and umount current file system of USB.
Step 6: Select new option
Step 7: From the new opened window enter new size(to burn ISO image). Select the file system
Step 8: Now make second partition and select file system as fat32 and then click add
Step 9: From edit menu select apply all operation
Step 11: Open the startup disk creator and select the source image location on the disk to which
the ISO is to be written
Step 12: Click make startup disk
Step 13: Restart the system and select boot from USB to boot OS from USB

132

DATE: 04/06/2014
Step 1: Enter the command
\$ sudo apt-get install lilo
Step 2: Open the configuration file /etc/lilo.conf and edit the entry under perimage
image = /boot/vmlinux 3.2.035genericpae
label = ubuntu
root = /dev/sda5
Step 3: Run the command
\$ /sbin/lilo -t
Step 4: Then run the command
\$ /sbin/lilo

133

DATE: 11/06/2014
5. PROVIDE A BACKGROUND IMAGE FOR GRUB MENU FOR YOUR
SYSTEM
Step 1: Login as super user
Step 2: Copy a picture to /boot/grub
Step 3: Run the command
\$ sudo updategrub

134

DATE: 11/06/2014
Step 1: Login as super user
Step 2: Copy a picture to /usr/share/backgrounds
\$ cp /Desktop/wartyfinalubuntu.png wartyfinalubuntu.png

135

DATE: 11/06/2014
7. YOUR FAVORITE QUOTE HAS TO BE DISPLAYED IN THE TERMINAL
WHENEVER YOU TAKE A NEW TERMINAL
Step 1: Take .bashrc file
\$ vim .bashrc
Step 2: Give an echo command in it that specifies the quote.
echo Hari Om!!!

136

DATE: 18/06/2014
8. GIVE AN ALTERNATIVE LOCATION FOR MOUNTING PEN DRIVE,
CD DRIVE AND DVD DRIVE
All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted
at /. These files can be spread out over several devices. The mount command serves to attach the
file system found on some device to the big file tree. Conversely, the umount command will
detach it again.
The standard form of the mount command, is mount -t type device dir
This tells the kernel to attach the file system found on device (which is of type type) at the
directory dir. The previous contents (if any) and owner and mode of dir become invisible, and as
long as this file system remains mounted, the pathname dir refers to the root of the file system on
device
For mounting the pendrive to an alternate location add the following line
to /etc/fstab file.
For mounting the pendrive to an alternate location add the following line to /etc/fstab file.
/dev/sdb1 /home/pranitha/pendrive vfat rw 0 0
After that run the mount command
sudo mount /home/pranitha/pendrive
For mounting the cd to an alternate location add the following line to /etc/fstab file.
/dev/sr0 /home/pranitha/cd iso9660 ro,nosuid,nodev 0 0
After that run the mount command
sudo mount /home/pranitha/cd
For mounting the dvd to an alternate location add the following line to /etc/fstab file.
/dev/sr0 /home/pranitha/dvd iso9660 ro 0 0
After that run the mount command
sudo mount /home/pranitha/dvd

137

DATE: 18/06/2014
9. GIVE AN EXAMPLE OF MOUNT AND UMOUNT COMMAND
For mounting the pendrive to an alternate location run the mount command as
sudo mount -t vfat -o rw /dev/sdb1 /home/pranitha/pendrive
For mounting the cd to an alternate location run the mount command as
sudo mount -t iso9660 -o ro /dev/sr0 /home/pranitha/cd
For mounting the dvd to an alternate location run the mount command as
sudo mount -t iso9660 -o ro /dev/sr0 /home/pranitha/dvd
For unmounting the pendrive
sudo umount /dev/sdb1
For mounting the cd to an alternate location run the mount command as
sudo umount /dev/sr0
For mounting the dvd to an alternate location run the mount command as
sudo umount /dev/sr0

138

DATE: 18/06/2014

First open the /etc/profile script.

sudo vim /etc/profile
Then add the following line to the script
echo `xclock`
After adding the line save the file.

139

DATE: 18/06/2014
11. CHECK THE DYNAMIC LOADED FILE SYSTEM DRIVERS IN YOUR
SYSTEM
The Linux kernel modules are defined as pieces of code, which can be dynamically linked
to the kernel , even after the system bootup . The command lsmod lists all modules that are
lsmod command will list dynamically loaded file system drivers

140

DATE: 18/06/2014
12. USER MANAGEMENT
1. Login as root and create groups as dba with id 501 & stud with id 555
2. Create the following list of users
User name UID GID Working Shell Secondary Comments Group
Mac1 501 501 Bourne shell 555 Mac1 user
useradd u 501 g 501 -c mac1 user d /home/mac1-m s /bin/sh G 555 mac1
Mac2 502 501 C shell NULL Mac2 user
useradd u 502 -g 501 -c mac2 user -d/home/mac2 m s /bin/csh mac2
Mac3 503 501 BASH Shell 555 Mac3 user
useradd u 503 -g 501 c mac3 user d/home/mac3 m s/bin/bash G 555 mac3
User1 504 555 Bourne shell NULL User1 user
useradd u 504 g 555 c user1 user d/home/user1 m s/bin/sh user1
User2 505 555 Bash Shell NULL User2 user
useradd u 505 g 555 c user2 user d/home/user2 m s/bin/bash user2

3. Examine the content of the /etc/passwd file.

# cat /etc/passwd
The contents of this file have the following format
mac1 /bin/sh
mac2 /bin/csh
mac3 /bin/bash
user1 /bin/sh
user2 /bin/bash
141

4. Examine the content of the /etc/shadow file. Name the text that is found in the second field
for the users created.
mac1:!!::
mac2:!!::
mac3:!!::
user1:!!::
user2:!!::
This is because we have not set password for the users mac1,mac2 and mac3
5. Set password for the users mac1, mac2, mac3 & shut.
passwd Mac1
passwd Mac2
passwd Mac3

142

6. Select user2 from the list of users. Change the passwd aging information for user2 so that
it matches the following information.
Max inactive 2 days
Expiry 4 days
Now change the system date increase by 5 days
# usermod f 2 e 04/19/01
Modifying the users expired by next
7. Logout of login session. Attempt to log as user2. What happens?
The user is not able to login because his account is expired. Only administrator can make
changes.
8. Change the shell for the user2 to Bourne shell.
# usermod -s /bin/sh user2
9. Delete user2 including his home directory and his comments.
# userdel -r user2
10. Lock the user1 with the help of a single command.
# passwd -l user1

143

DATE: 02/07/2014
\$ vim abc
\$ ln abc def
Then type the command
\$ ls -l abc
-rw-rwr-- 2 pranitha pranitha 10 Aug 12 09:24 abc
\$ ls -l def
-rw-rwr-- 2 pranitha pranitha 10 Aug 12 09:24 def
Thus for file def the number of link is 2

\$ ln -s abc hij
\$ ls -l hij
lrwxrwxrwx 1 pranitha 2 Aug 1 09:30 hij -> abc

144

DATE: 02/07/2014
14. ALLOWING AND DENYING ACCESS
Use pam to deny a user called user1 login access on all terminals except for
tty1
/etc/security/access.conf:
-:USER1:ALL EXCEPT tty1
account required /lib/security/pam_access.so

Allow user user1 to login during weekdays between 7 am and 5 p.m

/etc/security/time.conf:
account required /lib/security/pam_time.so

Disable login of all users except root user and display the message system
under maintenance whenever a normal user tries to login
Step 1: Create a file in /etc as nologin. If /etc/nologin exist no other users is allowed to
Step 2: Once /etc/nologin is created, add content into the file as echo SYSTEM UNDER
MAINTENANCE.
Then the message is printed when non root users tries to login.

Allow users user1 & user2 to login on all days between 8 am and 5pm
except for sunday
/etc/security/time.conf:
account required /lib/security/pam_time.so
145

Deny the users 'user1' and 'user2' service all of the time for login on tty* (any
ttyxxx device)
/etc/security/time.conf
account required /lib/security/pam_time.so

Limit the number of processes to 25 for a particular user

/etc/security/limits.conf:
user hard nproc 25
session required /lib/security/pam_limits.so

146

DATE: 09/07/2014
15. CHECK THE CURRENT RUNLEVEL OF YOUR SYSTEM AND
CHANGE THE DEFAULT RUNLEVEL OF YOUR SYSTEM TO 3

A run level is a software configuration of the system that represents the mode in which your
computer is operating. A runlevel determines which services are available on the system at any
given time.
The possible run levels are
0 Halt
1 single user mode.
2 multiuser mode without X and networking services
3 multiuser mode with networking services without X
5 multiuser mode with X and network services
6 reboot
/etc/inittab is the file associated with runlevel. It is having the format:
id:runlevel:action:process
the default entry is:
id:3:initdefault:
To check your machine's current runlevel, you can run the /sbin/runlevel command.
\$ runlevel
To change the default runlevel edit the /etc/inittab files default runlevel entry
OR
# /sbin/init 3
#/sbin/telinit 3

147

DATE: 09/07/2014
16. CRON AND AT
The cron utility allows you to schedule a repetitive task to take place at any regular interval
desired, and the at command lets you specify a one-time action to take place at some desired time.
At utility executes a command or multiple commands just once at some future time. The atd
daemon runs your command at the specified time and mails the output to you.

a) Using crontab print Hello every 5 minutes in terminal.

vim mycron
*/5 * * * * /bin/echo hello > /dev/pts/1
crontab mycron

b) Execute a cron job

1. every 5 Minutes
*/5 * * * * echo Hello

148

2. every 5 Hours
0 */5 * * * /bin/echo hello > /dev/pts/1

3. every 5 Seconds
Cron job cannot be used to schedule a job in seconds interval. i.e You cannot
schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep
5 command in it.
\$ cat every5seconds.sh
#!/bin/bash
while true
do
/home/pranitha/backup.sh
sleep 5
done
4. every 5th weekday
0 0 * * 5 /home/pranitha/backup.sh
(or)
0 0 * * Fri /home/pranitha/backup.sh

149

5. every 5 months
0 0 1 5,10 * /home/pranitha/backup.sh
(or)
0 0 1 May,Oct * /home/pranitha/backup.sh

c) Using at for setting a Reminder Message

\$ at 10:55
at> echo today you have a meeting at 11:00

d ) Schedule a Background Cron Job For Every 10 Minutes.

*/10 * * * * /bin/echo "hello" > /dev/pts/1

150

e) Use at command to shutdown the server at 7:30am on 9/18.

\$ at 7:30am sept 18
at> halt

f) Use cron to shutdown your system at 11:30pm every day

vim mycron
30 23 * * * /sbin/shutdown
crontab mycron

151

DATE: 09/07/2014
17. BACKUP AND RESTORE
Schedule a Full backup of a folder to another folder on every Saturday 8:30pm
vim cron
30 20 * * 6 cp /home/pranitha/file1/* /home/pranitha/file2
crontab cron
crontab -l

* After cron job execution contents of file1 and file2

152

Obtenez nos applications gratuites

Droits d'auteur © 2021 Scribd Inc.