Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity...

48
Data Structure using C Practical File Submitted to :- Submitted by :- Ms. Neetu Narayan Sooraj randhir Singh Department of Computer Science and Engineering A23053214334 Btech 3CSE5Y

Transcript of Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity...

Page 1: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

Data Structure using C Practical File

Submitted to :- Submitted by :-

Ms. Neetu Narayan Sooraj randhir Singh

Department of Computer Science and Engineering A23053214334

Btech 3CSE5Y

Department of Computer Science and EngineeringAmity School of Engineering and Technology

Amity UniversityUttar Pradesh

Session: - 2015-16

Page 2: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

Data Structure using C Practical File

Submitted to :- Submitted by :-

Ms. Neetu Naryana Suraj Jena

Asst. Professor A23053214283

Department of Computer Science and Engineering Btech 3CSE5X

Department of Computer Science and EngineeringAmity School of Engineering and Technology

Amity UniversityUttar Pradesh

Session: - 2015-16

Page 3: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

1) Write a program to implement all the operations in arrayA. traversingB. insertingC. deletingD. searching

#include<stdio.h>#include<conio.h>#include<process.h>void main(){

clrscr();int a[100],choise;printf("enter the values for the 10 membered array\n");for(int i=0;i<10;i++){

scanf("%d",&a[i]);}clrscr();

while(1){printf("1. enter 1 for traversing by printing data in 10 member

array\n");printf("2. enter 2 for insert a element at kth postion\n");printf("3. enter 3 for delete an element of kth postion\n");printf("4. enter 4 for searching an elements p[ostion\n");printf("5. enter 5 for exit\n");scanf("%d",&choise);clrscr();switch(choise){case 1 : clrscr();

printf("the element in the array are \n");int i;for(i=0;i<10;i++){

printf("%d \n",a[i]);}getch();break;

case 2 : int k,ele,temp[100],j=0;clrscr();printf("enter the value of k");scanf("%d",&k);scanf("%d",&ele);for(i=0;i<11;i++){ if(i==k){

temp[j]=ele;j++;

}

temp[j]=a[i];j++;

}for(i=0;i<11;i++){

a[i]=temp[i];}

Page 4: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

for(i=0;i<11;i++){

printf("%d \n",a[i]);}getch();break;

case 3: clrscr();printf("enter the value of k");scanf("%d",&k);j=0;for(i=0;i<10;i++){ if(i!=k){

temp[j]=a[i];j++;

}}for(i=0;i<10;i++){

a[i]=temp[i];}for(i=0;i<10;i++){

printf("%d \n",a[i]);}getch();break;

case 4: clrscr();printf("enter an element to search");scanf("%d",&ele);k=15;for(i=0;i<10;i++){

if(a[i]==ele){

k=i;break;

}}if(k==15){

printf("element not found");}else{

printf("element found at %d",k+1);}getch();break;

case 5: exit(0);break;

default:clrscr();printf("you entered a wrong input");break;

}clrscr();}

}

Page 5: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 6: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

2) LINEAR SEARCH

#include<stdio.h>#include<conio.h>

void main(){

int a[10],i,n,m,c=0;clrscr();printf("Enter the no of elements in array \n");scanf("%d",&n);printf("Enter the elements of the array \n");for(i=0;i<=n-1;i++){

scanf("%d",&a[i]);}printf("Enter the number to be search \n");scanf("%d",&m);for(i=0;i<=n-1;i++){

if(a[i]==m){

c=1;break;

}}if(c==0)

printf("The number is not in the list \n");else

printf("The number is found");getch();

}

Page 7: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

3) BINARY SEARCH

#include<stdio.h>#include<conio.h>

void main(){

int a[10],i,n,m,c=0,l,u,mid;clrscr();printf("Enter the size of an array \n ");scanf("%d",&n);printf("Enter the elements in ascending order \n");for(i=0;i<n;i++){

scanf("%d",&a[i]);}printf("Enter the number to be search \n");scanf("%d",&m);l=0,u=n-1;while(l<=u){

mid=(l+u)/2;if(m==a[mid]){

c=1;break;

}else if(m<a[mid]){

u=mid-1;}else

l=mid+1;}if(c==0)

printf("The number is not found.");else

printf("The number is found.");getch();

}

