Vous êtes sur la page 1sur 28

Submitted By

Jyoti Sharma BCA-IInd Sem R.No: 3875 (A)

Submitted To: KALPNA GUPTA

Name: JYOTI SHARMA Std: BCA-II SEM Sec. A Roll No.3875

Subject:- DATA STRUCTURE PRACTICAL FILE College: Pt. JAWAHAR LAL NEHRU GOVT COLLEGE FARIDABAD
Page No
01 02 03 04 06 07 08 10 14 18 21

S. No.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Date
01/03/2010 03/03/2010 05/03/2010 08/03/2010 10/03/2010 12/03/2010 14/03/2010 17/03/2010 20/03/2010 22/03/2010 25/03/2010 28/03/2010 30/03/2010 02/04/2010 05/04/2010

Title
WAP in C to insertion an Array WAP in C to deletion an Array WAP in C to Linear search an Array Program for Binary search Program for Insertion sort Program for Selection sort Program for Quick sort Program for push in Stack Program for insertion & Deletion in Queue Program for creation in linked list Program for insertion in linked list Program for deletion of any element in linked list Program for linear search in linked list Program for merging Program for concatenation of Two linked list

Remarks

To insert an element at location k.(Array)


#include<stdio.h> #include<conio.h> void main() { int a[100],k,item, n,j,I; clrscr( ); printf(enter how many elements\n); scanf(%d,&n); printf(Enter the %d elements\n,n); for(i=0;i<n;i++) scanf(%d,&a[i]); printf(enter the location\n); scanf(%d,&k); printf(Enter the item to inserted at a[%d]\n,k); scanf(%d,&item); j = n-1; while(j>=k) { a[j+1] = a[j]; j--; } a[k] = item; n++; printf(Resultant array is .\n); for(I = 0; i<n; i++) printf(%4d,a[i]); getch( ); }

Output of the program is:


Enter how many elements 5 Enter the 5 elements 11 22 33 44 55 Enter the location 3 Enter the item to be inserted at a [3] 66 Resultant array is . 11 22 33 66 44 55

To delete an element at location k.(Array)

#include<stdio.h> #include<conio.h> void main() { int a[100],k,item, n,j,i; clrscr( ); printf(enter how many elements\n); scanf(%d,&n); printf(Enter the %d elements\n,n); for(i=0;i<n;i++) scanf(%d,&a[i]); printf(enter the location of number you want to delete \n); scanf(%d,&k); item=a[k]; printf(Deleted item is =%d\n,item); for(j=k; j<n-1; j++) a[j] = a[j+1]; n--; printf(Resultant array is .\n); for(i = 0; i<n; i++) printf(%4d,a[i]); getch( ); }

Output of the program is:


Enter how many elements 5 Enter the 5 elements 11 22 33 44 55 Enter the location of number you want to delete 1 Deleted item is = 22 Resultant array is . 11 33 44 55

Program of linear search (Array)


#include<stdio.h>

#include<conio.h> void main( ) { int loc, i, n,k, a[30], data; clrscr( ); printf (How many elements); scanf (%d,&n); printf (Enter the %d elements\n,n); for (i=0; i<=n; i + +) scanf(%d,&a[i]); printf(Enter the element to be searched\n); scanf(%d,&data); k = 1; loc = 0; while(loc = = 0 && k<=n) { if (a[k] = = data) loc = k; k + +; } if (loc = = 0) printf (Data not found); else printf (Data found at location %d,loc); getch( ); }

Output of the program is :How many elements 7 Enter the 7 elements 98 23 87 34 76 Enter the element to be searched 76 Data found at location 5

45

65

Program of binary search (Array)


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

void main( ) { int loc, i, n, a[30], data, low, high, mid; clrscr( ); printf (How many elements); scanf (%d,&n); printf (Enter the %d elements\n,n); for (i=0; i<=n; i++) scanf(%d,&a[i]); printf(Enter the element to be searched\n); scanf(%d,&data); low = 0; high = n-1; found = n; while(found = = n && low<=high) { mid = (low +high)/2; if (data > a[mid]) low = mid+1; if (data < a[mid]) high = mid-1; if (data = = a[mid]) found =y; } if (found = = y) printf (Data found at location %d,mid+1); else printf (Data not found); getch( ); }

