Back to Browse

Data Structures using C Part 2 - Introduction stacks | What is a stack? Why stack data structure?

23.8K views
May 25, 2017
15:27

Introduction stacks data structure in C : Introduction stacks | What is a stack? Why stack data structure? ADT - Abstract Data Types : You have already familiar with Basic data types like integers, arrays, strings and so on. To access such data, you have used operations defined in the programming language for the data type. This approach doesn’t always work on large and complex programs in the real world which deal with large amount of data. ADTs are also called as custom data types which define : how the data values are stored. the possible operations that can be carried out on the stored data. It hides the detailed implementation of the data type and provides an interface to manipulate them. For example : stacks, queues, linked lists, graphs, trees etc… Introduction to Stacks Stack is a special type of data structure where elements are inserted from one end and elements are deleted from the same end. The position from where elements are inserted and from where elements are deleted is termed as top of the stack. Thus stack is a homogeneous collection of elements of any one type, arranged linearly with access at one end only. The two basic operations associated with stacks are : Push Pop Stack Program : void push(); void pop(); void display(); int top= -1; // value of top is initialised to -1 int stack[SIZE]; void main() { int choice; while(1) { printf("\n1.Push\n"); printf("\n2.Pop\n"); printf("\n3.Display\n"); printf("\n4.Quit\n"); printf("\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default:printf("Invalid Choice\n"); } } } void push() { int item; if(top==(SIZE-1)) printf("\nStack Overflow"); else { printf("\nEnter the item to be pushed in stack:"); scanf("%d",&item); top=top+1; stack[top]=item; } } void pop() { if(top==-1) printf("Stack Underflow\n"); else { printf("\nPopped element is : %d\n",stack[top]); top=top-1; } } void display() { int i; if(top== -1) printf("\nStack is empty\n"); else { printf("\nStack elements:\n"); for(i=top;igreaterthan=0;i--) printf("%d\n",stack[i]); } } Applications of Stack ankpro ankpro training Asp.net MVC C# C sharp Bangalore Rajajinagar Selenium Coded UI Mobile automation testing Mobile testing JQuery JavaScript .Net C C++ Components of the .Net framework Hello World Literal Keywords Variable Data types Operators Branching Loops Arrays Strings Structures Enums Functions

Download

0 formats

No download links available.

Data Structures using C Part 2 - Introduction stacks | What is a stack? Why stack data structure? | NatokHD