Page 8: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 9: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

4) BUBBLE SORTING

#include <stdio.h>#include <conio.h>

void main(){

int array[100], n, c, d, swap;clrscr();printf("Enter number of elements\n");scanf("%d", &n);printf("Enter %d integers\n", n);for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++){

for (d = 0 ; d < n - c - 1; d++){

if (array[d] > array[d+1]){

swap = array[d];array[d] = array[d+1];array[d+1] = swap;

}}

}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )printf("%d\n", array[c]);

getch();}

Page 10: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

5) INSERTION SORTING

#include<stdio.h>#include<conio.h>

void main(){

int n, A[50], c, d, t;clrscr();printf("Enter number of elements");scanf("%d", &n);printf("Enter array-", n);for (c = 0; c < n; c++){

scanf("%d", &array[c]);}for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && A[d] < A[d-1]) { t = A[d];A[d] = A[d-1];A[d-1] = t;d--;}}printf("Sorted list in ascending order:\n");for (c = 0; c <= n - 1; c++){ printf("%d\n", array[c]);}getch();

}

Page 11: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

6) SELECTION SORTING

#include <stdio.h>#include <conio.h>

void main(){

clrscr();int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");scanf("%d", &n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ ){

position = c;

for ( d = c + 1 ; d < n ; d++ ){

if ( array[position] > array[d] )position = d;

}if ( position != c ){

swap = array[c];array[c] = array[position];array[position] = swap;

}}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )printf("%d\n", array[c]);

getch();}

Page 12: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

7) WRITE A PROGRAM TO IMPLEMENT A QUICK SORT

#include<stdio.h>#include<conio.h>void quicksort(int [10],int,int);

void main(){clrscr();int x[20],size,i;

printf("Enter size of the array: ");scanf("%d",&size);

printf("Enter %d elements: ",size);for(i=0;i<size;i++)

scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");for(i=0;i<size;i++)

printf(" %d",x[i]);getch();

}

void quicksort(int x[10],int first,int last){int pivot,j,temp,i;

if(first<last){pivot=first;i=first;j=last;

while(i<j){while(x[i]<=x[pivot]&&i<last)

i++;while(x[j]>x[pivot])

j--;if(i<j){

temp=x[i];x[i]=x[j];x[j]=temp;

}}

temp=x[pivot];x[pivot]=x[j];x[j]=temp;quicksort(x,first,j-1);quicksort(x,j+1,last);

}}

Page 13: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 14: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

8) Write a program to implement all the operations in stackA. Push operationB. Pop operationC. Display operation

#include<stdio.h>#include<conio.h>#include<process.h>

void main(){

int S[20],a,n;int c,i,b,top=-1;while(1){

clrscr();printf("1.Push\n");printf("2.Pop\n");printf("3.display\n");printf("4.exit\n");scanf("%d",&c);switch(c){ case 1: if(top==n-1)printf(" Stack overflow!");else{

printf("Enter element to be pushed-");scanf("%d",&a);top=top+1;S[top]=a;

}break;

case 2: if(top==-1) printf(" Stack underflow!");

else{

printf("Element popped is-%d",S[top]);top=top-1;

} getch(); break;

case 3: if(top==-1) printf("\nstack emtpy");

else {

for(i=top;i>=0;i--) {

printf("\n%d",S[i]); }

} getch(); break;

case 4: printf(" exit");exit(0);break;

default: printf("Wrong choice!");break;}

}getch();

}

Page 15: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 16: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

9) Write a program to implement all the operations in QueueA. Insertion operationB. Deletion operationC. Display operation

#include<stdio.h>#include<conio.h>#include<stdlib.h>

