Sie sind auf Seite 1von 27

1

Stack
2
!ntroduction
3
!ntroduction
+
!ntroduction
5
!ntroduction
6
!ntroduction
- There is no need to search or remove an arbitrary
node
- A stack is a type of LIFO {Last In First Out)
- The last element added is the first to removed
- !nsert a node immediately after the head (Push)
- Retrieve and delete the node immediately after the
head (Pop)
- Stack Top copies the item at the top of the stack
7
- The last element added is the first to be
removed
- Example - The stack of rice bowls in a
cupboard, the stack of books.
L!FO
8
Process
Example:
Push books
Red Book
Blue Book
Green Book
Yellow Book
Pop books
Yellow Book
Green Book
Blue Book
Red Book
9
Create Stack
size = 4
3
2
1
0
top = -1
Stack Operation
10
- Nake sure the stack is empty@not - before `pop'
!f `0' => cannot `pop'
3
2
1
0
top =-1
Stack Operation
11
- Nake sure the stack is empty@not - before `push'
!f stack is full => cannot `push'
3
2
1
0
top = 3
Stack Operation
12
- Push data into the stack
!f stack = full => `push' to add a node
3
2
1
0
-10
2
1 top 3
top
Stack Operation
13
- Pop data from the stack
!f stack = empty => `pop' to remove a node
3
2
1
0
top
3021 top -1
Stack Operation
Struct Declaration
typedef struct STACK
{
int top;
int list[3];
}stack;
3
2
1
0
top list
Stack Array !mplementation
top list

3
2
1
0
void create(stack *t)
{
t->top = -1;
}
Create Stack
int empty(stack *t)
{
if(t->top == -1)
return (1);
else
return(0);
}
int full(stack *t)
{
if (t->top ==3)
return (1);
else
return (0);
}
Overflow and Underflow Controller
void push(stack *t)
{
int data;
if (full(t) == 1)
printf("\nStack is Full\n");
else
{
printf("\nPush Data : ");
scanf("%d",&data);
t->top++;
t->list[t->top] = data;
}
}
top

3
2
1
0 10
Push Data
top
void pop(stack *t)
{
if(empty(t) == 1)
printf("\nStack is Empty\n");
else
t->top--;
}
-1
3
2
1
0 10
Pop Data
19

list
top
Create Stack
20

list
top
switch(1)
{
case 1 : push(&t);
void push(stack*t)
{
if (full(t) == 1)
printf("\nStack is Full");
else
{
printf("\nPush Data : ");
scanf("%d",&data);
t->top++;
t->list[t->top] = data;
}
}
int full(stack *t)
{
if (t->top == 3)
return (1);
else
return (0);
}
Push Data
21

list
top
switch(1)
{
case 1 : push(&t);
void push(stack*t)
{
if (full(t) == 1)
printf("\nStack is Full");
else
{
printf("\nPush Integer : ");
scanf("%d",&data);
t->top++;
t->list[t->top] = data;
}
}
int full(stack *t)
{
if (t->top == 3)
return (1);
else
return (0);
}
Push Data
22

list
top
switch(2)
{
case 2 : pop(&t);
void pop(stack *t)
{
if(empty(t) == 1)
printf("\nStack is Empty\n);
else
{
t->top--;
}
}
int empty(stack *t)
{
if(t->top == -1)
return (1);
else
return(0);
}
Pop Data
Struct Declaration
struct LLNode
{
int index;
int data;
struct LLNode *next;
};
Stack Linked List !mplementation
LLNode
data
next

Build a Stack
- The first key step is to create a head node
- Store their addresses in pointer variables head and tail
head =NULL;
head
'0'
if(*t == NULL)
Underflow Controller
void push(LLNode **t, int i)
{
int value;
LLNode *temp;
temp = (LLNode *)
malloc(sizeof (LLNode));
printf("\nPush Data ");
scanf("%d",&value);
temp->data=value;
temp->index=i;
temp->next = *t;
*t = temp;
}
Push Data
void pop(LLNode **t)
{
LLNode *temp;
if(*t == NULL)
printf("\nStack is Empty\n");
else
{
temp=*t;
*t=temp->next;
}
}
Pop Data
stack

Das könnte Ihnen auch gefallen