INDEX
S. No. Title Page No. Teacher’s Signature
1. Perform Linear Search and Binary Search on an
array.
Descriptions of the programs:
a. Read and array of type integer.
b. Input element from user for searching.
c. Search the element by passing the array
to a function and then returning the
position of the element from the function
else return -1 if the element is not found.
d. Display the positions where the element
has been found.
01 - 04
2. Implement sparse matrix using array.
Description of program:
a. Read a 2D array from the user.
b. Store it in the sparse matrix form, use
array of structures.
c. Print the final array.
05 - 06
3. Create a linked list with nodes having
information about a student and perform.
Description of the program:
a. Insert a new node at specified position.
b. Delete of a node with the roll number of
student specified.
c. Reversal of that linked list.
07 - 13
4. Create doubly linked list with nodes having
information about an employee and perform
Insertion at front of doubly linked list and
perform deletion at end of that doubly linked list.
14 - 17
5. Create circular linked list having information
about a college and perform Insertion at front
perform Deletion at end.
18 - 22
6. Create a stack and perform Pop, Push, Traverse
operations on the stack using Linear Linked list.
23 - 26
7. Create a Linear Queue using Linked List and
implement different operations such as Insert,
Delete, and Display the queue elements.
27 - 30
01.
PerformLinear Search and Binary Search on an array.
Description of programs:
a. Read an array of type integer.
b. Inputelement from user for searching.
c. Search the element by passing the array to a function and then returning the
position of the element fromthe function else return -1 if the element is not
found.
d. Display the position wherethe element has been found.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void LinearSearch( );
void BinarySearch( );
int i,j,n,a[20],count=0,yn=-1,choice,mid,first,last;
void main( )
{
printf("ntNAME- Devesh Kumarnt");
printf("ntNorthern India Engineering Collegent");
printf("nt");
while(choice!=3)
{
printf("nntPress 1 For Linear Search");
printf("nntPress 2 For Binary Search");
printf("nntPress 3 For Exit");
printf("nntPleaseEnter your choice :");
Experiment No - 1
scanf("%d",&choice);
switch(choice)
{
case1: LinearSearch();
break;
case2: BinarySearch();
break;
case 3: Exit(1);
default: printf("nntYou Have Entered Wrong Choice");
printf("tEnter 1 for Continue :");
scanf("%d",&choice);
}
}
getch();
}
void LinearSearch()
{
printf("nntLinear Search :");
printf("nntEnter The Size Of The Array : ");
scanf("%d",&n);
printf("nntEnter The Sorted Array");
printf("nntEnter The %d Elements : n",n);
printf("nt");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
printf("nt");
}
printf("nntEnter The Elements To Be Searched :");
scanf("%d",&j);
printf("nntOriginalArraynnt");
for(i=0;i<n;i++)
{
printf("%dt",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]==j)
{
printf("nntElement %d Is Found At Location %dn",j,i+1);
break;
}
else
{
count++;
}
}
if(count==n)
{
n=-1;
printf("nntElement %d is not in this array",n);
}
}
void BinarySearch()
{
printf("nntBinary Search : ");
printf("nntEnter The Size Of The Array :");
scanf("%d",&n);
printf("nntEnter The Sorted Array");
printf("nntEnter The %d Elements :nt",n);
printf("nt");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
printf("nt");
}
printf("nntEnter The Elements To Be Searched :");
scanf("%d",&j);
printf("nntOriginalarray nnt");
for(i=0;i<n;i++) 04.
{
printf("%dt",a[i]);
}
first=0;
last= n-1;
while(first<=last)
{
mid=(first+last)/2;
if(j<a[mid])
{
last=mid-1;
}
else if(j>a[mid])
{
first=mid+1;
}
else if(j==a[mid])
{
printf("nntElement %d Is Found AtLocation %dn",j,mid+1);
yn=0;
break;
}
}
if(yn==1)
{
j=-1;
printf("nntElement %d Is NotIn This Array",j);
}
}
05.
Implement sparsematrixusing array.
Description of program:
a. Read a 2D array fromthe user.
b. Store it in the sparsematrixform, use array of structures.
c. Printthe final array.
Source Code:
#include<stdio.h>
#include<conio.h>
main( )
{
int A[10][10],B[10][3],m,n,s=0,i,j;
printf("ntNAME - Devesh Kumarnt");
printf("ntNorthern India Engineering Collegent");
printf("nt");
printf("ntEnter the order MxN of the matrixnt");
printf("ntEnter the value of M :");
scanf("%d",&m);
printf("ntEnter the value of N :");
scanf("%d",&n);
printf("ntEnter the elements in the matrixnnt");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("nt%d Row and %d Column - t",i,j);
scanf("%d",&A[i][j]);
}
}
Experiment No - 2
printf("ntThe given matrix is:nnt"); 06.
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",A[i][j]);
printf("t");
}
printf("nnt");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf("ntThesparsematrix is given by:nn");
printf("tValuetRowtColumnnnt");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d",B[i][j]);
printf("t");
}
printf("nnt");
}
getch();
}
07.
3. Create a linked list with nodes having information about a student and perform
Description of program:
a. Inserta new node at specified position.
b. Delete of a node with the roll number of student specified.
c. Reversal of that linked list.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
structstudent
{
char name[100];
int roll;
struct student*next;
};
structstudent *first=NULL,*last=NULL,*k;
void create(int n)
{
int i;
first=(structstudent*)malloc(sizeof(structstudent));
printf("ntEnter The Name Of The Student:");
scanf("%s",first->name);
printf("ntEnter The Roll Number Of The Student:");
scanf("%d",&first->roll);
first->next=NULL;
last=first;
Experiment No - 3
for(i=1;i<n;i++)
{
k=(structstudent*)malloc(sizeof(structstudent));
printf("ntEnter The Name Of The Student:");
scanf("%s",k->name);
printf("ntEnter The Roll Number Of The Student:");
scanf("%d",&k->roll);
k->next=NULL;
last->next=k;
last=k;
}
}
void display( )
{
structstudent *t;
int count=0;
t=first;
printf("ntTheDetails Of The Students Arenn");
printf("tS.No.tRollNo.tNamen");
while(t!=NULL)
{
count++;
printf("nt%dt%dtt%s",count,t->roll,t->name);
t=t->next;
}
}
void insertafter()
{
int r;
int flag=0;
structstudent *t;
printf("ntEnter The Roll Number You Want To InsertAfter That:");
scanf("%d",&r);
printf("nt");
if(first!=NULL)
{
t=first;
while(t!=NULL)
{
if(t->roll==r)
{
k=(structstudent*)malloc(sizeof(structstudent));
printf("ntRoll NotttNamen");
printf("nt%dttt%s",t->roll,t->name);
printf("nntEnter The Name Of The Student : ");
scanf("%s",k->name) ;
printf("ntEnter The Roll No Of The Student : ");
scanf("%d",&k->roll);
k->next=t->next;
t->next=k;
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf("ntThe Element Not Found!!!");
}
else
printf("tElement Not Found ");
}
void del()
{
struct student*back,*t,*k;
int r;
int flag=0;
if(first!=NULL)
{
printf("ntEnter The Roll Number You Want To Delete:");
scanf("%d",&r);
if(first->roll==r)
{
first=first->next;
flag=1;
}
else
{
back=first;
k=first->next;
while(k!=NULL && flag==0)
{
if(k->roll==r)
{
back->next=k->next;
flag=1;
printf("ntThe Record Deleted : ");
getch();
}
}
}
if(flag==0)
printf("ntThe Element Not Found!!!");
}
else
{
printf("ntNo Record In The Database");
getch();
}
}
void search()
{
int r;
int flag=0;
structstudent *t;
if(first!=NULL)
{
printf("ntEnter The Roll Number You Want To Search:");
scanf("%d",&r);
t=first;
while(t!=NULL)
{
if(t->roll==r)
{
printf("nntThe Roll Number Found In The List!!!n”);
printf(ntHis Name Is %sn",t->name);
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf("ntTheRoll Number Not In Database!!");
}
else
{
printf("ntNo Element In The List ");
getch();
}
}
void reverse_list()
{
structstudent *temp,*temp1,*var;
temp=first;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;
}
first=var;
}
int main()
{
int n,o;
printf("ntNAME- Devesh Kumarnt");
printf("ntNorthern India Engineering Collegent");
printf("nt");
while(o!=0)
{
printf("nntPleasePress Enter To Continue : nnt");
getch();
printf("ntEnter 1 For Creating Database");
printf("ntEnter 2 For Displaying Database");
printf("ntEnter 3 For Inserting An Record After Another");
printf("ntEnter 4 For Deleting A Record");
printf("ntEnter 5 For Searching A Record");
printf("ntEnter 6 Reversing The Nodes");
printf("ntEnter 0 For Exit!");
printf("ntPleaseEnter Your Choice:");
scanf("%d",&o);
switch(o)
{
case1: printf("ntEnter The Maximum Size Of The Data Base:");
scanf("%d",&n);
create(n);
break;
case2: display( );
break;
case3: insertafter( );
break;
case 4: del(); 13.
break;
case 5: search();
break;
case 6: reverse_list();
display();
break;
case 0: exit(0);
break;
default: printf("ntYou Have Entered A Wrong Choice!!!");
}
}
getch();
}
14.
Create doubly linked list with nodes having information about an employee and
performInsertion atfront of doubly linked list and performdeletion at end of that
doubly linked list.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
structemployee
{
int num;
long int salary;
char name[20];
structemployee *prev;
structemployee *next;
}
*start,*rear,*k;
typedef structemployee employee;
employee *create();
void addemployee(employee *);
void display(employee*);
void addbeg(employee*);
void del( );
void main( )
{
int m,o;
Experiment No - 4
employee *p;
start=NULL;
rear=NULL;
printf("ntNAME- Devesh Kumarnt");
printf("ntNorthern India Engineering Collegent");
printf("nt");
printf("Deletion At End");
printf("ntnt");
printf("ntEnter the Number of Employees :");
scanf("%d",&m);
while(m--)
{
p=create();
addbeg(p);
}
while(o!=0)
{
printf("nntPleasePress Enter to Continue nt");
getch();
printf("ntEnter 1 To InsertNew Recordnt");
printf("ntEnter 2.To Delete Record FromEndnt");
printf("ntEnter 3.To Display All Records nt");
printf("ntEnter 0.To Exitnt");
printf("ntEnter Your Choice :");
scanf("%d",&o);
switch(o)
{
case 1: p=create();
addbeg(p);
break;
case 2: del();
break;
case 3: display(start);
break;
}
}
}
employee *create()
{
employee *k;
k=((employee*)malloc(sizeof(employee)));
printf("ntEnter The Employee Number : ");
scanf("%d",&k->num);
printf("ntEnter The Name of The Employee : ");
scanf("%s",k->name);
printf("ntEnter The Salary Of The Employee : ");
scanf("%ld",&k->salary);
k->next=NULL;
k->prev=NULL;
return k;
}
void display(employee*k)
{
int count=0;
printf("ntS.No.tEmployeeNumberttNamettSalaryn");
while(k!=NULL)
{
count++;
printf("nt%dt%dttt%stt%ld",count,k->num,k->name,k->salary);
k=k->next;
}
}
void addbeg(employee*k)
{
if(start==NULL)
{ 17.
start=k;
rear=k;
}
else
{
k->next=start;
start->prev=k;
start=k;
}
}
void del()
{
employee *temp;
if(start==NULL)
return;
else if (rear->prev==NULL)
{
temp=rear;
start=rear=NULL;
}
else
{
temp=rear;
rear=rear->prev;
rear->next=NULL;
free(temp);
printf("ntNodeDeletednt");
}
}
18.
Create circular linked list having information about an college and perform
Insertion atfront performDeletion at end.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
structnode
{
int id;
char name[20];
char director[20];
char add[20];
char contact[11];
structnode *next;
}
*start,*rear,*k;
typedef structnode node;
node *create();
void display();
void addbeg(node*);
node *del(node *);
main()
{
int m,o;
node *p;
start=NULL;
Experiment No - 5
printf("ntNAME- Devesh Kumarn");
printf("ntNorthern India Engineering Collegen");
printf("nt");
printf("To PerformDeletion At End");
printf("nnt");
printf("ntPleasePress Enter To Continue :");
getch();
printf("nt");
printf("ntEnter Initial Number Of Colleges :");
scanf("%d",&m);
while(m--)
{
p=create();
addbeg(p);
}
while(o!=0)
{
printf("nntPleasePress Enter To Continue");
getch();
printf("nntPress 1 To Insertnew Record At Front");
printf("nntPress 2.To Delete Record From End");
printf("nntPress 3.To Display AllRecords");
printf("nntPress 0.To Exit");
printf("nntPress Your Choice: ");
scanf("%d",&o);
switch(o)
{
case 1: p=create();
addbeg(p);
break;
case 2: start=del(start);
break;
case 3: display( );
break;
}
}
}
node *create()
{
node *k;
k=(node *)malloc(sizeof(node));
printf("ntEnter The ID : ");
scanf("%d",&k->id);
printf("ntEnter Name Of College : ");
scanf("%s",k->name);
printf("ntEnter Name Of Director : ");
scanf("%s",k->director);
printf("ntEnter Address Of TheCollege : ");
scanf("%s",k->add);
printf("ntEnter Contact Number : ");
scanf("%s",k->contact);
k->next=NULL;
return k;
}
void display()
{
node *k;
int count=0;
k=start;
printf("nntS.No.tIDtNametDirectortAddressttContactn");
do
{
count++;
printf("nt%dt%dt%st%st%stt%sn",count,k->id,
k>name,k>director,k>add,k->contact);
k=k->next;
}
while(k!=start);
}
void addbeg(node*k)
{
node *n=start;
if(start==NULL)
{
start=k;
start->next=start;
}
else
{
while(n->next!=start)
n=n->next;
k->next=start;
start=k;
n->next=start;
}
}
node *del(node *start)
{
node *p,*q;
p=start;
if(p==NULL)
{
printf("ntListIs Emptyn");
}
else
{
while(p->next!=rear)
{
q=p;
p=p->next;
}
printf("ntRecord Deleted"); 22.
q->next=p->next;
}
rear=q;
return start;
}
23.
Create a stack and performPop, Push, Traverseoperations on the stack using
Linear Linked list.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
structstack
{
int info;
struct stack *next;
}
*top,*rear,*k;
typedef structstack stack;
stack *create(int);
void push(stack *);
void display(stack *);
void peep(int);
void pop();
void main()
{
int m=8,a,choice1;
stack *p;
top=NULL;
printf("ntNAME- Devesh Kumarnt");
printf("ntNorthern India Engineering Collegent");
printf("nt");
printf("ntPleasePress Enter To Continuent");
Experiment No - 6
getch();
printf("ntEnter Eight Numbersn");
while(m--)
{
printf("nt");
scanf("%d",&a);
p=create(a);
push(p);
}
printf("nt");
dispaly(top);
printf("nt");
do
{
printf("ntEnter 1 To PUSHnt");
printf("ntEnter 2 To POPnt");
printf("ntEnter 3 To SEARCHnt");
printf("ntEnter Your Choice :");
scanf("%d",&m);
switch(m)
{
case 1: printf("ntEnter A Value -");
scanf("%d",&m);
printf("n");
p=create(m);
push(p);
break;
case 2: printf("n");
pop();
break;
case 3: printf("ntEnter Value To Search -");
scanf("%d",&a);
printf("nn");
peep(a);
break;
default: printf("ntYou HaveEntered Wrong Choicen");
}
printf("t");
dispaly(top);
printf("nt");
printf("ntEnter 1 To Continue :");
scanf("%d",&choice1);
}
while(choice1==1);
getch();
}
stack *create(int m)
{
stack *k;
k=((stack *)malloc(sizeof(stack)));
k->info=m;
k->next=NULL;
return k;
}
void push(stack *n)
{
if(top==NULL)
{
top=n;
}
else
{
n->next=top;
top=n;
}
}
dispaly(stack *k) 26.
{
while(k!=NULL)
{
printf("%d->",k->info);
k=k->next;
}
}
void peep(int val)
{
stack *p=top;
stack *k;
int found =1;
while(p!=NULL)
{
if(p->info==val)
{
printf("tValueFoundnn");
found=0;
break;
}
p=p->next;
}
if(found)
printf("ntValueNot Found");
}
void pop( )
{
stack *k=top;
top=top->next;
free(k);
}
27.
Create a Linear Queue using Linked Listand implement different operations such
as Insert, Delete, and Display the queue elements.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
structqueue
{
int info;
structqueue *next;
}
*front,*rear,*k;
typedef structqueue queue;
queue *create(int);
void addq(queue*);
void delq();
void display(queue*);
void search(int);
void main()
{
int m=8,a,choice;
queue *p;
front=rear=NULL;
printf("ntNAME - Devesh Kumarn");
printf("ntNorthern India Engineering Collegen");
printf("ntPress a key : nt");
Experiment No - 7
getch();
printf("n");
printf("ntEnter Eight Numbersn");
printf("nn");
while(m--)
{
printf("t");
scanf("%d",&a);
p=create(a);
addq(p);
}
printf("t");
printf("nnt");
display(front);
printf("nntMENUn");
printf("nntEnter your choicen");
printf("ntTo Add Node, Enter 1 nnt");
printf("ntTo Delete Node, Enter 2nnt");
printf("ntTo Search Value, Enter 3nnt");
scanf("%d",&m);
switch(m)
{
case 1: printf("nntEnter a Value = n");
scanf("%d",&m);
p=create(m);
addq(p);
break;
case 2: delq();
break;
case 3: printf("ntEnter Value to search = ");
scanf("%d",&a);
search(a);
break;
default: printf("ntMismatch Casen");
printf("tEnter 1 for Continue :");
scanf("%d",&choice);
}
printf("nnt");
display(front);
getch();
}
queue *create(int m)
{
queue *k;
k=((queue*)malloc(sizeof(queue)));
k->info=m;
k->next=NULL;
return k;
}
void addq(queue*n)
{
if(front==NULL)
front=rear=n;
else
{
rear->next=n;
rear=n;
}
}
void display(queue*k)
{
while(k!=NULL)
{
printf("%d->",k->info); 30.
k=k->next;
}
}
void search(intval)
{
queue *p=front;
queue *k;
Intfound =1;
while(p!=NULL)
{
if(p->info==val)
{
printf("ntValueFoundn");
found=0;
break;
}
p=p->next;
}
if(found)
printf("tValueNot Foundn");
}
void delq( )
{
queue *p=front;
front=front->next;
free(p);
}

Data Structure Project File

  • 1.
    INDEX S. No. TitlePage No. Teacher’s Signature 1. Perform Linear Search and Binary Search on an array. Descriptions of the programs: a. Read and array of type integer. b. Input element from user for searching. c. Search the element by passing the array to a function and then returning the position of the element from the function else return -1 if the element is not found. d. Display the positions where the element has been found. 01 - 04 2. Implement sparse matrix using array. Description of program: a. Read a 2D array from the user. b. Store it in the sparse matrix form, use array of structures. c. Print the final array. 05 - 06 3. Create a linked list with nodes having information about a student and perform. Description of the program: a. Insert a new node at specified position. b. Delete of a node with the roll number of student specified. c. Reversal of that linked list. 07 - 13 4. Create doubly linked list with nodes having information about an employee and perform Insertion at front of doubly linked list and perform deletion at end of that doubly linked list. 14 - 17
  • 2.
    5. Create circularlinked list having information about a college and perform Insertion at front perform Deletion at end. 18 - 22 6. Create a stack and perform Pop, Push, Traverse operations on the stack using Linear Linked list. 23 - 26 7. Create a Linear Queue using Linked List and implement different operations such as Insert, Delete, and Display the queue elements. 27 - 30
  • 3.
    01. PerformLinear Search andBinary Search on an array. Description of programs: a. Read an array of type integer. b. Inputelement from user for searching. c. Search the element by passing the array to a function and then returning the position of the element fromthe function else return -1 if the element is not found. d. Display the position wherethe element has been found. Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> void LinearSearch( ); void BinarySearch( ); int i,j,n,a[20],count=0,yn=-1,choice,mid,first,last; void main( ) { printf("ntNAME- Devesh Kumarnt"); printf("ntNorthern India Engineering Collegent"); printf("nt"); while(choice!=3) { printf("nntPress 1 For Linear Search"); printf("nntPress 2 For Binary Search"); printf("nntPress 3 For Exit"); printf("nntPleaseEnter your choice :"); Experiment No - 1
  • 4.
    scanf("%d",&choice); switch(choice) { case1: LinearSearch(); break; case2: BinarySearch(); break; case3: Exit(1); default: printf("nntYou Have Entered Wrong Choice"); printf("tEnter 1 for Continue :"); scanf("%d",&choice); } } getch(); } void LinearSearch() { printf("nntLinear Search :"); printf("nntEnter The Size Of The Array : "); scanf("%d",&n); printf("nntEnter The Sorted Array"); printf("nntEnter The %d Elements : n",n); printf("nt"); for(i=0;i<n;i++) { scanf("%d",&a[i]); printf("nt"); } printf("nntEnter The Elements To Be Searched :"); scanf("%d",&j); printf("nntOriginalArraynnt"); for(i=0;i<n;i++) {
  • 5.
    printf("%dt",a[i]); } for(i=0;i<n;i++) { if(a[i]==j) { printf("nntElement %d IsFound At Location %dn",j,i+1); break; } else { count++; } } if(count==n) { n=-1; printf("nntElement %d is not in this array",n); } } void BinarySearch() { printf("nntBinary Search : "); printf("nntEnter The Size Of The Array :"); scanf("%d",&n); printf("nntEnter The Sorted Array"); printf("nntEnter The %d Elements :nt",n); printf("nt"); for(i=0;i<n;i++) { scanf("%d",&a[i]); printf("nt"); } printf("nntEnter The Elements To Be Searched :"); scanf("%d",&j); printf("nntOriginalarray nnt");
  • 6.
    for(i=0;i<n;i++) 04. { printf("%dt",a[i]); } first=0; last= n-1; while(first<=last) { mid=(first+last)/2; if(j<a[mid]) { last=mid-1; } elseif(j>a[mid]) { first=mid+1; } else if(j==a[mid]) { printf("nntElement %d Is Found AtLocation %dn",j,mid+1); yn=0; break; } } if(yn==1) { j=-1; printf("nntElement %d Is NotIn This Array",j); } }
  • 7.
    05. Implement sparsematrixusing array. Descriptionof program: a. Read a 2D array fromthe user. b. Store it in the sparsematrixform, use array of structures. c. Printthe final array. Source Code: #include<stdio.h> #include<conio.h> main( ) { int A[10][10],B[10][3],m,n,s=0,i,j; printf("ntNAME - Devesh Kumarnt"); printf("ntNorthern India Engineering Collegent"); printf("nt"); printf("ntEnter the order MxN of the matrixnt"); printf("ntEnter the value of M :"); scanf("%d",&m); printf("ntEnter the value of N :"); scanf("%d",&n); printf("ntEnter the elements in the matrixnnt"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("nt%d Row and %d Column - t",i,j); scanf("%d",&A[i][j]); } } Experiment No - 2
  • 8.
    printf("ntThe given matrixis:nnt"); 06. for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d",A[i][j]); printf("t"); } printf("nnt"); } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(A[i][j]!=0) { B[s][0]=A[i][j]; B[s][1]=i; B[s][2]=j; s++; } } } printf("ntThesparsematrix is given by:nn"); printf("tValuetRowtColumnnnt"); for(i=0;i<s;i++) { for(j=0;j<3;j++) { printf("%d",B[i][j]); printf("t"); } printf("nnt"); } getch(); }
  • 9.
    07. 3. Create alinked list with nodes having information about a student and perform Description of program: a. Inserta new node at specified position. b. Delete of a node with the roll number of student specified. c. Reversal of that linked list. Source Code: #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> structstudent { char name[100]; int roll; struct student*next; }; structstudent *first=NULL,*last=NULL,*k; void create(int n) { int i; first=(structstudent*)malloc(sizeof(structstudent)); printf("ntEnter The Name Of The Student:"); scanf("%s",first->name); printf("ntEnter The Roll Number Of The Student:"); scanf("%d",&first->roll); first->next=NULL; last=first; Experiment No - 3
  • 10.
    for(i=1;i<n;i++) { k=(structstudent*)malloc(sizeof(structstudent)); printf("ntEnter The NameOf The Student:"); scanf("%s",k->name); printf("ntEnter The Roll Number Of The Student:"); scanf("%d",&k->roll); k->next=NULL; last->next=k; last=k; } } void display( ) { structstudent *t; int count=0; t=first; printf("ntTheDetails Of The Students Arenn"); printf("tS.No.tRollNo.tNamen"); while(t!=NULL) { count++; printf("nt%dt%dtt%s",count,t->roll,t->name); t=t->next; } } void insertafter() { int r; int flag=0; structstudent *t; printf("ntEnter The Roll Number You Want To InsertAfter That:"); scanf("%d",&r);
  • 11.
    printf("nt"); if(first!=NULL) { t=first; while(t!=NULL) { if(t->roll==r) { k=(structstudent*)malloc(sizeof(structstudent)); printf("ntRoll NotttNamen"); printf("nt%dttt%s",t->roll,t->name); printf("nntEnter TheName Of The Student : "); scanf("%s",k->name) ; printf("ntEnter The Roll No Of The Student : "); scanf("%d",&k->roll); k->next=t->next; t->next=k; flag=1; break; } t=t->next; } if(flag==0) printf("ntThe Element Not Found!!!"); } else printf("tElement Not Found "); } void del() { struct student*back,*t,*k; int r; int flag=0; if(first!=NULL) {
  • 12.
    printf("ntEnter The RollNumber You Want To Delete:"); scanf("%d",&r); if(first->roll==r) { first=first->next; flag=1; } else { back=first; k=first->next; while(k!=NULL && flag==0) { if(k->roll==r) { back->next=k->next; flag=1; printf("ntThe Record Deleted : "); getch(); } } } if(flag==0) printf("ntThe Element Not Found!!!"); } else { printf("ntNo Record In The Database"); getch(); } } void search() { int r; int flag=0;
  • 13.
    structstudent *t; if(first!=NULL) { printf("ntEnter TheRoll Number You Want To Search:"); scanf("%d",&r); t=first; while(t!=NULL) { if(t->roll==r) { printf("nntThe Roll Number Found In The List!!!n”); printf(ntHis Name Is %sn",t->name); flag=1; break; } t=t->next; } if(flag==0) printf("ntTheRoll Number Not In Database!!"); } else { printf("ntNo Element In The List "); getch(); } } void reverse_list() { structstudent *temp,*temp1,*var; temp=first; var=NULL; while(temp!=NULL) { temp1=var; var=temp; temp=temp->next; var->next=temp1;
  • 14.
    } first=var; } int main() { int n,o; printf("ntNAME-Devesh Kumarnt"); printf("ntNorthern India Engineering Collegent"); printf("nt"); while(o!=0) { printf("nntPleasePress Enter To Continue : nnt"); getch(); printf("ntEnter 1 For Creating Database"); printf("ntEnter 2 For Displaying Database"); printf("ntEnter 3 For Inserting An Record After Another"); printf("ntEnter 4 For Deleting A Record"); printf("ntEnter 5 For Searching A Record"); printf("ntEnter 6 Reversing The Nodes"); printf("ntEnter 0 For Exit!"); printf("ntPleaseEnter Your Choice:"); scanf("%d",&o); switch(o) { case1: printf("ntEnter The Maximum Size Of The Data Base:"); scanf("%d",&n); create(n); break; case2: display( ); break; case3: insertafter( ); break;
  • 15.
    case 4: del();13. break; case 5: search(); break; case 6: reverse_list(); display(); break; case 0: exit(0); break; default: printf("ntYou Have Entered A Wrong Choice!!!"); } } getch(); }
  • 16.
    14. Create doubly linkedlist with nodes having information about an employee and performInsertion atfront of doubly linked list and performdeletion at end of that doubly linked list. Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> structemployee { int num; long int salary; char name[20]; structemployee *prev; structemployee *next; } *start,*rear,*k; typedef structemployee employee; employee *create(); void addemployee(employee *); void display(employee*); void addbeg(employee*); void del( ); void main( ) { int m,o; Experiment No - 4
  • 17.
    employee *p; start=NULL; rear=NULL; printf("ntNAME- DeveshKumarnt"); printf("ntNorthern India Engineering Collegent"); printf("nt"); printf("Deletion At End"); printf("ntnt"); printf("ntEnter the Number of Employees :"); scanf("%d",&m); while(m--) { p=create(); addbeg(p); } while(o!=0) { printf("nntPleasePress Enter to Continue nt"); getch(); printf("ntEnter 1 To InsertNew Recordnt"); printf("ntEnter 2.To Delete Record FromEndnt"); printf("ntEnter 3.To Display All Records nt"); printf("ntEnter 0.To Exitnt"); printf("ntEnter Your Choice :"); scanf("%d",&o); switch(o) { case 1: p=create(); addbeg(p); break; case 2: del(); break;
  • 18.
    case 3: display(start); break; } } } employee*create() { employee *k; k=((employee*)malloc(sizeof(employee))); printf("ntEnter The Employee Number : "); scanf("%d",&k->num); printf("ntEnter The Name of The Employee : "); scanf("%s",k->name); printf("ntEnter The Salary Of The Employee : "); scanf("%ld",&k->salary); k->next=NULL; k->prev=NULL; return k; } void display(employee*k) { int count=0; printf("ntS.No.tEmployeeNumberttNamettSalaryn"); while(k!=NULL) { count++; printf("nt%dt%dttt%stt%ld",count,k->num,k->name,k->salary); k=k->next; } } void addbeg(employee*k) { if(start==NULL)
  • 19.
    { 17. start=k; rear=k; } else { k->next=start; start->prev=k; start=k; } } void del() { employee*temp; if(start==NULL) return; else if (rear->prev==NULL) { temp=rear; start=rear=NULL; } else { temp=rear; rear=rear->prev; rear->next=NULL; free(temp); printf("ntNodeDeletednt"); } }
  • 20.
    18. Create circular linkedlist having information about an college and perform Insertion atfront performDeletion at end. Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> structnode { int id; char name[20]; char director[20]; char add[20]; char contact[11]; structnode *next; } *start,*rear,*k; typedef structnode node; node *create(); void display(); void addbeg(node*); node *del(node *); main() { int m,o; node *p; start=NULL; Experiment No - 5
  • 21.
    printf("ntNAME- Devesh Kumarn"); printf("ntNorthernIndia Engineering Collegen"); printf("nt"); printf("To PerformDeletion At End"); printf("nnt"); printf("ntPleasePress Enter To Continue :"); getch(); printf("nt"); printf("ntEnter Initial Number Of Colleges :"); scanf("%d",&m); while(m--) { p=create(); addbeg(p); } while(o!=0) { printf("nntPleasePress Enter To Continue"); getch(); printf("nntPress 1 To Insertnew Record At Front"); printf("nntPress 2.To Delete Record From End"); printf("nntPress 3.To Display AllRecords"); printf("nntPress 0.To Exit"); printf("nntPress Your Choice: "); scanf("%d",&o); switch(o) { case 1: p=create(); addbeg(p); break; case 2: start=del(start); break; case 3: display( ); break;
  • 22.
    } } } node *create() { node *k; k=(node*)malloc(sizeof(node)); printf("ntEnter The ID : "); scanf("%d",&k->id); printf("ntEnter Name Of College : "); scanf("%s",k->name); printf("ntEnter Name Of Director : "); scanf("%s",k->director); printf("ntEnter Address Of TheCollege : "); scanf("%s",k->add); printf("ntEnter Contact Number : "); scanf("%s",k->contact); k->next=NULL; return k; } void display() { node *k; int count=0; k=start; printf("nntS.No.tIDtNametDirectortAddressttContactn"); do { count++; printf("nt%dt%dt%st%st%stt%sn",count,k->id, k>name,k>director,k>add,k->contact); k=k->next;
  • 23.
    } while(k!=start); } void addbeg(node*k) { node *n=start; if(start==NULL) { start=k; start->next=start; } else { while(n->next!=start) n=n->next; k->next=start; start=k; n->next=start; } } node*del(node *start) { node *p,*q; p=start; if(p==NULL) { printf("ntListIs Emptyn"); } else { while(p->next!=rear) { q=p; p=p->next; }
  • 24.
  • 25.
    23. Create a stackand performPop, Push, Traverseoperations on the stack using Linear Linked list. Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> structstack { int info; struct stack *next; } *top,*rear,*k; typedef structstack stack; stack *create(int); void push(stack *); void display(stack *); void peep(int); void pop(); void main() { int m=8,a,choice1; stack *p; top=NULL; printf("ntNAME- Devesh Kumarnt"); printf("ntNorthern India Engineering Collegent"); printf("nt"); printf("ntPleasePress Enter To Continuent"); Experiment No - 6
  • 26.
    getch(); printf("ntEnter Eight Numbersn"); while(m--) { printf("nt"); scanf("%d",&a); p=create(a); push(p); } printf("nt"); dispaly(top); printf("nt"); do { printf("ntEnter1 To PUSHnt"); printf("ntEnter 2 To POPnt"); printf("ntEnter 3 To SEARCHnt"); printf("ntEnter Your Choice :"); scanf("%d",&m); switch(m) { case 1: printf("ntEnter A Value -"); scanf("%d",&m); printf("n"); p=create(m); push(p); break; case 2: printf("n"); pop(); break; case 3: printf("ntEnter Value To Search -"); scanf("%d",&a); printf("nn");
  • 27.
    peep(a); break; default: printf("ntYou HaveEnteredWrong Choicen"); } printf("t"); dispaly(top); printf("nt"); printf("ntEnter 1 To Continue :"); scanf("%d",&choice1); } while(choice1==1); getch(); } stack *create(int m) { stack *k; k=((stack *)malloc(sizeof(stack))); k->info=m; k->next=NULL; return k; } void push(stack *n) { if(top==NULL) { top=n; } else { n->next=top; top=n; } }
  • 28.
    dispaly(stack *k) 26. { while(k!=NULL) { printf("%d->",k->info); k=k->next; } } voidpeep(int val) { stack *p=top; stack *k; int found =1; while(p!=NULL) { if(p->info==val) { printf("tValueFoundnn"); found=0; break; } p=p->next; } if(found) printf("ntValueNot Found"); } void pop( ) { stack *k=top; top=top->next; free(k); }
  • 29.
    27. Create a LinearQueue using Linked Listand implement different operations such as Insert, Delete, and Display the queue elements. Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> structqueue { int info; structqueue *next; } *front,*rear,*k; typedef structqueue queue; queue *create(int); void addq(queue*); void delq(); void display(queue*); void search(int); void main() { int m=8,a,choice; queue *p; front=rear=NULL; printf("ntNAME - Devesh Kumarn"); printf("ntNorthern India Engineering Collegen"); printf("ntPress a key : nt"); Experiment No - 7
  • 30.
    getch(); printf("n"); printf("ntEnter Eight Numbersn"); printf("nn"); while(m--) { printf("t"); scanf("%d",&a); p=create(a); addq(p); } printf("t"); printf("nnt"); display(front); printf("nntMENUn"); printf("nntEnteryour choicen"); printf("ntTo Add Node, Enter 1 nnt"); printf("ntTo Delete Node, Enter 2nnt"); printf("ntTo Search Value, Enter 3nnt"); scanf("%d",&m); switch(m) { case 1: printf("nntEnter a Value = n"); scanf("%d",&m); p=create(m); addq(p); break; case 2: delq(); break; case 3: printf("ntEnter Value to search = "); scanf("%d",&a); search(a);
  • 31.
    break; default: printf("ntMismatch Casen"); printf("tEnter1 for Continue :"); scanf("%d",&choice); } printf("nnt"); display(front); getch(); } queue *create(int m) { queue *k; k=((queue*)malloc(sizeof(queue))); k->info=m; k->next=NULL; return k; } void addq(queue*n) { if(front==NULL) front=rear=n; else { rear->next=n; rear=n; } } void display(queue*k) { while(k!=NULL) {
  • 32.
    printf("%d->",k->info); 30. k=k->next; } } void search(intval) { queue*p=front; queue *k; Intfound =1; while(p!=NULL) { if(p->info==val) { printf("ntValueFoundn"); found=0; break; } p=p->next; } if(found) printf("tValueNot Foundn"); } void delq( ) { queue *p=front; front=front->next; free(p); }