int front=- 1;int rear;int queue[5];void insert(int item){

if(rear<3){

if(front==-1){

front=0;queue[front]=item;rear=0;

}

else{

rear=rear+1;queue[rear]=item;

}

}else{

printf("\nqueue is full");return;

}}void display(){

int i;if(front==-1){

printf("\nempty");return;

}for(i=front;i<=rear;i++){

printf("\n%d",queue[i]);}

}void del(){int p;

if(front==-1){

printf("\nalready empty");return;

}if(rear>front)

Page 17: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

{p = queue[front];printf("\ndeleted item is %d",p);front=front+1;

}else if(rear==front){

printf("\ndeleted item is %d",queue[0]);front=-1;rear=-1;

}}void main(){

clrscr();int a,item;while(1){

printf("\n1.insert\n2.display\n3.delete\n4.exit\n");scanf("%d",&a);switch(a){case 1: printf("\n enter the item:");

scanf("%d",&item);insert(item);break;

case 2: display();break;

case 3: del();break;

case 4: exit(0);break;

}}getch();

}

Page 18: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 19: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

10) Write a program to implement all the operations in Linked stackA. Push operationB. Pop operationC. Display operation

#include <stdio.h>#include<conio.h>#include <stdlib.h>

struct node{

int info;struct node *ptr;

}*top,*top1,*temp;

int topelement();void push(int data);void pop();void empty();void display();void destroy();void stack_count();void create();

int count = 0;

void main(){

int no, ch, e;clrscr();printf("\n 1 - Push");printf("\n 2 - Pop");printf("\n 3 - Top");printf("\n 4 - Empty");printf("\n 5 - Exit");printf("\n 6 - Dipslay");printf("\n 7 - Stack Count");printf("\n 8 - Destroy stack");

create();

while (1){

printf("\n Enter choice : ");scanf("%d", &ch);

switch (ch){case 1:

printf("Enter data : ");scanf("%d", &no);push(no);break;

case 2:pop();break;

case 3:if (top == NULL)

printf("No elements in stack");

Page 20: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

else{

e = topelement();printf("\n Top element : %d", e);

}break;

case 4:empty();break;

case 5:exit(0);

case 6:display();break;

case 7:stack_count();break;

case 8:destroy();break;

default :printf(" Wrong choice, Please enter correct choice ");break;

}}

}

void create(){

top = NULL;}

void stack_count(){

printf("\n No. of elements in stack : %d", count);}

void push(int data){

if (top == NULL){

top =(struct node *)malloc(1*sizeof(struct node));top->ptr = NULL;top->info = data;

}else{

temp =(struct node *)malloc(1*sizeof(struct node));temp->ptr = top;temp->info = data;top = temp;

}count++;

}

void display(){

top1 = top;

Page 21: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

if (top1 == NULL){

printf("Stack is empty");return;

}

while (top1 != NULL){

printf("%d ", top1->info);top1 = top1->ptr;

}}

void pop(){

top1 = top;

if (top1 == NULL){

printf("\n Error : Trying to pop from empty stack");return;

}else

top1 = top1->ptr;printf("\n Popped value : %d", top->info);free(top);top = top1;count--;

}

int topelement(){

return(top->info);}

void empty(){

if (top == NULL)printf("\n Stack is empty");

elseprintf("\n Stack is not empty with %d elements", count);

}

void destroy(){

top1 = top;

while (top1 != NULL){

top1 = top->ptr;free(top);top = top1;top1 = top1->ptr;

}free(top1);top = NULL;printf("\n All stack elements destroyed");count = 0;

}

Page 22: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 23: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

11) Write a program to implement all the operations in Linked QueueA. Insertion operationB. Deletion operationC. Display operation

#include<stdio.h> #include<conio.h>#include<stdlib.h>struct Node{

int Data;struct Node* next;

}*rear, *front;

void delQueue(){

struct Node *temp, *var=rear;if(var==rear){

rear = rear->next;free(var);

}else

printf("\nQueue Empty");}

void push(int value){

struct Node *temp;temp=(struct Node *)malloc(sizeof(struct Node));temp->Data=value;if (front == NULL){

front=temp;front->next=NULL;rear=front;

}else{

front->next=temp;front=temp;front->next=NULL;

}}