Output of the program is :How many elements 9 Enter the 9 elements

11 22 33 44 55 Enter the element to be searched 55 Data found at location 5 How many elements 9 Enter the 9 elements 11 22 33 44 55 Enter the element to be searched 98 Data not found

66

77

88

99

66

77

88

99

Program:-Insertion sort
#include<stdio.h> #include<conio.h> void insertion_ sort (int a[ ], int n); void main( ); {

int i, n, a[30]; clrscr(); printf("How many elements \n"); scanf(" %d",&n); printf("Enter the %d elements\n",n); for (i = 0; i<n; i + +) scanf("%d",&a[i]); insertion _sort(a,n); getch( ); void insertion _sort (int a[ ], int n) { int i, k, ptr, temp; for(k = 1; k< = n-1; k + + ) { temp = a[k]; ptr = k-1; while (temp<a[ptr] && ptr> = 0) { a[ptr +1] = a[ptr]; ptr--; } a[ptr + 1] = temp; } printf("Sorted elements are ..\n") ; for (i = 0; i < n; i + +) printf("%5d",a[i]); }

Output is:How many elements 7 Enter the 7 elements 25 15 89 78 10 sorted elements are 10 15 25 45 67

45 78

67 89

Program: -selection sort


#include<stdio.h> #include<conio.h> void selection _ sort (int a[ ], int n); void main( ); {

int i, n, a[30]; clrscr(); printf("How many elements \n"); scanf(" %d",&n); printf("Enter the %d elements\n",n); for (i = 0; i<n; i + +) scanf("%d",&a[i]); selection _sort(a,n); getch( ); void selection _sort (int a[ ], int n) { int smallest, loc, i, pass, j, temp; for(pass = 0; pass< = n-2; pass + + ) { smallest = a[pass]; loc = pass; for(j = pass+1; j<=n-1; j + +) if (a[j]< smallest) { smallest = a[j]; loc = j; } temp = a[pass]; a[pass] = a[loc]; a[loc] = temp; } printf("Sorted elements are ..\n") ; for (i = 0; i < n; i + +) printf("%5d",a[i]); }

Output is:How many elements 10 Enter the 7 elements 75 67 54 26 85 sorted elements are 11 19 26 37 43

19 54

37 67

98 75

43 85

11 98

Program: -quick sort


#include<stdio.h> #include<conio.h> void quick _ sort (int a[ ], int first, int last); void main( ); { int i, n, a[30];

clrscr(); printf("How many elements \n"); scanf(" %d",&n); printf("Enter the %d elements\n",n); for (i = 0; i<n; i + +) scanf("%d",&a[ i ]); quick _sort(a,0,n-1); printf("Sorted elements are ..\n") ; for (i = 0; i < n; i + +) printf("%5d",a[ i ]); getch( ); void quick _sort (int a[ ], int first, int last) { int i, k, j, temp; i = first; j = last; k = first; do { i + +; while(a[ i ]<a[ k ]) i + +; while(a[ j ]>a[ k ]) j - -; if(i<j) { temp = a i ]; a[ i ] = a[ j ]; a[ j ] = temp; } }while(i<j); temp = a[ k ]; a[ k ] = a [ j ] ; a[ j ] = temp; if (first<( j-1)) quick_sort(a, first, j-1); if(( j + 1)< last) quick_sort(a, j+1, last ); }

Output is:How many elements 10 Enter the 7 elements 64 20 75 9 99 sorted elements are

31

97

42

86

53

20

31

42

53

64

75

86

97

99

Program :- Array implementaion of stack


#include<stdio.h> #include<conio.h> #define SIZE 10 void push(int stack[], int item);

