Download presentation
Presentation is loading. Please wait.
Published byBlake Simmons Modified over 9 years ago
1
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg. email: praveenkumarjigajinni@yahoo.co.in
2
In computer science, a data structure is a particular way of organizing data in a computer so that it can be used Data structures provide a means to manage large amounts of data efficiently for uses such as large databases andinternet indexing services. Usually, efficient data structures are key to designing efficient algorithms. Some formal design methods and programming languages emphasize data structures, rather than algorithms, as the key organizing factor in software design. Storing and retrieving can be carried out on data stored in both main memory and in secondary memory. Data Structure
3
Data structures are generally based on the ability of a computer to fetch and store data at any place in its memory, specified by a pointer – a bit string, representing a memory address, that can be itself stored in memory and manipulated by the program. Thus, the array and record data structures are based on computing the addresses of data items with arithmetic operations; while the linked data structures are based on storing addresses of data items within the structure itself. Many data structures use both principles Data Structure
4
Primitive types Boolean, true or false Character Floating-point, single-precision real number values Double, a wider floating-point size Integer, integral or fixed-precision values Enumerated type, a small set of uniquely named values Composite types (Sometimes also referred to as Plain old data structures.) Array Record (also called tuple or struct or class) Union Tagged union (also called a variant, variant record, discriminated union, or disjoint union) Data Structure
5
Abstract data types Array Container Map/Associative array/Dictionary Multimap List Set Multiset/Bag Data Structure Priority queue Queue Deque Stack String Tree Graph
6
In computer science, a stack is a last in, first out (LIFO) data structure. A stack can is characterized by only two fundamental operations: push and pop. The push operation adds an item to the top of the stack. The pop operation removes an item from the top of the stack, and returns this value to the caller. A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also mean that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest. One of the common uses of stack is in function call Stack
8
Stack using ARRAY Stack using array #include const int size=5 class stack { int a[size]; //array a can store maximum 5 item of type int of the stack int top; //top will point to the last item pushed onto the stack public: stack(){top=-1;} //constructor to create an empty stack, top=-1 indicate that no item is //present in the array void push(int item) { If(top==size-1) cout<<”stack is full, given item cannot be added”;
9
cout<<”stack is full, given item cannot be added”; else a[++top]=item; //increment top by 1 then item at new position of the top in the array a } int pop() {If (top==-1) {out<<”Stack is empty “; return -1; //-1 indicates empty stack } else return a[top--];//return the item present at the top of the stack then decrement top by 1 } Stack using ARRAY
10
void main() { stack s1; s1.push(3); s1.push(5); cout<<s1.pop()<<endl; cout<<s1.pop(); } Stack using ARRAY
11
In Computer Science, a linked list (or more clearly, "singly- linked list") is a data structure that consists of a sequence of nodes each of which contains data and a pointer which points (i.e., a link) to the next node in the sequence. A linked list whose nodes contain two fields: an integer value and a link to the next node LINKED STACK
12
The main benefit of a linked list over a conventional array is that the list elements can easily be added or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk.Stack using linked lists allow insertion and removal of nodes only at the position where the pointer top is pointing to. LINKED STACK
13
struct NODE { int Book_No; char Book_Title[20]; NODE *Next; } *top, *newptr, *save; void PUSHBOOK(NODE*); // prototype void POPBOOK( ); // prototype void DISPLAY( ); // prototype LINKED STACK DEFINITION
14
void PUSHBOOK(NODE *np) { if(top==NULL) top=np; else {save=top; top=np; np->Next=save; } LINKED STACK – PUSH OPERATION
15
void POPBOOK( ) { if (top != NULL) { Stack *Temp; Temp = top; //Ignore the following line while evaluation cout Bno Bname<<”deleted”<<endl; top = top ->Next; delete Temp; } else cout<<”Stack Empty”; } LINKED STACK – POP OPERATION
16
void DISPLAY() { Node *Temp = Top; while (Temp! = NULL) { cout Bno Bname<<endl ; Temp = Temp -> Link; } LINKED STACK – DISPLAY OPERATION
17
Application of stacks in infix expression to postfix expression conversion Infix expr - operand1 operator operand2 for ex: a+b Postfix expr - operand1 operand2 operator for ex ab+ Prefix expr - operator operand1 operand2 for ex +ab APPLICATIONS OF STACK
18
Some example of infix expression and their corresponding postfix expression Infix expressionpostfix expression a*(b-c)/eabc-*e/ (a+b)*(c-d)/eab+cd-*e/ (a+b*c)/(d-e)+fabc*+de-/f+ APPLICATIONS OF STACK
19
For example convert the infix expression (A+B)*(C-D)/E into postfix expression showing stack status after every step. CONVERSION
20
Symbol scannedStack statusPostfix expr ( ((( A A +((+A B AB )( *(* ((*( C AB+C -(*(-AB+C D(*(-AB+CD )(*AB+CD- /(/AB+CD-* E(/AB+CD-*E )AB+CD-*E/ Answer : AB+CD-*E/
21
Evaluate the following postfix expression showing stack status after every step 8, 2, +, 5, 3, -, *, 4 / CONVERSION
22
scannedStack statusOperation performed 8 8Push 8 2 8, 2Push 2 + 10Op2=pop() i.e 2 Op1=pop() i.e 8 Push(op1+op2) i.e. 8+2 5 10, 5Push(5) 3 10, 5, 3Push(3) - 10, 2Op2=pop() i.e. 3 Op1=pop() i.e. 5 Push(op1-op2) i.e. 5-3 8, 2, +, 5, 3, -, *, 4 /
23
CONVERSION scannedStack statusOperation performed *20 Op2=pop() i.e. 2 Op1=pop() i.e. 10 Push(op1-op2) i.e. 10*2 4 20, 4Push 4 / 5Op2=pop() i.e. 4 Op1=pop() i.e. 20 Push(op1/op2) i.e. 20/4 NULLFinal result 5Pop 5 and return 5 8, 2, +, 5, 3, -, *, 4 / Answer : 5
24
Evaluate the following Boolean postfix expression showing stack status after every step. True, False, True, AND, OR, False, NOT, AND CONVERSION
25
scannedStack statusOperation performed True Push True FalseTrue, FalsePush False TrueTrue, False, TruePush True ANDTrue, FalseOp2=pop() i.e. True Op1=pop() i.e. False Push(Op2 AND Op1) i.e. False AND True=False ORTrueOp2=pop() i.e. False Op1=pop() i.e. True Push(Op2 OR Op1) i.e. True OR False=True True, False, True, AND, OR, False, NOT, AND
26
CONVERSION ScannedStack statusOperation Performed FalseTrue, FalsePush False NOTTrue, TrueOp1=pop() i.e. False Push(NOT False) i.e. NOT False=True ANDTrueOp2=pop() i.e. True Op1=pop() i.e. True Push(Op2 AND Op1) i.e. True AND True=True NULLFinal result TruePop True and Return True True, False, True, AND, OR, False, NOT, AND Answer :True
27
Queue is a linear data structure which follows First In First Out (FIFO) rule in which a new item is added at the rear end and deletion of item is from the front end of the queue. In a FIFO data structure, the first element added to the queue will be the first one to be removed. QUEUE
28
#include const int size=5; class queue {int front, rear; int a[size]; public: queue(){frot=0;rear=0;} //Constructor to create an empty queue void addQ() { if(rear==size) cout<<”queue is full<<endl; else a[rear++]=item; } QUEUE USING ARRAY
29
int delQ() {if(front==rear) {cout<<”queue is empty”<<endl; return 0;} else return a[front++]; } void main() { queue q1; q1.addQ(3); // CONTD… QUEUE USING ARRAY
30
q1.addQ(5) ; q1.addQ(7) ; cout<<q1.delQ()<<endl ; } Output is 3 5 7 Queue is empty QUEUE USING ARRAY
31
struct NODE { char Name[20]; NODE *Link; } *R,*F; void INSERT(); void DELETE(); LINKED QUEUE DEFINITION
32
void INSERT() { NODE *Temp; Temp=new NODE; gets(Temp->Name); Temp->Link=NULL; if (Rear==NULL) { Rear=Temp;Front=Temp; } else {Rear->Link=Temp; Rear=Temp; } LINKED QUEUE – PUSH OPERATION
33
void DELETE() { if(Front==NULL) // OR if(!Front) cout<<”\n Queue Underflow\n”; else { NODE *Temp; Temp=Front; Front=Front->Link; delete Temp; if (Front==NULL) Rear=NULL; } LINKED QUEUE – POP OPERATION
34
void DELETE() { if(Front==NULL) // OR if(!Front) cout<<”\n Queue Underflow\n”; else { NODE *Temp; Temp=Front; Front=Front->Link; delete Temp; if (Front==NULL) Rear=NULL; } LINKED QUEUE – DISPLAY OPERATION
35
Circular queues overcome the problem of unutilised space in linear queues implemented as array. In circular queue using array the rear and front moves in a circle from 0,1,2…size-1,0, and so on. #include const int size=4; class Cqueue { int a[size]; int front,rear,anyitem; public: void Cqueue(){front=0;rear=0;anyitem=0;} void addCQ(int item); int delCQ(); }; Circular Queue using Array
36
void Cqueue::addCQ(int item) { If(front==rear && anyitem>0) cout<<”Cqueue is full”<<endl; else { a[rear]=item; rear=(rear+1)%size; //rear will move in circular order anyitem++; //value of the anyitem contains no of items present in the queue } Circular Queue using Array
37
int Cqueue::delCQ() { if(front==rear&& anyitem==0) cout<<”Cqueue is empty”<<endl; return 0; //0 indicate that Cqueue is empty else { int r=a[front]; front=(front+1)/size; anyitem--; } Circular Queue using Array
38
void main() { Cqueue q1; q1.addCQ(3); q1.addCQ(5) ; cout<<q1.delCQ()<<endl ; q1.addCQ(7) ; cout<<q1.delCQ()<<endl ; q1.addCQ(8) ; q1.addCQ(9) ; cout<<q1.delCQ()<<endl; } Output is 3 5 7 8 Circular Queue using Array
39
? Any Questions Please
40
Thank You Very Much
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.