38
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: 07 - 13

Data Structure Project File

Embed Size (px)

Citation preview

Page 1: Data Structure Project File

INDEXS. 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

Page 2: Data Structure Project File

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

Page 3: Data Structure Project File

01.

Perform Linear Search and Binary Search on an array.

Description of programs:

a. Read an 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 position where the 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("\n\tNAME - Devesh Kumar\n\t"); printf("\n\tNorthern India Engineering College\n\t"); printf("\n\t");

while(choice!=3) { printf("\n\n\tPress 1 For Linear Search"); printf("\n\n\tPress 2 For Binary Search"); printf("\n\n\tPress 3 For Exit"); printf("\n\n\tPlease Enter your choice :");

Experiment No - 1

Page 4: Data Structure Project File

scanf("%d",&choice); switch(choice) { case 1: LinearSearch(); break;

case 2: BinarySearch(); break;

case 3: Exit(1);

default: printf("\n\n\tYou Have Entered Wrong Choice"); printf("\tEnter 1 for Continue :"); scanf("%d",&choice); } } getch(); }

void LinearSearch() { printf("\n\n\tLinear Search :"); printf("\n\n\tEnter The Size Of The Array : "); scanf("%d",&n); printf("\n\n\tEnter The Sorted Array"); printf("\n\n\tEnter The %d Elements : \n",n); printf("\n\t"); for(i=0;i<n;i++) { scanf("%d",&a[i]); printf("\n\t"); } printf("\n\n\tEnter The Elements To Be Searched :"); scanf("%d",&j); printf("\n\n\tOriginal Array\n\n\t"); for(i=0;i<n;i++) {

Page 5: Data Structure Project File

printf("%d\t",a[i]); } for(i=0;i<n;i++) { if(a[i]==j) { printf("\n\n\tElement %d Is Found At Location %d\n",j,i+1); break; } else { count++; } } if(count==n) { n=-1; printf("\n\n\tElement %d is not in this array",n); } }

void BinarySearch(){ printf("\n\n\tBinary Search : "); printf("\n\n\tEnter The Size Of The Array :"); scanf("%d",&n); printf("\n\n\tEnter The Sorted Array"); printf("\n\n\tEnter The %d Elements :\n\t",n); printf("\n\t"); for(i=0;i<n;i++) { scanf("%d",&a[i]); printf("\n\t"); } printf("\n\n\tEnter The Elements To Be Searched :"); scanf("%d",&j); printf("\n\n\tOriginal array \n\n\t");

Page 6: Data Structure Project File

for(i=0;i<n;i++) 04.

{ printf("%d\t",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("\n\n\tElement %d Is Found At Location %d\n",j,mid+1); yn=0; break; } } if(yn==1) { j=-1; printf("\n\n\tElement %d Is Not In This Array",j); } }

Page 7: Data Structure Project File

05.

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.

Source Code:

#include<stdio.h>#include<conio.h>main( ){ int A[10][10],B[10][3],m,n,s=0,i,j; printf("\n\tNAME - Devesh Kumar\n\t"); printf("\n\tNorthern India Engineering College\n\t"); printf("\n\t"); printf("\n\tEnter the order MxN of the matrix\n\t"); printf("\n\tEnter the value of M :"); scanf("%d",&m); printf("\n\tEnter the value of N :"); scanf("%d",&n); printf("\n\tEnter the elements in the matrix\n\n\t"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\n\t%d Row and %d Column - \t",i,j); scanf("%d",&A[i][j]); } }

Experiment No - 2

Page 8: Data Structure Project File

printf("\n\tThe given matrix is:\n\n\t"); 06.

for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d",A[i][j]); printf("\t"); } printf("\n\n\t");} 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("\n\tThe sparse matrix is given by:\n\n"); printf("\tValue\tRow\tColumn\n\n\t"); for(i=0;i<s;i++) { for(j=0;j<3;j++) { printf("%d",B[i][j]); printf("\t"); } printf("\n\n\t"); } getch();}

Page 9: Data Structure Project File

07.

3. Create a linked list with nodes having information about a student and perform

Description of 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.

