0% found this document useful (0 votes)
15 views21 pages

DSUsf 231110 202919

Uploaded by

Manthan Bhavsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views21 pages

DSUsf 231110 202919

Uploaded by

Manthan Bhavsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

A

Micro Project
Report on

“Implementation Of stack Using Linked List”.


In the fulfillment of the requirement for Diploma in

Computer Engineering.

Submitted by

MR.Abak Sahil Sunil


(2111710069)

Guided By

Prof. Ranode S.K.

DEPARTMENT OF COMPUTER
ENGINEERING
M.I.T POLYTECHNIC DHANORE,YEOLA
M.S.B.T.E MUMBAI
2022-23
MAHARASHTRA STATE BOARD OF TECHNICAL

EDUCATION,MUMBAI.

CERTIFICATE

This is to certify that Mr. Sahil Sunil Abak satisfactorily carried out the
investigation/experimentation and micro project work Entitled.
“Employee Record Maintaining Application”.

This work is being submitted for the award of diploma in Computer


Engineering. It is submitted in the partial fulfillment of the prescribed
Syllabus M.S.B.T.E Mumbai. For the academic year 2022-2023.

PROF. Ranode S.K PROF. Ghorpade M.S PROF.Gujrathi G.S


(Guided By) (H.O.D) (Principal)
Evalution Sheet For Micro Project: -

Academic Year:-2022-2023 Sub & Sub Code:-OOP(22316)

Course & Course Code:-CO3I Name of Guide:-Prof Ranode S.K.

Semester: - 3rd

Title Of Project:- “Implemintation of stack Using Linked List”.

Pos:-

1. Classes are easy to implement

2. Classes gives provision for hide data from end users

Cos:-

1. Classes are difficult to understand


2. Gets complicated when function names are similar

Roll Name Of Student Marks Out of ( )For Marks Out of #( Total Marks out
no performance in group )For performance in of( )
activity oral
presentation

19 Sahil Sunil Abak

Student signature Project Guide Signature


Weekly Progress Report

Sr Week Activity Performed Date signature


.no
1 1st Week Topic Discussion

2 2nd Week Topic Selection

3 3rd Week Collection of Data

4 4th Week Collection of Data

5 5th Week Analysis of Collected Data

6 6th Week Creating Report

7 7th Week Creating Report

8 8th Week Creating Report

9 9th Week Creating Report

10 10th Week Creating Report

11 11th Week Creating Report

12 12th Week Creating Report

13 13th Week Compilation of Report

14 14th Week Compilation of presentation

15 15th Week Presentation of seminar

16 16th Week Final Submission


Group Details:-

Roll No. Name of student Seat no Enrollment no

19 Abak Sahil Sunil 376674 2111710069

05 Birari Vishal Prakash 376660 2111710050

12 Pawar Gaurav Ambarsing 376667 2111710059

01 More Vaibhav Santosh 376656 2111710046

Guide Name: Prof. Ranode S.K.


Index

Sr.No Title Page No

1.
Introduction.

2.
Implementing a stack with a linked list:

3. Implementation

4.
Data Type Of Stack

5. Filling In Stack Operation

6. Program Source Code

7.
Output

8. Conclusion

9.
Reference
Introduction
• The linked list data structure is one of the fundamental data structures in
computer science.
• Think of the linked list data structure as your ABCs. Without learning the
ABCs, it is difficult to conceptualize words, which are made up by stringing
alphabetical characters together.
• Therefore, you want to know the ins and outs of the linked list data
structures.

• In this article, we will explore the linked list data structure’s key features
and operations. Afterwards, we will begin by implementing our own singly
linked list.
• Another separate post will be dedicated towards the doubly linked list,
which is a variant of the linked list data structure.
2. Implementing a stack with a linked list:
Using a linked list is one way to implement a stack so that it can handle
essentially any number of elements.

Here is what a linked list implementing a stack with 3 elements might look
like:

