0% found this document useful (0 votes)
62 views

Data Structure Shorting Programs

The document contains code snippets for implementing various operations on linked lists including: 1. Inserting an element in a singly linked list by adding a node after a specified position. 2. Deleting an element from a singly linked list by searching for the element to delete. 3. Implementing operations like insertion, deletion on doubly linked lists including adding a node at the beginning or after a specified position. 4. Deleting the last node from a doubly linked list. 5. Searching for an element in a doubly linked list.

Uploaded by

Aryan Shri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Data Structure Shorting Programs

The document contains code snippets for implementing various operations on linked lists including: 1. Inserting an element in a singly linked list by adding a node after a specified position. 2. Deleting an element from a singly linked list by searching for the element to delete. 3. Implementing operations like insertion, deletion on doubly linked lists including adding a node at the beginning or after a specified position. 4. Deleting the last node from a doubly linked list. 5. Searching for an element in a doubly linked list.

Uploaded by

Aryan Shri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

(ECS-353) Submitted to the

Prepared by Anurag Saxena


Roll No. 1171913006 (IIIrd Sem.2012-13) Under Guidance of Dr. Navneet Verma (H.O.D. B.Tech.,I.T.Deptt)

Binary search
#include <stdio.h> int binary_search(int array[ ],int value, int size) { int found = 0; int high = size, low = 0 mid; mid = ( high + low) / 2; printf(\n\nLooking for %d\n,value); while ((!found)&&(high>=low)) { printf(Low% dMid% High%d \n, low, mid, high); if(value == array[mid]) found = 1; else if ( value < array [mid]) high = mid 1; else low = mid 1; mid = (high + low) / 2; } return ((found)? Mid:-1); } void main ( void) { int array [100], i; for (I = 0; I < 100; i++) array[i] = I + 20; printf(Result of search %d\n, binary_search(array,33, 100)); printf(Result of search %d\n, binary_search(array,75, 100)); printf(Result of search %d\n, binary_search(array,1, 100)); printf(Result of search %d\n, binary_search(array,1001, 100)); }

Implement all operation on stack


#include <stdio.h> #include<conio.h> #define MAX 20 typedef struct n { int a [MAX]: int top: }stack; void initilize(stack*s) { s=top=-1 } int empty (stack*s) { if (s=top=-1) return(1) return(0) int full (stack*s) { if (s=msx-1) return(1) return(0) void push(stack*,intx) { s=top=stop+1 s=data=[stop+1]=x int pop(stack*s) { intx x=s=data[s=top] s=op=s=top-1 return(x); }

Insert an element in singly link list


#include <stdio.h> #include<conio.h> struct node { int info ; struct node*link; }*start; main() } int choice,n,m,position,I; start=null; while(1) { printf(1 create list ) printf(2 add at begining ) printf(3 add after) printf(4 quit) printf(enter your choice) scanf(%d,&choice) switch(choice) { case1: printf(how many node you want) scanf(%d,&n); for(i=0;I<n;i++) { printf(enter the element); scanf(%d,&m); create _list(m); }\break; case2: printf(enter the element); scanf(%d,&m); addbeg(m); break; case3: printf(enter the element); scanf(%d,&m); printf(enter the position after which this element is inserted );

scanf(%d,&position); addafter(m,position); break; case4: exit(); default: printf(enter wrong choice); create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) start=tmp; else { q=start; while(q=link!=null) q=q->link; } addbeg(int data) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=start; start=tmp; } add after (int data,int pos) { struct node *tmp,*q; int I; q=start; for(i=0;i<pos-1;i++) {

q=q->link; if(q==null) { printf(there are less than %d elements,pos); return; } tmp=malloc(sizeof(struct node)); tmp->link=q->link; tmp->info=data; q->link=tmp; }

Delete an element in singly link list


#include <stdio.h> #include<conio.h> struct node { int info ; struct node*link; }*start; main() } int choice,n,m,position,I; start=null; while(1) { printf(1 create list ) printf(2 delete ) printf(3 quit) printf(enter your choice) scanf(%d,&choice) switch(choice) { case1: printf(how many node you want) scanf(%d,&n); for(i=0;I<n;i++) { printf(enter the element); scanf(%d,&m); create _list(m); }\break; case2: if(start==null) { printf(list is empty) continue; } printf(enter the elementfor delete ); scanf(%d,&m); addbeg(m);

break; case4: exit(); default: printf(enter wrong choice); create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) start=tmp; else { q=start; while(q=link!=null) q=q->link; } if(q->link->info==data) { tmp=q->link; q->link=tmp->link; free() return; }

Insertation in double linked list #include<stdio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) {printf(1.CreateList\n); printf{2.Add at beginning\n); printf{3.Add after\n printf{4.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; case2: printf(enter the element); scanf(%d,&m); addbeg(m);

break; case3: printf(enter the element); scanf(%d,&m); printf(enter the position after which this element is inserted ); scanf(%d,&position); addafter(m,position); break; case4: exit(); default: printf(enter wrong choice); } } } create_list (int data) { struct node *q,*tmp; tmp=malloc(sizeof(struct node)); tmp=info=data; tmp=link=null; tmp=link=null; if(start==null) { tmp==prev=null; start=prev=tmp; start=tmp; }

else { q=start; while(q->next!=null) q->next=tmp; tmp->prev=q; } } addbeg(int num) tmp=malloc(sizeof(struct node));

tmp==prev=null; tmp==info=num; tmp==next=start; start=prev=tmp; start=tmp } add after(int num,int c) { struct node*tmp,*q; int i; for(i=0;I<n;i++) { q=q->next; if(q==null) { print(there are less than %d elements); } } tmp=malloc(sizeof(struct node)); tmp==info=num; q->nextprev=tmp; tmp=next=q=start; tmp=prev=q q=next=tmp; } return(); }

Delete an element in doubly link list #include<stdio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) { printf(1.CreateList\n); printf(2 delete); printf{3.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; case2: if(start==null) { printf(list is empty) continue; } printf(enter the elementfor delete ); scanf(%d,&m); addbeg(m);

break; case3: exit(); default: printf(enter wrong choice); printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); } break; void delete_end(node*h,node*t) } if(head==(node*)null) { printf(list is empty) getch(); } if(head==tail) { free(h); head=tail=(node*)null; return; } if(t->prev==h) { head->next=null; tail=head; } else tail=tail->prev; tail->next=null;}

Search an element in doubly link list #include<studio> #include<malloc.h> sturct node { struct node*prev; int info; struct node*next; }start; main() { int choice,n,m,po,I; start= NULL; while(1) { printf(1.CreateList\n); printf(2 search); printf{3.exit\n); { case 1: printf{How many nodes you want:); scanf(%d,&n); for(i=0;i<n;i++) { printf{Enter the element); scanf(%d,&m); create_list(m); break; } case2: printf(enter the element for search ); scanf(%d,&m); search(m); break;

case3: exit(); default: printf(enter wrong choice); void delete_end(node*h,node*t) } if(head==(node*)null) { printf(list is empty) getch(); } if(head==tail) { free(h); head=tail=(node*)null; return; } if(t->prev==h) { head->next=null; tail=head; } else tail=tail->prev; tail->next=null; }

You might also like