Source Code:

#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<string.h>struct student{ char name[100]; int roll; struct student *next; }; struct student *first=NULL,*last=NULL,*k; void create(int n) { int i; first=(struct student*)malloc(sizeof(struct student)); printf("\n\tEnter The Name Of The Student:"); scanf("%s",first->name); printf("\n\tEnter The Roll Number Of The Student:"); scanf("%d",&first->roll); first->next=NULL; last=first; for(i=1;i<n;i++)

Experiment No - 3

Page 10: Data Structure Project File

{ k=(struct student*)malloc(sizeof(struct student)); printf("\n\tEnter The Name Of The Student:"); scanf("%s",k->name); printf("\n\tEnter The Roll Number Of The Student:"); scanf("%d",&k->roll); k->next=NULL; last->next=k; last=k; } }

void display( ) { struct student *t; int count=0; t=first; printf("\n\tThe Details Of The Students Are\n\n"); printf("\tS.No.\tRoll No.\tName\n"); while(t!=NULL) { count++;

printf("\n\t%d\t%d\t\t%s",count,t->roll,t->name);

t=t->next; } }

void insertafter() { int r; int flag=0; struct student *t; printf("\n\tEnter The Roll Number You Want To Insert After That:"); scanf("%d",&r); printf("\n\t");

Page 11: Data Structure Project File

if(first!=NULL) { t=first; while(t!=NULL) { if(t->roll==r) { k=(struct student*)malloc(sizeof(struct student));

printf("\n\tRoll No\t\t\tName\n"); printf("\n\t%d\t\t\t%s",t->roll,t->name); printf("\n\n\tEnter The Name Of The Student : "); scanf("%s",k->name) ; printf("\n\tEnter 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("\n\tThe Element Not Found!!!"); }else printf("\tElement Not Found ");}

void del(){ struct student *back,*t,*k; int r; int flag=0; if(first!=NULL) { printf("\n\tEnter The Roll Number You Want To Delete:");

Page 12: Data Structure Project File

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("\n\tThe Record Deleted : "); getch();

} } } if(flag==0) printf("\n\tThe Element Not Found!!!"); } else { printf("\n\tNo Record In The Database"); getch(); } } void search() { int r; int flag=0; struct student *t; if(first!=NULL)

Page 13: Data Structure Project File

{ printf("\n\tEnter The Roll Number You Want To Search:"); scanf("%d",&r); t=first; while(t!=NULL) { if(t->roll==r) { printf("\n\n\tThe Roll Number Found In The List!!!\n”); printf(\n\tHis Name Is %s\n",t->name); flag=1; break; } t=t->next; } if(flag==0) printf("\n\tThe Roll Number Not In Database!!"); } else { printf("\n\tNo Element In The List "); getch(); }} void reverse_list() { struct student *temp,*temp1,*var; temp=first; var=NULL; while(temp!=NULL) { temp1=var; var=temp; temp=temp->next; var->next=temp1; } first=var;

Page 14: Data Structure Project File

}

int main() { int n,o; printf("\n\tNAME - Devesh Kumar\n\t"); printf("\n\tNorthern India Engineering College\n\t"); printf("\n\t");

while(o!=0) { printf("\n\n\tPlease Press Enter To Continue : \n\n\t"); getch(); printf("\n\tEnter 1 For Creating Database"); printf("\n\tEnter 2 For Displaying Database"); printf("\n\tEnter 3 For Inserting An Record After Another"); printf("\n\tEnter 4 For Deleting A Record"); printf("\n\tEnter 5 For Searching A Record"); printf("\n\tEnter 6 Reversing The Nodes"); printf("\n\tEnter 0 For Exit!"); printf("\n\tPlease Enter Your Choice:"); scanf("%d",&o); switch(o) { case 1: printf("\n\tEnter The Maximum Size Of The Data Base:"); scanf("%d",&n); create(n); break;

case 2: display( ); break;

case 3: insertafter( ); break;

case 4: del(); 13.

break;

Page 15: Data Structure Project File

case 5: search(); break; case 6: reverse_list(); display(); break;

case 0: exit(0); break;

default: printf("\n\tYou Have Entered A Wrong Choice!!!"); } }getch();}

14. Experiment No - 4

Page 16: Data Structure Project File

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.

Source Code:

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct employee { int num; long int salary; char name[20]; struct employee *prev;

struct employee *next; }

*start,*rear,*k;

typedef struct employee employee;

employee *create(); void addemployee(employee *); void display(employee *); void addbeg(employee *); void del( );

void main( ){ int m,o;employee *p;

Page 17: Data Structure Project File

start=NULL; rear=NULL; printf("\n\tNAME - Devesh Kumar\n\t"); printf("\n\tNorthern India Engineering College\n\t"); printf("\n\t"); printf("Deletion At End"); printf("\n\t\n\t"); printf("\n\tEnter the Number of Employees :"); scanf("%d",&m); while(m--) { p=create(); addbeg(p); }

while(o!=0) { printf("\n\n\tPlease Press Enter to Continue \n\t"); getch();

printf("\n\tEnter 1 To Insert New Record\n\t"); printf("\n\tEnter 2.To Delete Record From End\n\t"); printf("\n\tEnter 3.To Display All Records \n\t"); printf("\n\tEnter 0.To Exit\n\t"); printf("\n\tEnter Your Choice :"); scanf("%d",&o);switch(o) { case 1: p=create(); addbeg(p); break;

case 2: del(); break;

case 3: display(start); break;

Page 18: Data Structure Project File

} } }

employee *create() { employee *k;

k=((employee *)malloc(sizeof(employee))); printf("\n\tEnter The Employee Number : "); scanf("%d",&k->num); printf("\n\tEnter The Name of The Employee : "); scanf("%s",k->name); printf("\n\tEnter 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("\n\tS.No.\tEmployee Number\t\tName\t\tSalary\n"); while(k!=NULL) { count++; printf("\n\t%d\t%d\t\t\t%s\t\t%ld",count,k->num,k->name,k->salary); k=k->next; } }

void addbeg(employee *k) { if(start==NULL) { 17.

start=k;

Page 19: Data Structure Project File

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("\n\tNode Deleted\n\t"); } }

18. Experiment No - 5

Page 20: Data Structure Project File

Create circular linked list having information about an college and perform Insertion at front perform Deletion at end.

Source Code:

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct node { int id; char name[20]; char director[20]; char add[20]; char contact[11]; struct node *next; }

*start,*rear,*k;

typedef struct node node;

node *create();void display();void addbeg(node *);node *del(node *);main() { int m,o; node *p;

start=NULL; printf("\n\tNAME - Devesh Kumar\n"); printf("\n\tNorthern India Engineering College\n");

Page 21: Data Structure Project File

printf("\n\t"); printf("To Perform Deletion At End"); printf("\n\n\t"); printf("\n\tPlease Press Enter To Continue :"); getch(); printf("\n\t"); printf("\n\tEnter Initial Number Of Colleges :"); scanf("%d",&m); while(m--) { p=create(); addbeg(p); } while(o!=0) { printf("\n\n\tPlease Press Enter To Continue"); getch(); printf("\n\n\tPress 1 To Insert new Record At Front"); printf("\n\n\tPress 2.To Delete Record From End"); printf("\n\n\tPress 3.To Display All Records"); printf("\n\n\tPress 0.To Exit"); printf("\n\n\tPress Your Choice : "); scanf("%d",&o); switch(o) { case 1: p=create(); addbeg(p); break; case 2: start=del(start); break;

case 3: display( ); break; } } }

Page 22: Data Structure Project File

node *create(){ node *k; k=(node *)malloc(sizeof(node));

printf("\n\tEnter The ID : "); scanf("%d",&k->id); printf("\n\tEnter Name Of College : "); scanf("%s",k->name); printf("\n\tEnter Name Of Director : "); scanf("%s",k->director); printf("\n\tEnter Address Of The College : "); scanf("%s",k->add); printf("\n\tEnter Contact Number : "); scanf("%s",k->contact);

k->next=NULL; return k; }

void display(){ node *k; int count=0; k=start; printf("\n\n\tS.No.\tID\tName\tDirector\tAddress\t\tContact\n"); do { count++; printf("\n\t%d\t%d\t%s\t%s\t%s\t\t%s\n",count,k->id, k>name,k>director,k>add,k->contact); k=k->next; } while(k!=start);}

Page 23: Data Structure Project File

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("\n\tList Is Empty\n");} else { while(p->next!=rear) { q=p; p=p->next; } printf("\n\tRecord Deleted"); 22.

q->next=p->next; }

Page 24: Data Structure Project File

rear=q; return start; }

23. Experiment No - 6

Page 25: Data Structure Project File

Create a stack and perform Pop, Push, Traverse operations on the stack using Linear Linked list.

Source Code:

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct stack { int info; struct stack *next; }

*top,*rear,*k;

typedef struct stack 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("\n\tNAME - Devesh Kumar\n\t"); printf("\n\tNorthern India Engineering College\n\t"); printf("\n\t"); printf("\n\tPlease Press Enter To Continue\n\t"); getch();

printf("\n\tEnter Eight Numbers\n");

Page 26: Data Structure Project File

while(m--) { printf("\n\t"); scanf("%d",&a); p=create(a); push(p); } printf("\n\t"); dispaly(top); printf("\n\t"); do { printf("\n\tEnter 1 To PUSH\n\t"); printf("\n\tEnter 2 To POP\n\t"); printf("\n\tEnter 3 To SEARCH\n\t"); printf("\n\tEnter Your Choice :"); scanf("%d",&m); switch(m) { case 1: printf("\n\tEnter A Value -"); scanf("%d",&m); printf("\n");

p=create(m); push(p); break;

case 2: printf("\n"); pop();

break; case 3: printf("\n\tEnter Value To Search -");

scanf("%d",&a); printf("\n\n"); peep(a); break;

default: printf("\n\tYou Have Entered Wrong Choice\n");

Page 27: Data Structure Project File

} printf("\t"); dispaly(top); printf("\n\t"); printf("\n\tEnter 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) {

Page 28: Data Structure Project File

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("\tValue Found\n\n"); found=0; break;

} p=p->next; } if(found) printf("\n\tValue Not Found"); } void pop( ) { stack *k=top; top=top->next; free(k); }

27. Experiment No - 7

Page 29: Data Structure Project File

Create a Linear Queue using Linked List and implement different operations such as Insert, Delete, and Display the queue elements.

Source Code:

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct queue { int info; struct queue *next; } *front,*rear,*k; typedef struct queue 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("\n\tNAME - Devesh Kumar\n"); printf("\n\tNorthern India Engineering College\n"); printf("\n\tPress a key : \n\t"); getch(); printf("\n"); printf("\n\tEnter Eight Numbers\n"); printf("\n\n"); while(m--)

Page 30: Data Structure Project File

{ printf("\t"); scanf("%d",&a); p=create(a); addq(p); }

printf("\t"); printf("\n\n\t"); display(front); printf("\n\n\tMENU\n"); printf("\n\n\tEnter your choice\n"); printf("\n\tTo Add Node, Enter 1 \n\n\t"); printf("\n\tTo Delete Node, Enter 2\n\n\t"); printf("\n\tTo Search Value, Enter 3\n\n\t"); scanf("%d",&m); switch(m) {

case 1: printf("\n\n\tEnter a Value = \n");scanf("%d",&m);p=create(m);addq(p);break;

case 2: delq();break;

case 3: printf("\n\tEnter Value to search = ");scanf("%d",&a);search(a);break;

default: printf("\n\tMismatch Case\n"); printf("\tEnter 1 for Continue :"); scanf("%d",&choice); }

Page 31: Data Structure Project File

printf("\n\n\t"); 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(int val)

Page 32: Data Structure Project File

{ queue *p=front; queue *k; Int found =1; while(p!=NULL) { if(p->info==val) { printf("\n\tValue Found\n"); found=0;

break; } p=p->next;

} if(found) printf("\tValue Not Found\n");

} void delq( ) { queue *p=front; front=front->next; free(p); }