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; }