Professional Documents
Culture Documents
EX:
Data Structures: It is a particular way of storing and organizing data in computer memory .
Types of Data Structures :
Primitive
non-primitive
Primitive D.S :These are the data structures that can be manipulated directly by machine instructions. EX : integer ,float . Non primitive D.S :These can not be manipulated directly by machine instructions. Ex :Arrays..
Non primitive data structures are again classified into linear and non linear data structures. Linear D.S : These are the D.S in which all the elements form of a sequence.. EX : Arrays , linked lists
Non-Linear D.S : In this case the elements do not form any sequence. EX : trees ,graphs. Disadvantages of arrays : 1.Array size is fixed at compile time. 2.A data entry is to be added or removed from the array , data to be shifted to updated list
NEXT
DATA
2. 3.
List : A list is a collection of nodes. A node can contains the two parts ,first part contains data and second part contains pointer i.e) NODE
Data pointer
Write a program to create a number of nodes and print that nodes. OR Write a program to read the number of elements and print that elements using linked lists(single) /* Declaration of structure */ #include<iostream.h> struct node { int data; struct node *next; };
/*
Declaration of class
*/
class link { struct node *list; public : link() { list=NULL; } void create(); void display(); int count(); void insertAtbeg(); void insertAtend(); insertAtpos(); deleteAtbeg(); deleteAtend(); deleteAtpos(); void find(); };
void link::create() { int n,num,i; struct node *p,*q; cout<<"enter the number of nodes"; cin>>n; cout<<"enter the values"; for(i=0;i<n;i++) { cin>>num; p=(struct node *)malloc(sizeof(struct node)); //p=new node p->data=num; p->next=NULL; if(list==NULL) list=p; else q->next=p; q=p; } }
void link::display() { struct node *p; p=list; cout<<"the elements in the list are"; while(p!=NULL) { cout<<p->data<<endl; p=p->next; } }
int link::count() { struct node *p; int c=0; p=list; if(p==NULL) { cout<<"no elements is found"<<endl; return c; } else{ while(p!=NULL) { c++; p=p->next; } return c; } }
void link::insertAtbeg() { int n; struct node *p; cout<<"enter element to be inserted at Begin"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; p->next=list; list=p; }
void link::insertAtend() { int n; struct node *p,*q1,*q2; cout<<"enter the element to be inserted at the End"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; q1=list; while(q1!=NULL) { q2=q1; q1=q1->next; } q2->next=p; }
void link::insertAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the element to be inserted"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; cout<<"enter the position to be inserted"<<endl; cin>>k; q=list; for(i=1;i<k-1;i++) { q=q->next; } p->next=q->next; q->next=p; }
void link::deleteAtbeg() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl;
} else {
list=list->next; // list=p; }
}
void link::deleteAtend() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl; } else { while(p->next!=NULL) { q=p; p=p->next; } q->next=NULL; } }
void link::deleteAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the position to be deleted"<<endl; cin>>k; p=list; for(i=1;i<k-1;i++) { p=p->next; } q=p->next; p->next=q->next; free(q); }
void link::find() { struct node *p,*q; int ele, count=0; p=list; cout<<"entre the element to search"<<endl; cin>>ele; while(p!=NULL) { if(p->data==ele) count=count+1; p=p->next; } if(count==1) cout<<"element is found"<<endl; else cout<<"element is not found"<<endl; }
int main() { int c,ch; link l; do { cout<<"1.read \n 2.Display \n 3.count \n 4.InsertAtbegin \n 5.InsertAtend \n 6.InsertAtpos \n 7.deleteATbeg"<<endl; cout<<" 8.deleteAtend \n 9.deleteAtpos \n 10.find"<<endl; cout<<"Enter your choice"<<endl; cin>>ch; switch(ch) { case 1 :l.create(); break; case 2 :l.display(); break; case 3 :c=l.count(); cout<<"The number of elements in the list are"<<endl; cout<<c<<endl; break;
case 4 :l.insertAtbeg(); break; case 5 :l.insertAtend(); break; case 6 :l.insertAtpos(); break; case 7 :l.deleteAtbeg(); break; case 8 :l.deleteAtend(); break; case 9 :l.deleteAtpos(); break; case 10:l.find(); case 11 :exit(0); } }while(ch!=11); return 0; }
Write a program to create a number of nodes and print that nodes. OR Write a program to read the number of elements and print that elements using circular linked lists /* Declaration of structure */ #include<iostream.h> struct node { int data; struct node *next; };
class link { struct node *list; public : link() { list=NULL; } void create(); void display(); int count(); void insertAtbeg(); void insertAtend(); void insertAtpos(); void deleteAtbeg(); void deleteAtend(); void deleteAtpos(); void find(); };
void link::create() { int n,num,i; struct node *p,*q; cout<<"enter the number of nodes"; cin>>n; cout<<"enter the values"; for(i=0;i<n;i++) { cin>>num; p=(struct node *)malloc(sizeof(struct node)); //p=new node p->data=num; p->next=NULL; if(list==NULL) { list=p; p->next=list; } else { q->next=p; p->next=list; } q=p; } }
void link::display() { struct node *p; p=list; cout<<"the elements in the list are"<<endl; do { cout<<p->data<<endl; p=p->next; }while(p!=list); }
int link::count() { struct node *p; int c=0; p=list; if(p==NULL) { cout<<"no elements is found"<<endl; return c; } else{ do { c++; p=p->next; }while(p!=list); return c; } }
void link::insertAtbeg() { int n; struct node *p,*q; cout<<"enter element to be inserted at Begin"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; if(list==NULL) { list=p; p->next=list; } else { q=list; p->next=list; while(q->next!=list) { q=q->next; } q->next=p; list=p; } }
void link::insertAtend() { int n; struct node *p,*q; cout<<"enter element to be inserted at End"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; if(list==NULL) { list=p; p->next=list; } else { q=list; while(q->next!=list) { q=q->next; } q->next=p; p->next=list; } }
void link::insertAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the element to be inserted"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; cout<<"enter the position to be inserted"<<endl; cin>>k; q=list; for(i=1;i<k-1;i++) { q=q->next; } p->next=q->next; q->next=p; }
void link::deleteAtbeg() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl; } else { q=p; while(q->next!=list) q=q->next; list=list->next; q->next=list; // list=p; } }
void link::deleteAtend() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl; } else { while(p->next!=list) { q=p; p=p->next; } q->next=list; } }
void link::deleteAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the position to be deleted"<<endl; cin>>k; p=list; for(i=1;i<k-1;i++) { p=p->next; } q=p->next; p->next=q->next; free(q); }
void link::find() { struct node *p,*q; int ele, count=0; p=list; cout<<"entre the element to search"<<endl; cin>>ele; do { if(p->data==ele) count=count+1; p=p->next; }while(p!=list) ; if(count>=1) cout<<"element is found"<<endl; else cout<<"element is not found"<<endl; }
Doubly Linked Lists A Doubly Linked List can contains three fields, First field contains address of previous node Second field contains data and third field contains address of next node
Node
Previous
Next
Data
LIST
NULL 4 NULL 5
#include<iostream.h> struct node { int data; struct node *next; struct node *prev; }; class dlink { struct node *list; public : dlink() { list=NULL; } void create(); void display(); int count(); void insertAtbeg(); void insertAtend(); void insertAtpos(); void deleteAtbeg(); void deleteAtend(); void deleteAtpos(); void find(); };
void dlink::create() { int n,num,i; struct node *p,*q,*last; cout<<"enter the number of nodes"; cin>>n; cout<<"enter the values"; for(i=0;i<n;i++) { cin>>num; p=(struct node *)malloc(sizeof(struct node)); //p=new node p->data=num; p->next=NULL; p->prev=NULL; if(list==NULL) list=p; else { p->prev=q; q->next=p; } q=p; } }
void dlink::display() { struct node *p; p=list; cout<<"the elements in the list are"<<endl; while(p!=NULL) { cout<<p->data<<endl; p=p->next; } }
int dlink::count() { struct node *p; int c=0; p=list; if(p==NULL) { cout<<"no elemwnts is found"<<endl; return c; } else{ while(p!=NULL) { c++; p=p->next;
} return c;
} }
void dlink::insertAtbeg() { int n; struct node *p; cout<<"enter element to be inserted at Begin"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; p->prev=NULL; p->next=list; list->prev=p; list=p; }
void dlink::insertAtend() { int n; struct node *p,*q1,*q2; cout<<"enter the element to be inserted at the End"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; q1=list; while(q1!=NULL) { q2=q1; q1=q1->next; } q2->next=p; p->prev=q2; }
void dlink::insertAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the element to be inserted"<<endl; cin>>n; p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->next=NULL; cout<<"enter the position to be inserted"<<endl; cin>>k; q=list; for(i=1;i<k-1;i++) { q=q->next; } p->next=q->next; q->next->prev=q; q->next=p; p->prev=q; }
void dlink::deleteAtbeg() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl; } else { list=list->next; p->next=NULL; list->prev=NULL; list=p;
// }
}
void dlink::deleteAtend() { struct node *p,*q; p=list; if(p==NULL) { cout<<"There are no elements in the list"<<endl; } else { while(p->next!=NULL) { q=p; p=p->next; } q->next=NULL; p->prev=NULL; } }
void dlink::deleteAtpos() { int i,n,k; struct node *p,*q; cout<<"enter the position to be deleted"<<endl; cin>>k; p=list; for(i=1;i<k-1;i++) { p=p->next; } q=p->next; p->next=q->next; q->next->prev=q->prev; free(q); }
void dlink::find() { struct node *p,*q; int ele, count=0; p=list; cout<<"entre the element to search"<<endl; cin>>ele; while(p!=NULL) { if(p->data==ele) count=count+1; p=p->next; } if(count==1) cout<<"element is found"<<endl; else cout<<"element is not found"<<endl; }
Stacks
Stack is a linear D.S that contains list of elements in which insertion and deletions are done at one end of the stack. BASIC TERMINOLOGY : 1.TOP 2.OVERFLOW 3.UNDERFLOW 1.TOP : It keeps track of current position of the stack. 2.OVERFLOW :This condition occurs when we try to insert element into the stack when the stack reaches its maximum size. 3.UNDERFLOW : This condition occurs when we try to delete element from the stack when the stack is empty;
EXAMPLES ON STACK :
Representation of STACK
Characteristics of stacks
1.Stack is an ordered list with the restriction that insertion or deletions are done only at the top of the stack. 2.Whenever a new element is inserted older element moves down one position. 3.Stack is based on LIFO (Last In First Out) .
IMPLEMENTATION OF STACKS
1.By using ARRAYS. 2.By using Linked Lists
void pop() { int x; if(top==-1) cout<<"underflow"<<endl; else { x=s[top]; cout<<"delete element is"<<endl; cout<<x; top--; } }
void display() { int temp=top; if(top==-1) cout<<"no "<<endl; else { for(i=top;i>0;i--) cout<<s[i]; } }
int main() { int ch; while(1) { cout<<"1.push\n2.pop\n3.display\n4.exit"<<endl; cout<<"enter ur choiec"<<endl; switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); } }return 0; }
void stack::push( int a) { struct node *p; int i; p=new node; p->data=a; p->next=NULL; p->next=top; top=p; } int stack::pop() { struct node *p; if(isempty()) cout<<"stack is empty"<<endl; else { p=top; top=top->next; } }
int main() { stack s; int i,j,ch,a; cout<<"1.push\n2.pop\n3.display\n4.isempty\5.exit"<<endl; do { cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1:cout<<"entre an element into stack"<<endl; cin>>a; s.push(a); break; case 2:if(!s.isempty()) s.pop(); else cout<<"stack is empty"<<endl; break;
case 3:s.display(); break; case 4:j=s.isempty(); if(j==1) cout<<"stack is empty"<<endl; else cout<<"stack is not empty"<<endl; break; case 5:exit(0); } }while(ch<=5); return 0; }
Applications Of Stacks 1.Conversion of infix to postfix expression. 2.Conversion of infix to prefix expression. 3.Evaluation of postfix expression. 4.Paranthesis matching. 5.Recursion.
void stack::precedance(char t) { if(priority(s[top])>priority(t)) postfix[j++]=pop(); } int stack::priority(char t) { switch(t) { case '+' : case '-' :return 1 ;break; case '*' : case '/' :return 2 ;break; default :return 0; } }
void stack::push(char t) { if(top==max-1) cout<<"overflow"<<endl; else s[++top]=t; } char stack::pop() { char x; if (top==-1) cout<<"under flow"<<endl; else { x=s[top]; top--; }return x; }
void stack::infixexp() { char infix[30],t1,t; int i=0,j=0; cout<<"enter the infix expression"<<endl; cin>>infix; for(i=0;infix[i]!='\0';i++) { t=infix[i]; if(isalpha(t)||isdigit(t)) postfix[j++]=t; else { switch(t) { case '(' : push(t); break;
case '*' : case '/' :precedance(t); push(t); break; case ')' :t1=pop(); while(t1!='(') { postfix[j++]=t1; t1=pop(); } } break; } } while(top!=-1) { postfix[j++]=pop(); } postfix[j]='\0'; cout<<"postfix expression is"<<endl; cout<<postfix; }
void postfix:: push(int x) { top++; s[top]=x; } int postfix::pop() { int x; x=s[top]; top--; return x; }
void postfix::pp() { int I,a,b; char post[25],t; cout<<"enter the post fix exp"<<endl; cin>>post; for(i=0;post[i]!='\0';i++) { t=post[i]; if(isalpha(t)|| isdigit(t)) push(t); else { switch(t) { case '+' :b=pop(); a=pop(); a=a+b; push(a); break;
case '-':
b=pop(); a=pop(); a=a-b; push(a); break; b=pop(); a=pop(); a=a*b; push(a); break;
case '*':
case '/' :
} cout<<"result"<<endl; cout<<s[a]<<endl; }
Parenthesis matching
#include<iostream.h> #define size 10 void push(char); void pop(); int top=-1; char stack[size]; void push(char ch) { if(top==size-1) cout<<"overflow"<<endl; else { top++; stack[top]=ch; } } void pop() { char ch; ch=stack[top]; top--; //return ch; }
main() { int i; char str[10]; cout<<"enter the string"<<endl; cin>>str; for(i=0;str[i]!='\0';i++) { if(str[i]=='(' || str[i]=='[' || str[i]=='{') push(str[i]); else if(str[i]==')' || str[i]==']' || str[i]=='}') pop(); else { } } if(top==-1) cout<<"balance"<<endl; else cout<<"not balanced"<<endl; }
Queues
Queue is an ordered collection of elements from which elements may be added to one end and delete at other end. Queues follows the FIFO(First In First Out) BASIC TERMINOLOGY : 1.REAR 2.FRONT REAR : It is a variable that stores index number in the array at which new data will be added. FRONT : It is a variable that stores index number in the array at which the data will be deleted.
Example for how to insert an elements into queue (A,B,C,D,E)-Max.size=5 (Fix f=-1,r=-1) Insert A f=0,r=0 Insert D f=0,r=3 f
f Insert B f A A Insert C f
A
AAA
C AAA
r
B
f=0,r=1
Insert E
A B
r
f=0,r=4
C D AAA E
r f=0,r=2
B C AAA
Deleting an elements from the QUEUE Delete A f=0,r=4 Delete D aasBa C ac D E f r Delete B f=1,r=4 Delete E
C D E
f=3,r=4
E
r f=4,r=4
f r
Delete C
f
f=2,r=4
D E
IMPLIMENTATION OF QUEUES BY USING ARRAYS #include<iostream.h> //#define size 10 class queue { int s[10],size,front,rear; public: queue() { rear=-1; front=-1; cout<<"entre the size"<<endl; cin>>size; } void enqueue(); int dequeue(); void display(); int isfull(); int isempty(); };
void queue::enqueue() { int a; cout<<"enter the element"<<endl; cin>>a; if(isfull()) cout<<"queue is full"<<endl; else { rear++; s[rear]=a; } } int queue::dequeue() { int ele; if(isempty()) cout<<"queue is empty"<<endl; else { front++; ele=s[front]; return ele; }
int queue::isfull() { if(rear==size-1) return 1; else return 0; } int queue::isempty() { if(front==rear) return 1; else return 0; } void queue::display() { int i; cout<<"The elements in the Queue are"<<endl; for(i=front+1;i<=rear;i++) cout<<s[i]<<endl; }
int main() { queue q; int i,j,ch,a,ele; do { cout<<"1.enqueu \n2.dequeue \n3.display \n4.isfull \n5.isempty"<<endl; cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1 :q.enqueue(); break; case 2 :if(!q.isempty()) { ele=q.dequeue(); cout<<"element is "<<ele<<endl; } else { cout<<"queue empty"<<endl; } break;
case 3 :q.display();
break; case 4 : i=q.isfull(); if(i==1) cout<<"queue is full"<<endl; else cout<<"queue is not full"<<endl; break; case 5 : j=q.isempty(); if(j==1) cout<<"queue is empty"<<endl; else cout<<"queue is not emmpty"<<endl; break; case 6 : exit(0);
} }while(ch<=6); }
IMPLIMENTATION OF QUEUES BY USING LINKED LISTS #include<iostream.h> struct node { int data; struct node *next; }; class Queue { struct node *front,*rear; public: Queue() { front=NULL; rear=NULL; } void enqueue(int); int dequeue(); void display(); int isempty(); };
void Queue::enqueue(int a) { struct node *p; int i; p=new node; p->data=a; p->next=NULL; if(rear==NULL && front==NULL) front=p; else rear->next=p; rear=p; } int Queue::dequeue() { struct node *p; if(isempty()) cout<<"Queue is empty"<<endl; else { p=front; front=front->next; }
int Queue::isempty() { if(front==NULL) return 1; else return 0; } void Queue::display() { struct node *p; p=front; while(p!=NULL) { cout<<p->data<<endl; p=p->next; } }
int main() { Queue q; int i,j,ch,a; do { cout<<"1.Enqueue\n2.dequeue\n3.display\n4.isempty\n5.exit \n"<<endl; cout<<"enter yoyr choice"<<endl; cin>>ch; switch(ch) { case 1 : cout<<"enter element into the Queue"<<endl; cin>>a; q.enqueue(a); break; case 2 : if(!q.isempty()) q.dequeue(); break;
case 3 : q.display();
break; case 4 : j=q.isempty(); if(j==1) cout<<"Queue is empty<<endl; else cout<<"not empty"<<endl; break; case 5 : exit(0);
} }while(ch<=5); }
Applications Of Queues 1.In all the reservation counters where the people collect their tickets on FIRST COME FIRST SERVE basis. 2.Printer service routines are designed using queues 3.These are also used in Multi-programming environment. 4.Call center phone system will use a queue to hold people in line until a service representative is free.
CIRCULAR QUEUES
Circular queue is a type of queue in which last element comes just before the first element. Representation of circular Queue: . : .
.
N-1 0 1
In circular queue when rear=max-1 ,if we want to insert a new element instead of increasing rear pointer to maximum we preset it to zero. If front=max-1 when we want to delete an element from the queue we reset the pointer to zero. When there are no elements in the queue make the front and rear pointers to -1.
# include<iostream.h> #define MAXSIZE 50 struct que { int items[MAXSIZE]; int front, rear; }; typedef struct que *queue; queue makeempty(queue q) { int i; q=(queue)malloc(sizeof(struct que)); for(i-0;i<MAXSIZE;i++) q->items[i]=0; q->rear=-1; q->front=-1; return q; }
int enque(queue q) { int x; if((q->front==0&& q->rear==MAXSIZE-1)||(q->front=q->rear+1)) cout<<"Overflow"; if(q->rear==MAXSIZE-1) q->rear=0; if(q->rear==-1) q->rear=0; if(q->rear==-1) { q->rear=0; q->front=0; } else q->items[++q->rear]=x; return x; }
void dequeue(queue q) { int x; if(q->front==-1) cout<<"Underflow"; if(q->rear==q->front) { q->rear=-1; q->front=-1; } if(q->front==MAXSIZE-1) q->front=0; else { x=q->items[q->front]; q->front++; cout<<"Deleted element is "<<endl; cout<<x; } }
void display(queue q) { int i; if(q->rear==-1) cout<<"No ele"; if(q->rear<q->front) { for(i=0;i<q->rear;i++) cout<<q->items[i]; } else { for(i=q->front;i<=q->rear;i++) cout<<q->items[i]; } }
SEARCHING Searching is a process of finding the location of an element in the array . There are two searching techniques 1.Linear search 2.Binary search Linear search: In this method compare each element of array with the element to be searched. If the element is found then give some message. Binary Search : In this method all elements must be in sorted ordered ,then we are looking at the middle element(i.e low =starting element, high=n-1,mid=(low+high)/2).If it is middle element the element is found. Otherwise repeat the process until element is found.
HASHING Hashing is a technique used to perform insertion ,deletion and searching operations in constant access time .The ideal hash table data structure is simply an array of some fixed size containing key and we will refer size as table size. Each key is mapped into some number in the array index from 0 to table size and replace into appropriate cell .This mapping is called hashing. Hash function is define as H(x)=x mod table size i.e) x is an element to be inserted into hash table.
Example to inserting an elements into hash table Elements are( 10,11,12,13,15) table size is 5.
H(10)=10%5=0; insert 10 element into hash table at 0 index. H(11)=11%5=1 insert 10 element into hash table at 1 index. H(12)=12%5=2 insert 10 element into hash table at 2 index. H(13)=13%5=3 insert 10 element into hash table at 3 index. H(14)=14%5=4 insert 10 element into hash table at 4 index.
0 1
2
15
11 12
3 4
13
14
22 is inserted into the 2 cell ,but 2nd cell already has an element ,This situation is called collision. This is the disadvantage of hashing. To reduce the collisions following techniques are used. 1.Separate chaining 2.Open addressing Separate Chaining : In this technique hash table is implemented as an array of linked lists called chains.
Considered a hash table with the table size of 10 elements and the elements are(11,13,31,29,45,33,75,87,53,15)
0
1 2 3 4 5 6 7 8 9
45 11 75 31
13
33
53
45 87
29
int x; void ha::insert(hash h[10],int x) { hash temp,t; int i; i=hashfunction(x); temp=(hash)malloc(sizeof(hash)); //temp=new hash; temp->data=x; temp->next=NULL; if(h[i]==NULL) h[i]=temp; else { while(t->next!=NULL) t=t->next; t->next=temp; } t=temp; }
void ha:: makeempty(hash h[10]) { int i; for(i=0;i<tablesize;i++) h[i]=NULL; } void ha::display(hash h[10]) { hash temp; int i; for(i=0;i<tablesize;i++) { temp=h[i]; while(temp!=NULL) { cout<<temp->data; temp=temp->next; } } }
hash ha::find(hash l,int x) { while(l!=NULL) { if(l->data==x) break; l=l->next; } return l; } int ha::hashfunction(int x) { return(x%tablesize); }
void ha::delet(hash h[10],int x) { hash temp,prev; int i; i=hashfunction(x); if(h[i]->data==x) h[i]=h[i]->next; else temp=h[i]; while(temp!=NULL) { prev=temp; temp=temp->next; if(temp->data==x) {
prev->next=temp->next; break;
main() { ha hh; hash h[10],l; int ch,i; while(1) { ; cout<<"1.makeempty\n2.insert\n3.delete\n4.display\n5.find"<<endl cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1 :hh.makeempty(h); break; case 2 :cout<<"enter the elements to inserted"<<endl; cin>>x; hh.insert(h,x); break;
case 3 :cout<<"enter the element to be deleted"<<endl; cin>>x; hh.delet(h,x); break; case 4 :hh.display(h); break; case 5 :cout<<"enter element to found"<<endl; cin>>x; i=hh.hashfunction(x); l=hh.find(h[i],x); if(l==NULL) cout<<"element not found"<<endl; else cout<<"found"<<endl; break;
}
} }
Disadvantages of separate Chaining : 1.It requires more pointers. 2.It requires implementation of data structures. 3.It tends to slow down a bit ,because of time required to allocate new cells.
OPEN ADDRESSING : Open addressing is an alternative method to resolve collision. In this method collision occurs ,alternative cells are tried until an empty cell is found. When collision occurs during the insert operation ,this method sequentially searches for next available cells in the hash table . There are 3 techniques to solve collision 1.Linear probing 2.Quadratic probing 3.Double Hashing.
Linear probing: In this technique function f(i) is defined as f(i)=i. It indicates where ever we encounter collisions next available cell is searched and data elements are placed accordingly. When the collision is occurs the hash function is calculated as H(x)=(h(x)+f(i)) mod table size, i = 1 to table size-1 h(x)=x mod table size; If there are no collisions ,the hash function is h(x)=x mod table size.
Consider a hash table that contains 5 elements and insert the following elements(23,56,33,43,63). 1.Insert the element 23 h(23)=23%5 = 3. 2.Insert the element 56 h(56)= 56%5 = 1. 3.Insert the element 33 (1) h(33)=33%5=3 ,Here collision occurs Then H(33)=(3+1)%5=4. 4.Insert the element 43 (2) h(43)= 43%5 =3,Here collision occurs, Then H(43)=(3+1)%5=4,here collision occurs, Then H(43)=(3+2)%5=0
5.Insert the element 63 (4) h(63)=63%5 =3,here collision occurs, H(63) =(3+1)%5 = 4,again collision occurs, H(63) =(3+2)%5 =0,again collision occurs, H(63) =(3+3)%5=1,again collision occurs, H(63) = (3+3)%5=2. 0 43
1 2 3 4 56 63 23 33
void makeempty(int h[10],int n) { int i; for(i=0;i<n;i++) h[i]=0; } int hashfunction(int x,int n) { return (x%n); }
void insert(int h[10], int n,int x) { int i,k,l; i=hashfunction(x,n); if(h[i]==0) h[i]=x; else { for(k=0;k<n;k++) { l=(i+k)%n; if(h[l]==0) { h[l]==x; break; } } } }
void delet(int h[10],int n,int x) { int i,k,l; i=hashfunction(x,n); if(h[i]==x) h[i]=-1; else { for(k=1;k<n;k++) { l=(i+k)%n; if(h[i]==x) h[l]=-1; break; } } }
void search(int h[10],int n,int x) { int i,c=0; for(i=0;i<n;i++) if(h[i]==x) { c++; break; } if(c>0) cout<<"found"<<endl; else cout<<"not found"<<endl; } void display(int h[10],int n) { int i; cout<<"elements are"<<endl; for(i=0;i<n;i++) cout<<h[i]; }
main() { int ch,x, h[10],n,i; cout<<"enter the hash table size"<<endl; cin>>n; while(1) { cout<<"1.makeempty\n2.insert\n3.delete\n4.display\n5.find"<<endl; cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1 :makeempty(h,n); break; case 2 :cout<<"enter the elements to inserted"<<endl; for(i=0;i<n;i++) { cin>>x; insert(h,n,x); } break;
case 3 :cout<<"enter the element to be deleted"<<endl; cin>>x; delet(h,n,x); break; case 4 :display(h,n); break; case 5 :cout<<"enter element to found"<<endl; cin>>x; search(h,n,x); break; } } }
INSERTION SORTINGS
30 50 20 70 40 10 80 60 20 30 50 70 40 10 80 60 (do nothing)
20 30 50 70 40 10 80 60
20 30 40 50 70 10 80 60 10 10 20 30 40 50 70 80 60 20 30 40 50 70 80 60 (Do nothing)
10
20
30 40 50 60 70 80
Procedure for INSERTION sort : Step 1: Take the second index value into temp variable i.e) temp=30 Step 2: Take the first index value and compare with the temp value(50>30) if it is true place the first index value into the second index value and the check all left side values of temp variables where the condition is fails place the temp value at that place. Step 3: If the condition is fails(i.e 50>70) do nothing and no need to check for left side elements because all left side elements less than the temp element Step 4:Take the next index value(i.e 20) then GOTO step 2 untile all elements completed in the given list. Step 5: Get the sorted elements
1. INSERTION SORT
class Isorting { int a[10],n; public: void read(); void sort(); void display(); }; void Isorting::read() { int i; cout<<"enter the array size"<<endl; cin>>n; cout<<"enter the elements"<<endl; for(i=0;i<n;i++) cin>>a[i]; }
void Isorting::display() { int i; cout<<"The elemente are "<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; } void Isorting::sort() { int i,j; int temp; for(i=1;i<n;i++) { temp=a[i]; for(j=i;j>0 && a[j-1]>temp;j--) a[j]=a[j-1]; a[j]=temp; } }
cout<<"\n1.read\n2.display\n3.Insertion sorting"<<endl; cout<<"enter your choice"<<endl; cin>>ch; switch(ch) { case 1 : I.read( ); break; case 2 : I.display(); break; case 3 : I.sort(); break; case 4 : exit(0);
} }while(ch!=5); }
2.HEAP SORT
class hs { int a[10],n; public: //void read(int [],int); void display(int [],int); void heapsort(int [],int); void buildheap(int [],int); void heap(int [],int,int); }; void hs::display(int a[10],int n) { int i; cout<<"after sorting the elements are"<<endl; for(i=1;i<=n;i++) cout<<a[i]; }
void hs:: heapsort(int a[10],int n) { int i,t; buildheap(a,n); for(i=n;i>=2;i--) { t=a[i]; a[i]=a[1]; a[1]=t; heap(a,1,i-1); } } void hs:: buildheap(int a[10],int n) { int i; for(i=n/2;i>=1;i--) heap(a,i,n); }
void hs ::heap(int a[10],int i,int n) { int j,item; j=2*i; item=a[i]; while(j<=n) { if(j<n && a[j]<a[j+1]) j++; if(item>=a[j]) break; a[j/2]=a[j]; j=2*j; } a[j/2]=item; }
int main() { hs h; int a[10],n,i; cout<<"enter the n value"<<endl; cin>>n; cout<<"entre the elements into array"<<endl; for(i=1;i<=n;i++) cin>>a[i]; h.heapsort(a,n); h.display(a,n); }
3.SHELL SORT
#include<iostream.h>
class ss { int a[10],n; public: void shellsort(int a[10],int n); void display(int a[10],int n); }; int main() { ss s; int a[10],n,i; cout<<"Enter n value: "; cin>>n; cout<<"Enter array elements: "; for(i=0;i<n;i++) cin>>a[i]; s.shellsort(a,n); s.display(a,n); return 0; }
void ss::display(int a[10],int n) { int i; cout<<"After Sorting "; for(i=0;i<n;i++) cout<<a[i]<<" "; }
void ss::shellsort(int a[10], int n) { int i,j, inc; int temp; for(inc=n/2;inc>0;inc=inc/2) { for(i=0;i<n;i++) { temp=a[i]; for(j=i;j>=inc;j-=inc) { if(temp<a[j-inc]) a[j]=a[j-inc]; else break; a[j-inc]=temp; } } } }
4.QUICK SORT #include<iostream.h> class qs { int a[10], n; public: void sort(int [],int, int); int partition(int [], int, int); void display(int [],int); };
main() {
qs s; int a[10],n; int i,l,h; cout<<"enter the array size"<<endl; cin>>n; cout<<"enter the elements"<<endl; for(i=0;i<n;i++) cin>>a[i]; l=0,h=n-1; s.sort(a,l,h); s.display(a,n);
} void qs::display(int a[10],int n) { int i; cout<<"The elemente are "<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; }
int qs::partition(int a[10], int l, int h) { int i,j,pivot,t; i=l+1; j=h; pivot=a[l]; while(i<=j) { while(a[i]<pivot&&i<=h) i++; while(a[j]>pivot) j--; if(i<j) { t=a[i]; a[i]=a[j]; a[j]=t; } } a[l]=a[j]; a[j]=pivot; return j; }
5.RADIX SORT
#include<iostream.h> main() { int a[10],i,j,k,l,m,n,lar=0; int n1[10],a1[10][10],mod,div=1,temp; for(i=0;i<10;i++) { } cout<<"enter the size of array:"; cin>>n; cout<<"Enter the elements now\n"; for(i=0;i<n;i++) n1[i]=0; { cin>>a[i]; if(a[i]>lar) lar=a[i]; }
mod=1; for(i=0;lar!=0;i++) { mod=mod*10; for(j=0;j<n;j++) { temp=(a[j]%mod)/div; a1[temp][n1[temp]++]=a[j]; } div=div*10; l=0; for(k=0;k<10;k++) { for(m=1;m<=n1[k];m++) { a[l]=a1[k][m-1] ; l++; } n1[k]=0; } lar=lar/10; } cout<<"\nThe sorted array is:"; for(i=0;i<n;i++) { cout<<a[i]<<" "; } }
Redblack tree
Properties: A node is either red or black. The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice-versa, this rule has little effect on analysis.) All leaves (NIL) are black. (All leaves are same color as the root.) Both children of every red node are black. Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
Example :
SPLAY TREE In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete operations are like in binary search trees, except at the end of each operation a special step called splaying is done. Splaying ensures that all operations take O(lg n) amortized time. First, a quick review of BST operations
In splay trees, after performing an ordinary BST Search, Insert, or Delete, a splay operation is performed on some node x (as described later). The splay operation moves x to the root of the tree. The splay operation consists of sub-operations called zig-zig, zig-zag, and zig.
Zig-Zig
z 10 x has a grandparent x
30
y 20
y 20
x 30 z 10
T1 T2 T3
T4 T3
T4
T1
T2
Zig-Zag
z 10 y 30 T1 x 20 T4 T2 T3 T1 T2 T3 T4 z 10 y 30 x has a grandparent x
20
Zig
x has no grandparent (so, y is the root) y 10 Note: w could be NIL x
20 x 20
w 30 y 10 w 30
T1 T2 T3
T4
T1
T2
T3
T4
Result of splaying
The result is a binary tree, with the left subtree having all keys less than the root, and the right subtree having keys greater than the root. Also, the final tree is more balanced than the original. However, if an operation near the root is done, the tree can become less balanced.
Complete Example
44 Splay(78) 17 32 zig-zag 28 54 82 65 50 88 97
29
z 76
78
x
80 y
44 Splay(78) 17 32 zig-zag 28 54 82 y z 65 50 88 97
29
76
78 x
80
44 Splay(78) 17 32 zig-zag 28 z 65 y 82 76 80 x 78 50 88 97
29
54
44 Splay(78) 17 32 zig-zag 28 65 x 78 50
z
88 y 97 82 76 80
29
54
44 Splay(78) 17 32 zig-zag 28 54 50 z 78
x 88 y 82 97
65
80 76
29
y Splay(78) 17 32 zig 28
44 78 50 82 x 88 w 97
65
80 54 76
29
78 x y 44 Splay(78) 17 32 zig 28 54 50 82 88 w 97
65
80 76
29
BST
#include<iostream.h> #include<stdlib.h> struct btree { int data; struct btree *l; struct btree *r; }; typedef struct btree *bst;
bst insert(bst,int); void preorder(bst); void inorder(bst); void postorder(bst); bst findmin(bst); bst findmax(bst); bst search(bst,int); bst creat(bst); bst delet(bst);
bst creat(bst t) { int i,n,val; cout<<"enter the n value"<<endl; cin>>n; cout<<"enter the values"<<endl; for(i=0;i<n;i++) { cin>>val; t=insert(t,val); } return t; } bst insert(bst t,int val) { bst temp; if(t==NULL) {
void preorder(bst t) { if(t!= NULL) { cout<<" "<<t->data<<" "; preorder(t->l); preorder(t->r); } } void inorder(bst t) { if(t!= NULL) { inorder(t->l); cout<<" "<<t->data<<" "; inorder(t->r); } }
void postorder(bst t) { if(t!= NULL) { preorder(t->l); preorder(t->r); cout<<" "<<t->data<<" "; } } bst findmin(bst t) { if(t==NULL) return NULL; else if(t->l==NULL) return t; else return(findmin(t->l)); }
bst findmax(bst t) { if(t==NULL) return NULL; else if(t->r==NULL) return t; else return(findmax(t->r)); } bst search(bst t,int x) { if(t==NULL) return NULL; else if(t->data==x) return t; else if (x<t->data) return (search(t->l,x)); else return (search(t->r,x)); }
bst delet(bst t,int x) { bst y,temp; if(t==NULL) return NULL; else if(x<t->data) t->l=delet(t->l,x); else if(x>t->data) t->r=delet(t->r,x); else { if((t->l!=NULL) && (t->r!=NULL)) { y=findmin(t->r); t->data=y->data; t->r=delet(t->r,t->data); } else { temp=t; if(t->l==NULL) t=t->r; else t=t->l; free(temp); } } return t; }
int main() { bst t1,t2,t3,t=NULL; int ch,x; do { cout<<endl<<endl; cout<<" Binary Search Tree Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Creation "<<endl; cout<<" 2. Find minimum"<<endl; cout<<" 3. Find maximum"<<endl; cout<<" 4. In-Order Traversal "<<endl; cout<<" 5. Pre-Order Traversal "<<endl; cout<<" 6. Post-Order Traversal "<<endl; cout<<" 7. Search "<<endl; cout<<" 8. Delete"<<endl; cout<<" 9. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch;
switch(ch) { case 1 : t=creat(t); cout<<endl; break; case 2: t1=findmin(t); cout<<"The minimam element is "<<endl; cout<<t1->data; break; case 3: t2=findmax(t); cout<<"The maximamum element is "<<endl; cout<<t2->data; break; case 4: cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; inorder(t); break; case 5: cout<<" Pre-Order Traversal "<<endl; cout<<" -------------------"<<endl; preorder(t); break;
case 6 : cout<<" Post-Order Traversal "<<endl; cout<<" --------------------"<<endl; postorder(t); break; case 7 : cout<<"Enter the element to search"<<endl; cin>>x; t3=search(t,x); if(t3!=NULL) cout<<"element is found"<<endl; else cout<<"element is not found"<<endl; break; case 8 : cout<<"Enter the node to be deleted"<<endl; cin>>x; t=delet(t,x); inorder(t); break; case 9 : exit(0); } }while(ch!=11); }
void inorder(node t) { if(t!=NULL) { inorder(t->l); cout<<t->data <<endl; inorder(t->r); } } void preorder(node t) { if(t!=NULL) { cout<<t->data <<endl; preorder(t->l); preorder(t->r); } }
void create(node p) { node temp; char x; cout<<"Enter the data into left child of" <<endl; cout<<p->data <<endl; cin>>x; if(x!='n') { temp=(node)malloc(sizeof(node)); temp->data=x; temp->l=temp->r=NULL; p->l=temp; create(p->l); }
cout<<"Enter the data into right child of "<<endl; cout<<p->data <<endl; cin>>x; if(x!='n') { temp=(node)malloc(sizeof(node)); temp->data=x; temp->l=temp->r=NULL; p->r=temp; create(p->r); } }
int main() { char c; //clrscr(); cout<<"Enter data into root node" <<endl; cin>>c; root=(node)malloc(sizeof(node)); root->data=c; root->l=root->r=NULL; create(root); cout<<"The inorder expression is:" <<endl; inorder(root); cout<<"The preorder expression is:" <<endl; preorder(root); cout<<"The postorder expression is:" <<endl; postorder(root); getch(); }