void display(){

struct Node *var=rear;if(var!=NULL){

printf("\nElements are as: ");while(var!=NULL){

printf("\t%d",var->Data);var=var->next;

}printf("\n");

} else

Page 24: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

printf("\nQueue is Empty");}

void main(){

clrscr();int i=0;front=NULL;printf(" \n1. Push to Queue");printf(" \n2. Pop from Queue");printf(" \n3. Display Data of Queue");printf(" \n4. Exit\n");while(1){

printf(" \nChoose Option: ");scanf("%d",&i);switch(i){case 1:

{int value;printf("\nEnter a valueber to push into Queue: ");scanf("%d",&value);push(value); display();break;

}case 2:

{delQueue();display();break;

}case 3:

{display();break;

}case 4:

{exit(0);

}default:

{printf("\nwrong choice for operation");

}}

}}

Page 25: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 26: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

12) Write a program to implement all the operations in Linked ListA. Insertion at beginningB. insertion at endC. DisplayD. Deletion at endE. Display Operation

#include<stdio.h>#include<conio.h>#include<stdlib.h>

struct node{

int info;struct node*link;

};

void main(){

clrscr();struct node*temp, *start, *r,*ptr, *loc, *g,*a;start=NULL;int ch,chi, item, terminator=0, search, count=0, x=0, unsorted=0,

insertitem,insertitem2, location=0,del;do{

printf("\nenter your choice\n1.creating list\n2.searching\n3.insertion\n4.Deletion\n5.display\n6.EXIT");

scanf("%d",&ch);switch(ch){case 1:

if(start==NULL){

printf("\nenter the element to be inserted\n");scanf("%d",&item);temp=(struct node*)malloc(sizeof(struct node*));start=temp;temp->link=NULL;temp->info=item;

}else{

printf("enter the element to be inserted\n");scanf("%d",&item);r=(struct node*)malloc(sizeof(struct node*));r->link=NULL;r->info=item;temp->link=r;temp=temp->link;

}break;

case 2:ptr=start;

while(ptr!=NULL){

Page 27: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

if(ptr>ptr->link){

unsorted=1;}ptr=ptr->link;

}printf("enter the element to found \n");scanf("%d",&search);if(unsorted==1){

ptr=start;while(ptr!=NULL){

count++;if(ptr->info==search){

x=1;loc=ptr->link;break;

}ptr=ptr->link;

}if(x==1){

printf("the item is found at location%d \n",count);

count=0;x=0;unsorted=0;

}else{

printf("element not found\n");}

}else{

ptr=start;while(ptr!=NULL || ptr->info<=item){

count++;if(ptr->info==item){

x=1;}ptr=ptr->link;

}if(x==1){printf("element is at %d location\

n",count);count=0;x=0;}else{printf("the element not found\n");}

}break;

case 3:printf("enter your choice insertion at \n1.begining \

n2.after any location \n3.at the end");scanf("%d",&chi);switch(chi){case 1:

printf("enter the element to be insert\n");scanf("%d",&insertitem);

Page 28: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

r=(struct node*)malloc(sizeof(struct node*));r->link=start;r->info=insertitem;start=r;break;

case 2:printf("enter the element to be insert\n");scanf("%d",&insertitem2);printf("enter the location after you want to insert

the element\n");scanf("%d",&location);int i=0;ptr=start;while(i<location){

++i;g=ptr;ptr=ptr->link;

}r=(struct node*)malloc(sizeof(struct node*));r->link=ptr;r->info=insertitem2;g->link=r;break;

case 3:int insertend;printf("enter the element to be insert\n");scanf("%d",&insertend);ptr=start;while(ptr!=NULL){

g=ptr;ptr=ptr->link;

}r=(struct node*)malloc(sizeof(struct node*));r->link=NULL;r->info=insertend;g->link=r;

}break;

case 4:printf("enter your choice of deletion \n1. from beg \n2. from a location, \n3.from end");

scanf("%d",&chi);switch(chi){case 1:

temp=start;start=start->link;free(temp);break;

case 2:printf("enter the location of element to be

deleted\n");scanf("%d",&del);ptr=start;int i=1;while(i<del){

g=ptr;ptr=ptr->link;i++;

Page 29: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

}g->link=ptr->link;break;

case 3:ptr=start;while(ptr!=NULL){

a=g;g=ptr;ptr=ptr->link;

}temp=g;a->link=NULL;free(temp);

}break;

case 5:ptr=start;while(ptr!=NULL){

printf("\n%d",*ptr);ptr=ptr->link;

}break;

case 6:terminator=1;

} }while(terminator==0);getch();

}

Page 30: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 31: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

13) Write a program to implement all the operations in Doubly Linked ListA. Insertion at beginningB. insertion at endC. Display at beginning D. Deletion at endE. Display Operation

#include<stdio.h>#include<conio.h>#include<stdlib.h>struct node{

int info;struct node * next;struct node * prev;

};typedef struct node NODE;NODE *start = NULL;//NODE *last = NULL;void traverse(){

