0% found this document useful (0 votes)
18 views

CS DataStructure-Lecture 2-Linked Stack

Data strructure

Uploaded by

AsmaaGhoniem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CS DataStructure-Lecture 2-Linked Stack

Data strructure

Uploaded by

AsmaaGhoniem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structure

Dr. Ahmed Hesham Mostafa


Lecture 2 –Linked Stack
TextBooks
• s k chang, “data structures and algorithms”
• Kruse and Leung, “Data Structures & Program Design in C”
Online Martials
• CS214: Data Structures by Prof. Dr Waleed A. Yousef
• https://www.youtube.com/playlist?list=PLoK2Lr1miEm-
5zCzKE8siQezj9rvQlnca
• Data Structures Learning Course by Dr Mohammed El-Said
• https://www.youtube.com/playlist?list=PLfay0LLBd0wiNeOR_SGoYfC
3w-NxFwd0D
• Lectures Source code (updated frequently)
• https://github.com/ahmedheshamostafa/DataStructure
Linked Stacks (to overcome fixed size limitations):
Just get the idea now, do not worry about details.
s.top  ps->top ps->top
ps Node
s
Empty stack Stack of size 1

pn

New
node

Old top Old second Old bottom


node node node
ps->top

How to insert a node


© Waleed A. Yousef 2008 4
ps -> top

Old top Old second Old bottom


node node node

pn How to delete a node

© Waleed A. Yousef 2008 5


Type Definition

Old top Old second Old bottom


node node node
s.top
s
typedef struct stacknode{ 1. To make logical distinction
StackEntry entry; between the stack itself
struct stacknode *next; and its top, which points to
}StackNode; a node.

typedef struct stack{ 2. To be consistent with the


StackNode *top; definitions of other DS.
}Stack;
3. For upgradability (adding
Why not: more functions) that may
Typedef StackNode *Stack; need other pieces of
information to be saved
than top. (we will see).
© Waleed A. Yousef 2008 6
Implementation level User Level
(what really happens) (interface)
void CreateStack(Stack *ps){ void main(){
ps->top=NULL;
} Stack s;

NULL is defined in <stdlib.h> CreateStack(&s);

}
CreateStack
&s The execution time does not
ps depend on anything, therefore
the complexity is: Θ(1)

s.top
s
© Waleed A. Yousef 2008 7
/* Pre: The stack exists and is initialized.
Post: The argument item has been stored at the top of the stack
*/

void Push(StackEntry e, Stack *ps){


StackNode *pn=(StackNode*)malloc(sizeof(StackNode));
pn->entry=e;
pn->next=ps->top; User Call:
ps->top=pn; Stack s;
} e
StackEntry e;
Complexity is: Θ(1)
Push(e, &s);

Push entry
e

e pn
&s Old top Old bottom
ps node node
© Waleed A. Yousef 2008 s 8
Always take care of special cases
/* Pre: The stack exists and is initialized
Post: The argument item has been stored at the top of the stack */

void Push(StackEntry e, Stack *ps){


StackNode *pn=(StackNode*)malloc(sizeof(StackNode));
pn->entry=e;
pn->next=ps->top;
ps->top=pn;
}

Push entry
e

e pn
&s
ps
© Waleed A. Yousef 2008 s 9
/*Pre: The stack exists and it is not empty.
Post: The item at the top of the stack has been removed and returned
in *pe */

void Pop(StackEntry *pe, Stack *ps){ User Call:


StackNode *pn; Stack s;
This would be
*pe=ps->top->entry; the only code for StackEntry e;
pn=ps->top; StackTop.
ps->top=ps->top->next;
Pop(&e, &s);
free(pn);
} 1 st element

Complexity is: Θ(1) e

Pop 1st node

&e
pe pn
&s 2nd top Old bottom
ps node node
© Waleed A. Yousef 2008 s 10
Always take care of special cases
/*Pre: The stack exists and it is not empty.
Post: The item at the top of the stack has been
removed and returned in *pe */
User Call:
void Pop(StackEntry *pe, Stack *ps){ Stack s;
StackNode *pn; StackEntry e;
*pe=ps->top->entry;
pn=ps->top; Pop(&e, &s);
ps->top=ps->top->next;
free(pn); 1 st element
} e

Pop 1st node

&e
pe pn
&s
ps
© Waleed A. Yousef 2008 s 11
/* Pre: The stack exists
Post: returns the status, 1 or 0*/

int StackEmpty(Stack *ps){ For any function that does not change
return ps->top==NULL; the stack there is no problem in passing
} the stack itself s rather than a pointer
to it ps. This will not copy the elements
int StackFull(Stack *ps){ as opposed to the array-based
return 0;
implementation. However, of course,
}
we do not do that to keep the code at
the user level unchanged.
Complexity is: Θ(1)

