You are on page 1of 63

DATA STRUCTURES

Structure :It is a heterogeneous collection of data


types sorted in contiguous memory locations referred by some name. Working with structures: Declaring structure prototype Creating structure variables Accessing and initializing members of structure

Declaring structure prototype


SYNTAX :

struct <structure name> { data type members; data type members


} EX :

struct student { int sno; char sname[10]; float fees; }

Creating structure variables:


SYNTAX: struct <structure name > variable1,variable2;

EX:

struct student s1,s2;


struct student { int sno; char sname[10]; float fees; }s1; struct student { int sno; char sname[10]; float fees; }; struct student s1,s2;

Accessing and initializing structure members By using dot( . ) operator.


Syntax: structure variable . Structure member EX: s1.sno; s1.sname; s1.fees; s1.sno=10;

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

Self referential Structures :


It is a structure that contains a pointer that points to same structure it self. EX : struct node { int data ; struct node *next: NODE };

NEXT

DATA

Classification of single linked list


1. Single linked list

2. 3.

Circular linked list Double linked list

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

Single Linked List :


Single linked list that contains one address field and one data field. Representation of single linked list : NODE 1 NODE 2 NODE 3 NULL Pointer Data

Null pointer is a pointer which points to nothing

Steps for creation of a node :


1.Read the element. 2.Allocate memory space for newly created node and place the element into the data field of new node and make the next field of new node to null. 3.If the list is empty then newly created node will become the first node otherwise next field of first node is given to newly created node

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

Difference between the Arrays and Linked Lists


1.Array is a homogenous collection of data elements stored in contiguous memory location referred by same name List is a collection of nodes .A node contains two parts , data and pointer which points to same structure. 2.Memory is allocated when the array is declared Memory is allocated for a node is done usually when a node is created. 3.Array size is fixed in size We can dynamically created any number of nodes. 4.All elements in the array stored in contiguous memory . Elements are stored whenever they find a free space. 5.Whenever a data entry is to be added or deleted data , need to be shifted. Easy to perform insert and deleted just by changing the pointer

Circular Linked Lists


It is a linear data structure in which next field of last node contains the address of first Node. Suppose we are at the last node of list ,if we want to move first node ,you need to move One step.

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

Representing the Doubly Linked List


DATA

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;

BASIC OPERATIONS ON STACK


1.PUSH 2.POP 3.ISFULL 4.ISEMPTY 1.PUSH :Inserting an element into the stack is called push. 2.POP :Deleting an element from the stack is called pop. 3.ISFULL :This function is used to check whether stack is empty or not. 4.ISEMPTY : This function is used to check whether top reaches its maximum size.

EXAMPLES ON STACK :

Pile of books Plate trays

Representation of STACK

PUSH 6 5 TOP 4 POP

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

IMPLIMENTATION OF STACKS BY USING ARRAYS


#include<iostream.h> #define size 10 int s[size]; int top=-1; int i; void push(); void pop(); void display(); void push() { int x; if(top==size-1) cout<<"overflow"<<endl; else { for(i=0;i<10;i++) { cout<<"enter the element"<<endl; cin>>x; top++; s[top]=x; } } }

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

Implementation of Stacks By using Linked Lists.


struct node { int data; struct node *next; }; class stack { struct node *top; public : stack() { top=NULL; } void push(int); int pop(); void display(); int isempty(); };

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 stack::isempty() { if(top==NULL) return 1; else return 0; }


void stack::display() { struct node *p; p=top; while(p!=NULL) { cout<<p->data<<endl; p=p->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; }

You might also like