list
|
v
-------- -------- --------- |
C | -+-->| B | -+-->| A | 0 |
-------- -------- ---------

Where should we consider the top of the stack to


be, the beginning or end of the list.

3. implementation
Now, think about how to actually implement this stack of characters in C.

It is usually convenient to put a data structure in its own module, thus, we'll
want to create files stack.h and a stack.c.

The operations needed for our stack are mainly determined by the
operations provided by an abstract stack, namely:

StackPush()
StackPop() StackIsEmpty()
4. Data types for a stack
In order to determine what data types we'll need, let's think about how
someone will use our stack:

1. type-of-a-stack stack1, stack2;


2. StackPush(ref-to-stack1, 'A');
3. StackPush(ref-to-stack2, 'B');
4. ch = StackPop(ref-to-stack1);

First, stack variables must be defined (e.g., stack1 and stack2). Then, stack
operations may be called. Those operations will need to know which stack
to operate on.

Thus, our goal is to get some kind of type that we can use to keep track of
a stack.

Let's start bottom-up from the simplest type and work our way up through
types that use the simpler types...

First, we want to write a stack that is very generic. The fact that this stack
holds a character is only particular to this program. It should be easy to
change the type of things held in the stack.

Let's introduce a type name for the type of thing held in the stack:

typedef char stackElementT;


Now, elements of the stack are being stored in a linked list. Recall that
linked lists are made up of nodes that contain both an element and a pointer
to the next node.

typedef struct stackTag


{ stackElementT
element; struct stackTag
*next;
}
stackNodeT;

Finally, we need something that holds all the information needed to keep
track of the stack. Since elements will be held in nodes, we only need a
pointer to keep track of the beginning of the list (which will be our top of
the stack).

We suggest that this single pointer be put in a structure, so that we can give
it a descriptive field name and so that we can add more fields easily in the
future (if needed):

typedef struct
{ stackNodeT
*top; }
stackT;
5. Filling in stack operations
Now that we've decided on the data types for a stack, we think we'd like to
add 2 extra operations:

StackInit()
StackDestroy()

They are not part of the abstract concept of a stack, but they are necessary
for setup and cleanup when writing the stack in C.

Now, let's think about the StackInit() function. It will need to set up a stack
stackT structure, so that we have an empty stack.

What does the prototype for StackInit() look like? What do we do in its
body?

Now, what about putting an element in the stack. What should the
prototype for StackPush() look like?

The steps needed to push an element onto the stack are:

1. Allocate a new node.


2. Put the element in the node.
3. Link the element into the proper place in the list.

Let's fill in the body of StackPush().


Stack using Linked List

The major problem with the stack implemented using array is, it works only for
fixed number of data values. That means the amount of data must be specified at
the beginning of the implementation itself. Stack implemented using array is not
suitable, when we don't know the size of data which we are going to use. A stack
data structure can be implemented by using linked list data structure. The stack
implemented using linked list can work for unlimited number of values. That
means, stack implemented using linked list works for variable size of data. So,
there is no need to fix the size at the beginning of the implementation. The Stack
implemented using linked list can organize as many data values as we want.

In linked list implementation of a stack, every new element is inserted as 'top'


element. That means every newly inserted element is pointed by 'top'. Whenever
we want to remove an element from the stack, simply remove the node which is
pointed by 'top' by moving 'top' to its next node in the list.
The next field of the first element must be always NULL.

Example
Operations
To implement stack using linked list, we need to set the following things before
implementing actual operations.

Step 1: Include all the header files which are used in the program. And declare
all the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
Step 4: Implement the main method by displaying Menu with list of operations
and make suitable function calls in the main method.

Push(value) - Inserting an element into the Stack


We can use the following steps to insert a new node into the stack...

• Step 1: Create a newNode with given value.


• Step 2: Check whether stack is Empty (top == NULL)
• Step 3: If it is Empty, then set newNode → next = NULL.
• Step 4: If it is Not Empty, then set newNode → next = top.
• Step 5: Finally, set top = newNode.
Pop() - Deleting an Element from a Stack
We can use the following steps to delete a node from the stack...