void pop(int stack[]); void display(int stack[]); int top; void main( ) { int stack[SIZE], val, element, choice; clrscr(); printf(MAIN MENU\n); printf(For push operation enter choice =1\n); printf("For pop operation enter choice =2\n"); printf("For exit enter choice =3\n"); top=-1; do { printf("Enter the choice:\n"); scanf("%d",&choice); if(choice = =1) { printf("Enter the element to push\n"); scanf("%d",&element); push(stack, element); } else if (choice = =2) { pop(stack); } } while(choice = =1 || choice = =2); } void push(int stack[], int item) { if(top = =SIZE-1)

{ printf(stack is full\n); } else { top++; stack[top]=item; display(stack); } } void pop(int stack[]) { int val; if(top = = -1) { printf(stack is empty\n); } else { val=stack[top]; top--; printf("The poped item is =%d\n",val); display(stack); } } void display(int stack[]) { int i; printf("Stack elements are ..\n"); for(i=top;i>=0;i--) printf("%d\n",stack[i]);

Output of the program is:MAIN MENU For Push operation enter choice =1 For Pop operation enter choice =2 For exit enter choice =3 Enter your choice: 2 stack is empty Enter the element to push 11 stack elements are.. 11 Enter your choice: 1 Enter the element to push 22 stack elements are .. 22 11 Enter your choice: 1 Enter the element to push 33 stack elements are .. 33 22 11 Enter your choice:

2 The popped item is = 33 Stack elements are. 22 11 Enter your choice: 3

Program for insertion and deletion in a queue

#include<stdio.h> #include<conio.h> #define SIZE 4

void qinsert(int q[], int item); void qdelete(int q[]); void display(int q[]); int front, rear; void main() { int q[SIZE], val, element, choice; clrscr(); front = -1; rear = -1; printf(MAIN MENU\N); printf(For insert operation enter choice =1\n); printf(For delete operation enter choice =2\n); printf(For exit enter choice =3\n); do { printf(Enter your choice: ); scanf(%d,&choice); if(choice = =1) { printf(Enter the element to insert ); scanf(%d,&element); qinsert(q, element); } else if (choice = =2) { qdelete(q); } }while(choice = = 1 || choice = =2); } void qinsert(int q[], int val)

{ if (rear = = SIZE-1) { printf(Queue is full\n); } else { if(rear = = -1 && front = = -1) { front =0; rear =0; q[rear] = val; } else { rear + +; q[rear] = val; } display(q); } } void q delete(int q[]) { int val; if(rear = = -1) { printf(Queue is empty\n); } else if (rear = = front) { val = q[front];

front = -1; rear = -1; printf(The deleted item is =%d\n, val); display(q); } } void display(int q[]) { int i; printf(Queue elements are \n); printf(Front - - - >); for( i = front; i< = rear; i + +) printf(%4d,q[i]); printf(<- - -Rear); printf(\n); }

Output of the program is .


MAIN MENU For insert operation enter choice = 1 For Delete operation enter choice = 2 For exit enter choice = 3 Enter your choice: 2

Queue is empty Enter your choice : 1 Enter the element to insert 11 Queue elements are.. Front - - -> 11 <- - -Rear Enter your choice : 1 Enter the element to insert 22 Queue elements are.. Front - - -> 11 22 <- - -Rear Enter your choice : 1 Enter the element to insert 33 Queue elements are.. Front - - -> 11 22 33 <- - -Rear Enter your choice: 2 The deleted item is :11 Queue elements are.. Front - - -> 22 33 <- - -Rear Enter your choice : 1 Enter the element to insert 44 Queue elements are.. Front - - -> 11 22 33 44 <- - -Rear Enter your choice : 1 Enter the element to insert 55 Queue elements are.. Front - - -> 11 22 33 44 55 <- - -Rear Queue is full Enter your choice: 3

Program for creation in linked list


#include<stdio.h> #include<alloc.h> void createlist(struct node **first) void traverse(struct node *first)

struct node { int info; struct node *next; }; void main(); { struct node *list; list = NULL; createlist(&list); traverse(list); } void createlist (struct node **first) { struct node *z, *ptr; int data; printf("Enter the data to insert(-1 to terminate)"); scanf("%d",&data); while(data ! = -1) { z= (struct node *) malloc(size of (struct node)); z->info = data; z->next = NULL; if((*first) = = NULL) (*first) =z; else ptr-> next =z; ptr = z; printf("Enter the data to insert(-1 to terminate)"); scanf(%d,&data); }

} void treverse(struct node *first) { int count; struct node *ptr; if(first = = NULL) { Printf("List empty. Press any key to continue.\n"); Getch(); } else { count =0; ptr = first; printf("first->"); while(ptr ! =NULL) { printf("%d->",ptr->info); count + +; ptr = ptr->next; } printf("null\n"); printf("Number of nodes =%d",count); getch( ); } }

Output of the program:Enter the data to insert (-1 to terminate) 11 Enter the data to insert (-1 to terminate) 22 Enter the data to insert (-1 to terminate) 33

Enter the data to insert (-1 to terminate) 44 Enter the data to insert (-1 to terminate) 55 Enter the data to insert (-1 to terminate) -1 First-> 11-> 22-> 33-> 44-> 55->null Number of nodes = 5

Program for insertion in linked list


#include<stdio.h> #include<alloc.h> typedef struct node {

int info; struct node *next; } NODE; void insert(NODE **first, int loc, int data); void create(NODE ** first); void traverse(NODE *first); void main( ); { int data, loc; NODE *list; list = NULL; create(&list); printf("linked list before insertion.\n"); traverse(list); printf("enter the position at which you what to insert new node: "); scanf("%d",&loc); printf("enter the data value of new node: "); scanf(%d,&data); insert(&list, loc, data); printf("linked list after insertion.\n"); traverse(list); } void create(NODE ** first) { NODE *z, *ptr; int val; printf("enter the value (-1 to exit); "); scanf("%d",&val); while(val ! = -1) {

z = (NODE *)malloc(sizeof(NODE)); z->info = val; z->next = NULL; if((*first) = = NULL) (*first) = z; else ptr->next = z; ptr = z; printf("enter the value(-1 to exit): "); scanf("%d",&val); } } void traverse(NODE *first) { NODE *ptr; ptr = first; printf("first->"); while(ptr ! = NULL) { printf("%d->",ptr->info); ptr = ptr->next; } printf("NULL\n"); } void insert(NODE **first, int loc, int data) { NODE *z, *ptr; int i; ptr = *first; z = (NODE*)malloc(sizeof(NODE)); z->info = data;

if(loc = = 1) { z->next = (*first); (*first) = z; } else { for(i = 2; i<loc; i + +) { ptr = ptr->next; if(ptr = = NULL) { printf("there are less number of nodes so \n"); printf("insertion at desired location is not possible\n"); getch( ); exit( ); } } z->next = ptr->next; ptr->next = z; } }

