How to Be Famous in your Field just visit our Site
Presentation topic is stick data structure
1. PRESENTATION TOPIC
IS STICK DATA
STRUCTURE
PRESENTED BY: AIZAZ ALI
BSCS 3RD SEMESTER GPGC LANDIKOTAL
PRESENTED TO: SIR MR. MUHAMMAD NISAR SAIB
2. Stick definition
Stick operations , pop, push,
Example of stick
Implementation of stack as an array and
as linklist
References for study and objects
3. Stack definition :-
A stack is a linear data structure that can be accessed only at one of its ends for
storing and retrieving data. Such a stack resembles a stack of trays in a cafeteria:
new
trays are put on the top of the stack and taken off the top. The last tray put on
stack is the first tray removed from the stack.
Or
A stack is a list of homogenous elements in which the addition and deletion of
elements
occurs only at one end, called the top of the stack.
Or
A data structure in which the elements are added and removed from one end
5. FIGURE 1.1 An example of adding numbers 592 and 3,784 using stacks.
6. Now that you know what a stack is, let us see what kinds of operations can be
performed on a stack. Because new items can be added to the stack, we can
perform the add operation, called push, to add an element onto the stack.
Similarly, because the top item can be retrieved and/or removed from the stack,
we can perform the operation top to retrieve the top element of the stack, and
the operation pop to remove the top element from the stack.
The push, top, and pop operations work as follows: Suppose there are boxes
lying on the floor that need to be stacked on a table. Initially, all of the boxes
are on the floor and the stack is empty. (See Figure 7-2.)
7. First we push box A onto the stack. After the push operation, the stack is as shown in
Figure 7-3(a).
10. Array:-
The array implementation aims to create an array where the first element (usually at the zero-
offset) is the bottom.
That is, array[0] is the first element pushed onto the stack and the last element popped off. The
program must
keep track of the size, or the length of the stack. The stack itself can therefore be effectively
implemented as a
two-element structure in C:
typedef struct {
size_t size;
int items[STACKSIZE];
} STACK;
void push(STACK *ps, int x)
{
if (ps->size == STACKSIZE) {
fputs("Error: stack overflown", stderr);
abort();
} else
ps->items[ps->size++] = x;
}
int pop(STACK *ps)
{
if (ps->size == 0){
fputs("Error: stack underflown", stderr);
abort();
} else
return ps->items[ --ps->size];
}
11.
12. The linked-list implementation is equally simple and straightforward. In fact, a simple singly linked list is sufficient
to implement a stack—it only requires that the head node or element can be removed, or popped, and a node can
only be inserted by becoming the new head node.
Unlike the array implementation, our structure typedef corresponds not to the entire stack structure, but to a single
node:
typedef struct stack {
int data;
struct stack *next;
} STACK;
void push(STACK **head, int value)
{
STACK *node = malloc(sizeof(STACK)); /* create a new node */
if (node == NULL){
fputs("Error: no space available for noden", stderr);
abort();
} else { /* initialize node */
node->data = value;
node->next = empty(*head) ? NULL : *head; /* insert new head if
any */
*head = node;
}
}
13. int pop(STACK **head)
{
if (empty(*head)) { /* stack is empty */
fputs("Error: stack underflown", stderr);
abort();
} else { //pop a node
STACK *top = *head;
int value = top->data;
*head = top->next;
free(top);
return value;
}
}
Stack (ab