• Step 1: Check whether stack is Empty (top == NULL).


• Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not
possible!!!" and terminate the function
• Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to
'top'.
• Step 4: Then set 'top = top → next'.
• Step 7: Finally, delete 'temp' (free(temp)).
Display() - Displaying stack of elements
We can use the following steps to display the elements (nodes) of a stack...

• Step 1: Check whether stack is Empty (top == NULL).


• Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the
function.
• Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize
with top.
• Step 4: Display 'temp → data --->' and move it to the next node. Repeat
the same until temp reaches to the first node in the stack (temp → next !=
NULL).
• Step 4: Finally! Display 'temp → data ---> NULL'.
6) Source Code
#include <stdio.h>

#include <stdlib.h> struct

node { int info; struct node

*ptr; }*top,*top1,*temp;

int topelement(); void

push(int data); void pop();

void empty(); void

display(); void destroy();

void stack_count(); void

create(); int count = 0; void

main() { int no, ch, e;

printf("\n 1 - Push");

printf("\n 2 - Pop");

printf("\n 3 - Top");

printf("\n 4 - Empty");

printf("\n 5 - Exit");

printf("\n 6 - Dipslay");

printf("\n 7 - Stack

Count"); printf("\n 8 -
Destroy stack"); create();

while (1) { printf("\n Enter

choice : "); scanf("%d",

&ch); switch (ch) { case 1:

printf("Enter data : ");

scanf("%d", &no); push(no);

break; case 2: pop(); break;

case

3:

if (top == NULL) printf("No elements

in stack"); else { e = topelement();

printf("\n Top element

: %d", e);

} break; case

4: empty();

break; case

5: exit(0);

case 6:

display();

break; case
7: stack_co

unt(); break;

case 8:

destroy() ;

break;

default :

printf("

Wrong

choice, Please enter correct choice

"); break;

} } void create()

top = NULL; }

void stack_count()

{ printf("\n No. of

elements in stack :

%d", count); }

void push(int

data)

{ if (top ==
NULL)

{ top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL; top->info = data;

} else { temp =(struct node

*)malloc(1*sizeof(struct node)); temp->ptr = top; temp->info = data; top = temp;

} count++; } void display()

{ top1 = top; if (top1

== NULL)

{ printf("Stack is empty");

return; } while

(top1 != NULL) {

printf("%d ",

top1>info); top1 =

top1>ptr; } } void pop()

{ top1 = top; if

(top1 == NULL)

{ printf("\n Error : Trying to pop from empty


stack"); return;

} else top1 = top1->ptr; printf("\n


Popped value : %d", top->info);
free(top); top = top1; count--; } int
topelement()
{ return(top->info);
} void empty() { if (top == NULL) printf("\n Stack is

empty"); else printf("\n Stack is not empty with

%d elements", count); } void destroy()

{ top1 = top; while

(top1 != NULL)

{ top1 = top->ptr; free(top); top = top1; top1

= top1->ptr; } free(top1); top = NULL;

printf("\n All stack elements destroyed");

count = 0;

}
Output
Conclusion
• In this lesson we learned how to traverse, append, prepend, insert and delete
nodes from a linked list. These operations are quite useful in many practical
applications.
• Suppose you are the programmer responsible for an application like
Microsoft PowerPoint. We can think of a PowerPoint presentation as a list,
whose nodes are the individual slides. In the process of creating and
managing slides we can use the concepts such as inserting and deleting
nodes learned in this lesson.

Reference
• http://btechsmartclass.com/downloads.html
• https://www.programmingsimplified.com/c/datastructures/cprogramimple
ment-linked-list
• https://www.hackerearth.com/practice/datastructures/linkedlist/singlylink
ed-list/tutorial/
• https://www.geeksforgeeks.org/linked-list-set-1-introduction/
https://www.geeksforgeeks.org/data-structures/linked-list/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy