KEMBAR78
c programming and data structure notes for ECE | PPTX
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 /.
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.
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

c programming and data structure notes for ECE

  • 1.
    REPRESENTATION OF LISTS Createa list (N No.of data/elements) : eg: 10 20 30 40
  • 2.
  • 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 { intdata; 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(structnode)); 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 1010 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 102024 head To Print the List 20 3024 30 4024 40 NULL head head head head
  • 9.
    • A linkedlist 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 trackof 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 ona 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 tobe inserted X X A B C B C curr tmp
  • 13.
    Pseudo-code for insertion 13 typedefstruct 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 AB C C Item to be deleted curr tmp
  • 15.
    Pseudo-code for deletion 15 typedefstruct 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 LinkedLists • 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: UsingArray and Linked List 18
  • 19.
    A Last-in First-out(LIFO) List 19 In Out A B C C B Also called a STACK
  • 20.
  • 21.
  • 22.
    Stack: Linked ListStructure 22 top PUSH OPERATION
  • 23.
    Stack: Linked ListStructure 23 top POP OPERATION
  • 24.
    Basic Idea • Inthe 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 evaluatingAlgebraic 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 infixto 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 ofprecedence (highest to lowest)
  • 29.
    Convert infix topostfix :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 postfixexpression 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.
  • 34.
    A First-in First-out(FIFO) List 35 Also called a QUEUE In Out A C B A B
  • 35.
    Basic Idea • Basicidea: – 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 LISTSTRUCTURE 37 front rear ENQUEUE
  • 37.
    QUEUE: LINKED LISTSTRUCTURE 38 front rear DEQUEUE
  • 38.