NODE * p;if(start==NULL)

printf("\nnothing to show");else{

p=start;while(p!=NULL){

printf("\n%d",p->info);p=p->next;

}}}

void traverseback(){

NODE * p,*temp;p=start;while(p->next!=NULL){

p=p->next;}temp=p;// temp->next=NULL;while(temp->prev!=NULL){

printf("\n%d",temp->info);temp=temp->prev;

}printf("\n%d",temp->info);

}void insertbeg(int item){

NODE * p;p=(NODE*)malloc(sizeof(NODE));p->info=item;if(start==NULL){

p->next=NULL;p->prev=NULL;

Page 32: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

start=p;}else{

p->next=start;p->prev=NULL;start->prev=p;start=p;

}}void insertend(int item){

NODE * p,*temp;p = (NODE*)malloc(sizeof(NODE));p->info=item;p->next=NULL;if(start==NULL){

//p->next=NULL;p->prev=NULL;start=p;

}else{

temp=start;while(temp->next!=NULL)

temp=temp->next;

temp->next=p;p->prev=temp;

}}void insertpos(int item){ int n,i;NODE * p,*temp,*temp1;p = (NODE*)malloc(sizeof(NODE));printf("\nenter the positon:");scanf("%d",&n);p->info=item;p->next=NULL;

if(n==1){

insertbeg(item);}else{

temp=start;for(i=1;i<n-1;i++){

if(temp->next==NULL){

printf("\n Error position:");exit(0);

}temp=temp->next;

}if(temp->next==NULL)

Page 33: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

{//printf("\ninvalid pos");insertend(item);

}else{

temp1=temp->next;temp->next=p;

p->next=temp1;p->prev=temp;temp1->prev=p;

}

}}void delbeg(){

NODE * p;if(start==NULL)

printf("empty linkd list");else if(start->next==NULL){

p=start;start=NULL;free(p);

}else{

p=start;start=start->next;start->prev=NULL;free(p);

}}void delend(){

NODE * p,*temp;if(start==NULL)

printf("empty linkd list");else if(start->next==NULL){

p=start;start=NULL;free(p);

}else{

p=start;while(p->next!=NULL){

temp=p;p=p->next;

}temp->next=NULL;free(p);

}}void delpos(){

Page 34: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

int pos,i;NODE *p,*temp;printf("enter pos to be deleted");scanf("%d",&pos);if(pos==1){

delbeg();

}else{

temp=start;for(i=1;i<pos;i++){

if(temp->next==NULL){

printf("invalid pos");exit(0);

}

p=temp;temp=temp->next;

}if(temp->next==NULL)

delend();else{

p->next=temp->next;temp->next->prev=p;//temp->next=p->next;free(temp);

}// }

}

}void main(){

clrscr();int i,item;char ch;while(1){

printf("\nenter ur choice:");printf("\n1.add at beginning\n2.add at end \n3.insert at pos\

n4.traverse\n5.traverse back\n6.delete start\n7.del end\n8.del pos\n9.Exit\n");

scanf("%d",&i);switch(i){case 1 :printf("\nenter the no.:");

scanf("%d",&item);insertbeg(item);break;

case 2 : printf("\nenter item:");scanf("%d",&item);insertend(item);

Page 35: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

break;case 3 : printf("\nenter item:");

scanf("%d",&item);insertpos(item);break;

case 4 : traverse();break;

case 5 : traverseback();break;

case 6: delbeg();break;

case 7: delend();break;

case 8: delpos();break;

case 9: exit(0);break;

default : printf("error");break;

}

}getch();

}

Page 36: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 37: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

14) Write a program to implement Graph Traversal algorithmA. Depth first search