Output of the program:Enter the value(-1 to exit): 11 Enter the value(-1 to exit): 22 Enter the value(-1 to exit): 33 Enter the value(-1 to exit): 44

Enter the value(-1 to exit): -1 Linked list before insertion. First->11->22->33->44->NULL Enter the position at which you want to insert new node: 3 Enter the data value of new node: 99 Linked list after insertion First->11->22->99->33->44->NULL

Program to merge two sorted array


#include<stdio.h> #include<conio.h> void main ( ); { int a[30], b[30],c[60],I, j, k, m, n, p;

clrscr( ); printf(Enter how many elements in array A\n); scanf(%d,&m) ; printf(Enter hoe many elements in array B\n); scanf(%d,&n); printf(Enter the %d elements of array A\n,m); for(i =0;i<m;i+ +) scanf(%d,&a[i]); printf(Enter the %d elements of array B\n,n); for(i =0;i<n;i+ +) scanf(%d,&b[i]); i = j = k = 0; while(i<m &&j<n) { if(a[i]<=b[j]) { c[k] = a[i]; i + +; k + +; } else { c[k] = b[j]; j + +; k++; } } if(i = = m) for(p =j;p<n;p + +) { c[k] = b[p];

k + +; } else for(p = i;p<m;p + +) { c[k] = a[p]; k + +; } printf(Resultant array is \n); for(i =0;i<m+n;i+ +) printf(%4d,c[i]); getch( ); }

Output of the program:Enter how many elements in array A 5 Enter how many elements in array B 8 Enter the 5 elements of array A 10 5 5 20 15 10 30 25 15 40 35 20 50 45 25 65 30 75 35 40 45 50 65 75 Enter the 8 elements of array B Resultant array is ..

Vous aimerez peut-être aussi