Old top Old bottom


node node
&s s
ps
© Waleed A. Yousef 2008 12
/* Pre: The stack exists You can replace qn by ps->top
Post: All the elements freed
*/
void ClearStack(Stack *ps){ void ClearStack(Stack *ps){
StackNode *pn=ps->top; StackNode *pn=ps->top;
StackNode *qn=ps->top;
while(pn){ while(pn){
The same as:
pn=pn->next; pn!=NULL pn=pn->next;
free(qn); free(ps->top);
qn=pn; ps->top=pn;
} }
ps->top=NULL;
} }
The wrong code is:
Complexity is: Θ(N):
ps->top=NULL; Total time = N * time of one loop
Always take care of special cases:
s
pn Old bottom
&s Old top
node node
ps s
© Waleed A. Yousef 2008
qn 13
/* Pre: The stack exists
Post: Function is passed to process every element*/

void TraverseStack(Stack *ps, void(*pf)(StackEntry)){


StackNode *pn=ps->top;
while(pn){
(*pf)(pn->entry);
pn=pn->next;
}
}
Complexity is: Θ(N).
Same efficient code but more compact is:
for(StackNode *pn=ps->top; pn; pn=pn->next)
(*pf)(pn->entry);

pf
pn Old bottom
Old top
&s node node
ps s
© Waleed A. Yousef 2008 14
/* Pre: The stack exists
Post: returns the number of elements*/

int StackSize(Stack *ps){ Comments: This function is Θ(N),


int x;
i.e., the execution time is
StackNode *pn=ps->top;
proportional to the size! Because,
for(x=0; pn; pn=pn->next)
x++; we have to traverse the stack node-
return x; by-node to count. (this statement is
} not precise).
Can we modify the structure to
decrease the complexity of this
algorithm?
Always take care of special cases:
s
pn
&s Old top Old bottom
node node
ps s
© Waleed A. Yousef 2008 15
We add extra field, called, size in struct stack. Then, we need to add
just one statement to: CreateStack, Pop, Push, ClearStack.

typedef struct stack{


StackNode *top;
int size;
}Stack;

void CreateStack(Stack *ps){


ps->top=NULL;
ps->size=0;
}

void Push(StackEntry e, Stack *ps){


StackNode *pn=(StackNode*)malloc(sizeof(StackNode));
pn->entry=e;
pn->next=ps->top;
ps->top=pn;
ps->size++;
}

© Waleed A. Yousef 2008 16


void Pop(StackEntry *pe, Stack *ps){
StackNode *pn;
*pe=ps->top->entry;
pn=ps->top;
ps->top=ps->top->next; What happened is that, we added
free(pn); extra field in the data structure,
ps->size--;
} which is size (2 bytes). But, this
void ClearStack(Stack *ps){
saves us time by reducing the
StackNode *pn=ps->top; complexity of the algorithm of the
while(pn){
pn=pn->next;
function StackSize. From Θ(N) to
free(ps->top); Θ(1).
ps->top=pn;
} This is a typical trade-off between
ps->size=0; time and space
}

Then the function StackSize is simply:

int StackSize(Stack *ps){


return ps->size;
}
© Waleed A. Yousef 2008 17
Comparison between the array-based and the linked
implementation: “Which is always better?” is a wrong question!
Array-based implementation Linked implementation

typedef struct stack{ typedef struct stacknode{


int top; StackEntry entry;
StackEntry entry[MAXSTACK]; struct stacknode *next;
} Stack; }StackNode;

typedef struct stack{


StackNode *top;
int size;
}Stack;

-All the space is reserved even the stack is -Extra field next for every new node.
empty (wasting memory)

-Stack gets full even if the memory is not! -The stack size is as large as the memory you have!

© Waleed A. Yousef 2008 18


Comparison between the array-based and the linked
implementation: “Which is always better?” is a wrong question!
Array-based Linked
implementation implementation
Pop Θ(1) Θ(1)
Push Θ(1) Θ(1)
CreateStack Θ(1) Θ(1)
StackSize Θ(1) Θ(1)
TraverseSack Θ(N) Θ(N)
ClearStack Θ(1) Θ(N)

Then, there are advantages and disadvantages for every implementation. Which one is better really depends on
the application.E.g.,

If ClearStack is extensively used then, may be array-based implementation is better.

If the memory is very limited, then may be the linked implementation is better.

The rule is: Know very well the pros and cons of each implementation and decide based on your application needs.
© Waleed A. Yousef 2008 19
References

Thanks

You might also like