SlideShare a Scribd company logo
REPRESENTATION OF LISTS
Create a list (N No.of data/elements) :
eg: 10 20 30 40
MEMORY
STATIC
(STACK)
DYNAMIC
(HEAP)
RAM
ARRAY
• int a[N];
• for(int i=0; i<N;i++)
scanf(“%d”,&a[i]);
10 20 30 40
0 N
MEMORY
LINKED LIST
struct node
{
int data;
struct node *next;
}
Creation of list
struct node *d1,*d2,*d3,*d4;
d1
d2
d3
d4
DECLARATION STATIC MEMORY
d1=malloc(sizeof(struct node)); 1024
d2=malloc(sizeof(struct node)); 2024
d3=malloc(sizeof(struct node)); 3024
d4=malloc(sizeof(struct node)); 4024
d1
1024
d2
2024
d3
3024
d4
4024
1024
2024
3024
4024
data *next
Dynamic memory
d1
d2
d3
d4
d1 ->data = 10;
d2 ->data = 20;
d3 ->data = 30;
d4 ->data = 40;
d1 - >next=d2;
d2 - >next =d3;
d3 - >next=d4;
d4 ->next=NULL;
struct node *head=d1;
data *next
1024
10 2024
2024
20 3024
3024
30 4024
4024
40 NULL
head
LINKED LIST REPRESENTATION
10 10 10 10
head 1024
10 2024
2024
20 3024
3024
30 4024
4024
40 NULL
whike(head!=NULL)
{
printf(“%d”,head->data);
head=head ->next;
}
OUTPUT
10
20
30
40
data *next
1024
10 2024
head
To Print the List
20 3024
30 4024
40 NULL
head
head
head
head
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
– It does not waste memory space.
9
A B C
head
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
• Linked lists provide flexibility in allowing the
items to be rearranged efficiently.
– Insert an element.
– Delete an element.
10
Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one
11
Illustration: Insertion
12
A
A
Item to be
inserted
X
X
A B C
B C
curr
tmp
Pseudo-code for insertion
13
typedef struct nd {
struct item data;
struct nd * next;
} node;
void insert(node *curr)
{
node * tmp;
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Illustration: Deletion
14
A B
A B C
C
Item to be deleted
curr
tmp
Pseudo-code for deletion
15
typedef struct nd {
struct item data;
struct nd * next;
} node;
void delete(node *curr)
{
node * tmp;
tmp=curr->next;
curr->next=tmp->next;
free(tmp);
}
In essence ...
• For insertion:
– A record is created holding the new item.
– The next pointer of the new record is set to link it to
the item which is to follow it in the list.
– The next pointer of the item which is to precede it
must be modified to point to the new item.
• For deletion:
– The next pointer of the item immediately preceding
the one to be deleted is altered, and made to point
to the item following the deleted item.
16
Array versus Linked Lists
• Arrays are suitable for:
– Inserting/deleting an element at the end.
– Randomly accessing any element.
– Searching the list for a particular value.
• Linked lists are suitable for:
– Inserting an element.
– Deleting an element.
– Applications where sequential access is required.
– In situations where the number of elements cannot
be predicted beforehand.
17
Stack Implementations: Using Array
and Linked List
18
A Last-in First-out (LIFO) List
19
In Out
A
B
C C
B
Also called a
STACK
STACK USING ARRAY
20
top
top
PUSH
STACK USING ARRAY
21
top
top
POP
Stack: Linked List Structure
22
top
PUSH OPERATION
Stack: Linked List Structure
23
top
POP OPERATION
Basic Idea
• In the array implementation, we would:
– Declare an array of fixed size (which determines the maximum size of
the stack).
– Keep a variable which always points to the “top” of the stack.
• Contains the array index of the “top” element.
• In the linked list implementation, we would:
– Maintain the stack as a linked list.
– A pointer variable top points to the start of the list.
– The first element of the linked list is considered as the stack top.
25
Applications of stack:
1. Stack is used by compilers to check for balancing
of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into
postfix/prefix form.
4. In recursion, all intermediate arguments and
return values are stored on the processor’s stack.
5. During a function call the return address and
arguments are pushed onto a stack and on return
they are popped off.
Converting and evaluating Algebraic
expressions
• Infix: It is the form of an arithmetic expression in which we
fix (place) the arithmetic operator in between the two
operands.
Example: A + B
• Prefix: It is the form of an arithmetic notation in which we
fix (place) the arithmetic operator before (pre) its two
operands. The prefix notation is called as polish notation.
Example: + A B
• Postfix: It is the form of an arithmetic expression in which
we fix (place) the arithmetic operator after (post) its two
operands. The postfix notation is called as suffix notation
and is also referred to reverse polish notation.
Example: A B +
Conversion from infix to postfix:
Scan the infix expression from left to right.
• If the scanned symbol is left parenthesis, push it onto the stack.
• If the scanned symbol is an operand, then place directly in the
postfix expression (output).
• If the symbol scanned is a right parenthesis, then go on popping all
the items from the stack and place them in the postfix expression
till we get the matching left parenthesis.
• If the scanned symbol is an operator, then go on removing all the
operators from the stack and place them in the postfix expression,
if and only if the precedence of the operator which is on the top of
the stack is greater than (or equal) to the precedence of the
scanned operator and push the scanned operator onto the stack.
The order of precedence
(highest to lowest)
Convert infix to postfix :A + B * C – D / E * H
A
+
B
*
C
–
D
/
E
*
H
+ A
+ AB
+* AB
+* ABC
- ABC*+
-/ ABC*+D
-/ ABC*+DE
-* ABC*+DE/
-* ABC*+DE/H
A
ABC*+DE/H*-
- lower priority
than *, so POP
*.
+equal priority
with -, so POP +.
* equal priority
with /, so POP /.
c programming and data structure notes for ECE
Evaluation of postfix expression
The postfix expression is evaluated easily by the use of a
stack.
1. When a number is seen, it is pushed onto the stack;
2. When an operator is seen, the operator is applied to
the two numbers that are popped from the stack and
the result is pushed onto the stack.
3. When an expression is given in postfix notation, there
is no need to know any precedence rules; this is our
obvious advantage.
c programming and data structure notes for ECE
Queue Implementation using Linked
List
34
A First-in First-out (FIFO) List
35
Also called a QUEUE
In Out
A
C B
A
B
Basic Idea
• Basic idea:
– Create a linked list to which items would be added
to one end and deleted from the other end.
– Two pointers will be maintained:
• One pointing to the beginning of the list (point from
where elements will be deleted).
• Another pointing to the end of the list (point where
new elements will be inserted).
36
Front
Rear
DELETION INSERTION
QUEUE: LINKED LIST STRUCTURE
37
front rear
ENQUEUE
QUEUE: LINKED LIST STRUCTURE
38
front rear
DEQUEUE
Spring 2012

More Related Content

PPTX
DSA_Unit3_ Stacks and Queues using array (1).pptx
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PPTX
introduction of the Stacks and Queues.pptx
PDF
Data Structures And Algorithms(stacks queues)
PPT
cs201-list-stack-queue-linked-lists .ppt
PPT
cs201-list-stack-queue-linked-list (2).ppt
PPTX
STACK1.pptx
PPTX
Conversion of Infix to postfix.pptxjjjjj
DSA_Unit3_ Stacks and Queues using array (1).pptx
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
introduction of the Stacks and Queues.pptx
Data Structures And Algorithms(stacks queues)
cs201-list-stack-queue-linked-lists .ppt
cs201-list-stack-queue-linked-list (2).ppt
STACK1.pptx
Conversion of Infix to postfix.pptxjjjjj

Similar to c programming and data structure notes for ECE (20)

PPT
Exploring the Stack Stack operation.pptx
PDF
Data structure and algorithm.(dsa)
PPT
Data Structures and Discrete Mathematics
PDF
Stacks,queues,linked-list
PPT
Linked list-stack-queue Data Structure .ppt
PPTX
Stacks in c++
PPT
linkedlist (1).ppt
PPTX
13 Stacks and Queues.pptx
PPTX
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
PPTX
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
PPTX
CH4.pptx
PPTX
Stack_Overview_Implementation_WithVode.pptx
PPT
Data Structures by Maneesh Boddu
PPT
List Data Structure, Linked List, Stacks.ppt
PPTX
Stack implementation using linked list ppt
PPTX
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
PPT
lect- 3&4.ppt
PPT
Stack application
PPTX
Unit 3 Stacks and Queues.pptx
PPT
Rana Junaid Rasheed
Exploring the Stack Stack operation.pptx
Data structure and algorithm.(dsa)
Data Structures and Discrete Mathematics
Stacks,queues,linked-list
Linked list-stack-queue Data Structure .ppt
Stacks in c++
linkedlist (1).ppt
13 Stacks and Queues.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
UNIT II LINEAR DATA STRUCTURES – STACKS.pptx
CH4.pptx
Stack_Overview_Implementation_WithVode.pptx
Data Structures by Maneesh Boddu
List Data Structure, Linked List, Stacks.ppt
Stack implementation using linked list ppt
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
lect- 3&4.ppt
Stack application
Unit 3 Stacks and Queues.pptx
Rana Junaid Rasheed
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Mechanical Engineering MATERIALS Selection
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Project quality management in manufacturing
PPTX
Geodesy 1.pptx...............................................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
R24 SURVEYING LAB MANUAL for civil enggi
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
OOP with Java - Java Introduction (Basics)
Mechanical Engineering MATERIALS Selection
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Foundation to blockchain - A guide to Blockchain Tech
Project quality management in manufacturing
Geodesy 1.pptx...............................................
bas. eng. economics group 4 presentation 1.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
UNIT 4 Total Quality Management .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Ad

c programming and data structure notes for ECE

  • 1. REPRESENTATION OF LISTS Create a list (N No.of data/elements) : eg: 10 20 30 40
  • 3. ARRAY • int a[N]; • for(int i=0; i<N;i++) scanf(“%d”,&a[i]); 10 20 30 40 0 N MEMORY
  • 4. LINKED LIST struct node { int data; struct node *next; } Creation of list struct node *d1,*d2,*d3,*d4; d1 d2 d3 d4 DECLARATION STATIC MEMORY
  • 5. d1=malloc(sizeof(struct node)); 1024 d2=malloc(sizeof(struct node)); 2024 d3=malloc(sizeof(struct node)); 3024 d4=malloc(sizeof(struct node)); 4024 d1 1024 d2 2024 d3 3024 d4 4024 1024 2024 3024 4024 data *next Dynamic memory d1 d2 d3 d4
  • 6. d1 ->data = 10; d2 ->data = 20; d3 ->data = 30; d4 ->data = 40; d1 - >next=d2; d2 - >next =d3; d3 - >next=d4; d4 ->next=NULL; struct node *head=d1; data *next 1024 10 2024 2024 20 3024 3024 30 4024 4024 40 NULL head
  • 7. LINKED LIST REPRESENTATION 10 10 10 10 head 1024 10 2024 2024 20 3024 3024 30 4024 4024 40 NULL
  • 8. whike(head!=NULL) { printf(“%d”,head->data); head=head ->next; } OUTPUT 10 20 30 40 data *next 1024 10 2024 head To Print the List 20 3024 30 4024 40 NULL head head head head
  • 9. • A linked list is a data structure which can change during execution. – Successive elements are connected by pointers. – Last element points to NULL. – It can grow or shrink in size during execution of a program. – It can be made just as long as required. – It does not waste memory space. 9 A B C head
  • 10. • Keeping track of a linked list: – Must know the pointer to the first element of the list (called start, head, etc.). • Linked lists provide flexibility in allowing the items to be rearranged efficiently. – Insert an element. – Delete an element. 10
  • 11. Basic Operations on a List • Creating a list • Traversing the list • Inserting an item in the list • Deleting an item from the list • Concatenating two lists into one 11
  • 12. Illustration: Insertion 12 A A Item to be inserted X X A B C B C curr tmp
  • 13. Pseudo-code for insertion 13 typedef struct nd { struct item data; struct nd * next; } node; void insert(node *curr) { node * tmp; tmp=(node *) malloc(sizeof(node)); tmp->next=curr->next; curr->next=tmp; }
  • 14. Illustration: Deletion 14 A B A B C C Item to be deleted curr tmp
  • 15. Pseudo-code for deletion 15 typedef struct nd { struct item data; struct nd * next; } node; void delete(node *curr) { node * tmp; tmp=curr->next; curr->next=tmp->next; free(tmp); }
  • 16. In essence ... • For insertion: – A record is created holding the new item. – The next pointer of the new record is set to link it to the item which is to follow it in the list. – The next pointer of the item which is to precede it must be modified to point to the new item. • For deletion: – The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item. 16
  • 17. Array versus Linked Lists • Arrays are suitable for: – Inserting/deleting an element at the end. – Randomly accessing any element. – Searching the list for a particular value. • Linked lists are suitable for: – Inserting an element. – Deleting an element. – Applications where sequential access is required. – In situations where the number of elements cannot be predicted beforehand. 17
  • 18. Stack Implementations: Using Array and Linked List 18
  • 19. A Last-in First-out (LIFO) List 19 In Out A B C C B Also called a STACK
  • 22. Stack: Linked List Structure 22 top PUSH OPERATION
  • 23. Stack: Linked List Structure 23 top POP OPERATION
  • 24. Basic Idea • In the array implementation, we would: – Declare an array of fixed size (which determines the maximum size of the stack). – Keep a variable which always points to the “top” of the stack. • Contains the array index of the “top” element. • In the linked list implementation, we would: – Maintain the stack as a linked list. – A pointer variable top points to the start of the list. – The first element of the linked list is considered as the stack top. 25
  • 25. Applications of stack: 1. Stack is used by compilers to check for balancing of parentheses, brackets and braces. 2. Stack is used to evaluate a postfix expression. 3. Stack is used to convert an infix expression into postfix/prefix form. 4. In recursion, all intermediate arguments and return values are stored on the processor’s stack. 5. During a function call the return address and arguments are pushed onto a stack and on return they are popped off.
  • 26. Converting and evaluating Algebraic expressions • Infix: It is the form of an arithmetic expression in which we fix (place) the arithmetic operator in between the two operands. Example: A + B • Prefix: It is the form of an arithmetic notation in which we fix (place) the arithmetic operator before (pre) its two operands. The prefix notation is called as polish notation. Example: + A B • Postfix: It is the form of an arithmetic expression in which we fix (place) the arithmetic operator after (post) its two operands. The postfix notation is called as suffix notation and is also referred to reverse polish notation. Example: A B +
  • 27. Conversion from infix to postfix: Scan the infix expression from left to right. • If the scanned symbol is left parenthesis, push it onto the stack. • If the scanned symbol is an operand, then place directly in the postfix expression (output). • If the symbol scanned is a right parenthesis, then go on popping all the items from the stack and place them in the postfix expression till we get the matching left parenthesis. • If the scanned symbol is an operator, then go on removing all the operators from the stack and place them in the postfix expression, if and only if the precedence of the operator which is on the top of the stack is greater than (or equal) to the precedence of the scanned operator and push the scanned operator onto the stack.
  • 28. The order of precedence (highest to lowest)
  • 29. Convert infix to postfix :A + B * C – D / E * H A + B * C – D / E * H + A + AB +* AB +* ABC - ABC*+ -/ ABC*+D -/ ABC*+DE -* ABC*+DE/ -* ABC*+DE/H A ABC*+DE/H*- - lower priority than *, so POP *. +equal priority with -, so POP +. * equal priority with /, so POP /.
  • 31. Evaluation of postfix expression The postfix expression is evaluated easily by the use of a stack. 1. When a number is seen, it is pushed onto the stack; 2. When an operator is seen, the operator is applied to the two numbers that are popped from the stack and the result is pushed onto the stack. 3. When an expression is given in postfix notation, there is no need to know any precedence rules; this is our obvious advantage.
  • 33. Queue Implementation using Linked List 34
  • 34. A First-in First-out (FIFO) List 35 Also called a QUEUE In Out A C B A B
  • 35. Basic Idea • Basic idea: – Create a linked list to which items would be added to one end and deleted from the other end. – Two pointers will be maintained: • One pointing to the beginning of the list (point from where elements will be deleted). • Another pointing to the end of the list (point where new elements will be inserted). 36 Front Rear DELETION INSERTION
  • 36. QUEUE: LINKED LIST STRUCTURE 37 front rear ENQUEUE
  • 37. QUEUE: LINKED LIST STRUCTURE 38 front rear DEQUEUE