#include<stdio.h>#include<conio.h>#include<stdlib.h>char que[20];int front=0, rear=0, n;char arr[20];int bfs(int);char ajMat[20][20];char b[20];void display();int p=0;

void main(){

clrscr();char v;int i,j;printf("Enter the number of nodes in a graph\n");scanf("%d",&n);printf("Enter the value of node of graph\n");for(i=0; i<n; i++){

scanf("%s",&b[i]);}

printf("Enter the value in adjancency matrix in from of 'y' or 'n'\n");

printf("If there exits an edge between two vertices than 'y' otherwise 'n'\n");

for(i=0; i<n; i++)printf(" %c ",b[i]);

for(i=0;i<n; i++){

printf("\n%c ",b[i]);for(j=0; j<n; j++){

printf("%c ",v=getch());ajMat[i][j]=v;

}printf("\n\n");

}for(i=0;i<n;i++)

bfs(i);

display();getch();

}

void display(){

printf("BFS of Graph : ");for(int i=0; i<n; i++)

printf("%c ",arr[i]);}

void insert(char val)

Page 38: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

{que[front]=val;front++;

}

char del(){

rear=rear+1;return que[rear-1];

}

int unVisit(char val){

for(int i=0; i<front; i++){

if(val==que[i])return 0;

}return 1;

}

int bfs(int i){

char m;if(front==0){

insert(b[i]);}for(int j=0; j<n; j++){

if(ajMat[i][j]=='y'){

if(unVisit(b[j])){

insert(b[j]);}

}}m=del();arr[p]=m;p++;return 0;

}

Page 39: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the
Page 40: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

B. Depth First Search

#include<stdio.h>#include<conio.h>

char stack[20];int top=-1, n;char arr[20];char dfs(int );char ajMat[20][20];char b[20];void display();int p=0;

void main(){

clrscr();char v;int l=0;printf("Enter the number of nodes in a graph");scanf("%d",&n);printf("Enter the value of node of graph");for(int i=0; i<n; i++){

scanf("%s",&b[i]);}char k=b[0];printf("Enter the value in adjancency matrix in from of 'Y' or 'N'\

n");printf("\nIf there is an edge between the two vertices then enter 'Y'

or 'N'\n");for(i=0; i<n; i++)

printf(" %c ",b[i]);for(i=0;i<n; i++){

printf("\n%c ",b[i]);for(int j=0; j<n; j++){

printf("%c ",v=getch());ajMat[i][j]=v;

}printf("\n\n");

}for(i=0;i<n;i++){

l=0;while(k!=b[l])

l++;k=dfs(l);

}display();getch();

}

void display(){

printf(" DFS of Graph : ");for(int i=0; i<n; i++)

printf("%c ",arr[i]);}

Page 41: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the

void push(char val){

top=top+1;stack[top]=val;

}char pop(){

return stack[top];}

bool unVisit(char val){

for(int i=0; i<p; i++)if(val==arr[i])

return false;for(int i=0; i<=top; i++)

if(val==stack[top])return false;

return true;}

char dfs(int i){

int k;char m;if(top==-1){

push(b[i]);}m=pop();top--;arr[p]=m;p++;for(int j=0; j<n; j++){

if(ajMat[i][j]=='y'){

if(unVisit(b[j])){

push(b[j]);}

}}return stack[top];

}

Page 42: Microsoft Word...  · Web viewAmity University. Uttar Pradesh. Session: - 2015-16. ... Amity University. Uttar Pradesh. Session: - 2015-16. 1) Write a program to implement all the