Uploaded by salma25102001

lec 5

advertisement
El-Shorouk Academy
The Higher Institute of Engineering, El-Shorouk City
Communications and Computer Engineering Department
Data Structure
CCE 411(2013)
CCE 332(2019)
Dr. Ahmed El-Shafei
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
Data Structure
‫هياكل بيانات‬
Fourth Lecture
Sunday 22/10/2023
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
1
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
❑ In infix notation, the operators have precedence. That is, we must evaluate
expressions from left to right, and multiplication and division have higher
precedence than addition and subtraction. If we want to evaluate the
expression in a different order, we must include parentheses.
✓ For example, in the expression a + b * c, we first evaluate * using the
operands b and c, and then we evaluate + using the operand a and the
result of b * c.
❑ In the early 1920s, the Polish mathematician Jan Lukasiewicz discovered
that if operators were written before the operands (prefix or Polish notation;
for example, + a b), the parentheses can be omitted.
❑ In the late 1950s, the Australian philosopher and early computer scientist Charles L.
Hamblin proposed a scheme in which the operators follow the operands (postfix
operators), resulting in the Reverse Polish notation. This has the Advantage that
the operators appear in the order required for computation.
❑ postfix notation had important applications in computer science. (*)
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
6
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
❑ The following example shows various infix expressions and their
equivalent postfix expressions.
❑Many compilers use stacks to first translate infix expressions
into some form of postfix notation and then translate this postfix
expression into machine code.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
7
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
❑ Postfix expressions can be evaluated using the following algorithm:
➢ Scan the expression from left to right. When an operator is found, back
up to get the required number of operands, perform the operation, and
continue.
Consider the following postfix expression: 6 3 + 2 * =
Let us evaluate this expression using a stack and the previous
algorithm. Following Figure shows how this expression gets evaluated.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
8
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
Read the first symbol, 6, which is a number. Push the number onto the stack;
see Figure (a). Read the next symbol, 3, which is a number. Push the number
onto the stack; see Figure (b). Read the next symbol, +, which an operator.
Because an operator requires two operands to be evaluated, pop the stack twice;
see Figure (c). Perform the operation and push the result back onto the stack;
see Figure (d). Read the next symbol, 2, which is a number. Push the number
onto the stack; see Figure (e). Read the next symbol, *, which is an operator.
Because an operator requires two operands to be evaluated, pop the stack twice;
see Figure (f). Perform the operation and push the result back onto the stack;
see Figure (g). Scan the next symbol, =, which is the equal sign, indicating the
end of the expression. Therefore, print the result. The result of the expression is
in the stack, so pop and print; see Figure (h). The value of the expression 6 3 + 2
* = 18
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
9
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
when we read a symbol other than a number, the following cases arise:
1. The symbol we read is one of the following: +, -, *, /, or =
a. If the symbol is +, -, *, or /, the symbol is an operator and so we must
evaluate it. Because an operator requires two operands, the stack must
have at least two elements; otherwise, the expression has an error.
b. If the symbol is = (an equal sign), the expression ends, and we must
print the answer. At this step, the stack must contain exactly one
element; otherwise, the expression has an error.
2. The symbol we read is something other than +, -, *, /, or =
In this case, the expression contains an illegal operator.
It is also clear that when an operand (number) is encountered in an
expression, it is pushed onto the stack because the operator comes after the
operands. Data Structure - Fourth Year, Computer and Control Engineering- First Term
10
1 November 2023
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
Infix to Postfix Conversion Using Stack
❑ One of the applications of Stack is in the conversion of arithmetic
expressions in high-level programming languages into machine readable
form.
❑ Infix to Postfix conversion is one of the most important applications of stack.
❑ As our computer system can only understand and work on a binary
language, it assumes that an arithmetic operation can take place in two
operands only e.g., A+B, C*D, D/A … etc.
❑ But in our usual form an arithmetic expression may consist of more than
one operator and two operands e.g. (A+B)*C(D/(J+D))
❑ These complex arithmetic operations can be converted into polish notation
using stacks which then can be executed in two operands and an operator
form.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
11
Data Structure
‫هياكل بيانات‬
Arithmetic Expression: (Continued)
Algorithm to convert Infix to Postfix
Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the
same precedence as or higher precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left
parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
1 November 2023
7. END.
Data Structure - Fourth Year, Computer and Control Engineering- First Term
12
Data Structure ‫هياكل بيانات‬
Arithmetic Expression: (Continued)
Let’s take an example to better understand the algorithm.
Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.
Resultant Postfix Expression: ABC*DEF^/G*-H*+
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
13
Data Structure ‫هياكل بيانات‬
Arithmetic Expression: (Continued)
❑ Advantage of Postfix Expression over Infix Expression
✓ An infix expression is difficult for the machine to know and
keep track of precedence of operators.
✓ On the other hand, a postfix expression itself determines the
precedence of operators (as the placement of operators in a
postfix expression depends upon its precedence).
✓ Therefore, for the machine it is easier to carry out a postfix
expression than an infix expression.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
14
Data Structure ‫هياكل بيانات‬
Arithmetic Expression: (Continued)
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
15
Data Structure ‫هياكل بيانات‬
LINKED LISTS
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
15
Data Structure ‫هياكل بيانات‬
LINKED LISTS
❑
❑
❑
❑
❑
❑
Learn about linked lists
Become aware of the basic properties of linked lists
Explore the insertion and deletion operations on linked lists
Discover how to build and manipulate a linked list
Learn how to construct a doubly linked list
Discover how to use the STL (Standard Template Library) container list
(Sections)
❑ Learn about linked lists with header and trailer nodes
❑ Become aware of circular linked lists
➢ A Complete Overview Of (STL):
✓ Standard Template Library (STL) of C++ is a collection of template
classes that provide data structures such as arrays, vectors, queue, etc.
✓ STL is a library consisting of containers, algorithms, and iterators.
✓ As STL consists of a collection of template classes, it’s a generalized
library that is independent of data types.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
16
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ We studied how Data is organized and processed sequentially using an array, called a
sequential list. Several operations can be performed on sequential lists, such as
sorting, inserting, deleting, and searching.
➢ Lists are the most used non-primitive data structures
➢ If data is not sorted, searching for an item in the list can be very time consuming,
especially with large lists. Once the data is sorted, binary search can be used and
improve the search algorithm.
➢ However, in this case, insertion and deletion become time consuming, especially with
large lists because these operations require data movement.
➢ Also, because the array size must be fixed during execution, new items can be added
only if there is room.
➢ In the following slides using pointers to organize and process data in lists, called linked
lists.
➢ When data is stored in an array, memory for the components of the array is
contiguous—that is, the blocks are allocated one after the other.
➢ However, the components (called nodes) of a linked list need not be contiguous.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
17
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ A linked list is a collection of components, called nodes. Every node (except the last node)
contains the address of the next node.
➢ Every node in a linked list has two components: one to store the relevant information (that
is, data) and one to store the address, called the link, of the next node in the list.
➢ The address of the first node in the list is stored in a separate location, called the head or
first.
➢ The order of the nodes is determined by the address, called the link, stored in each node.
➢ The arrow in each node indicates that the address of the node to which it is pointing is
stored in that node. The down arrow in the last node indicates that this link field is NULL.
➢ suppose that the first node is at memory location 1200, and the second node is at memory
location 1575,
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
18
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❑ Because each node of a linked list has two components, we need to declare each node
as a class or struct.
❑ The data type of each node depends on the specific application—that is, what kind of
data is being processed.
❑ The link component of each node is a pointer. The data type of this pointer variable is
the node type itself.
❑ For the previous linked list, the definition of the node is as follows. (Suppose that the
data type is int.)
struct nodeType
{
int info;
nodeType *link;
};
The variable declaration is as follows:
nodeType *head;
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
19
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❑ Some Properties Linked Lists:
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
20
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
✓ Suppose that current is a pointer of the same type as the pointer head. Then the
statement
current = head;
copies the value of head into current.
✓ Now consider the following statement:
current = current ->link;
This statement copies the value of current->link, which is 2800, into current.
After this statement executes, current points to the second node in the list.
(When working with linked lists, we typically use these types of statements to advance a
pointer to the next node in the list.)
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
21
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❑ Type of Linked Lists:
➢ Single linked list
➢ Doubly linked list
➢ Single circular linked list
➢ Doubly circular linked list
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
22
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ Single Linked List:
❑ A Single linked list contains two fields in each node – an information field and the
linked field.
❑ The information field contains the data of that node.
❑ The link field contains the memory address of the next node.
❑ There is only one link field in each node, the linked list is called singly linked list.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
23
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ Single circular Linked List:
▪ The link field of the last node contains the memory address of the first node, such a
linked list is called circular linked list.
▪ In a circular linked list, every node is accessible from a given node.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
24
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ Doubly Linked List:
❑ It is a linked list in which each node points both to the next node and to the
previous node.
❑ In doubly linked list each node contains three parts:
✓ FORW: It is a pointer field that contains the address of the next node.
✓ BACK : It is a pointer field that contains the address of the previous node.
✓ INFO : It contains the actual data.
❑ In the first node, if BACK contains NULL, it indicated that it is the first node
in the list.
❑ The node in which FORW contains, NULL indicates that the node is the last node.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
25
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
➢ Doubly circular Linked List:
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
26
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❑
Operation on Linked Lists
The operation that are performed on linked lists are:
➢ Creating a linked list.
➢ Traversing a linked list.
➢ Inserting an item into a linked list.
➢ Deleting an item from the linked list.
➢ Searching an item in the linked list.
➢ Merging two or more linked lists.
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
27
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖Creating a linked list.
❑ The nodes of a linked list can be created by the following structure
declaration.
struct Node
{
int info;
struct Node *link;
}*nodel, node2;
❑ Here info is the information field and link is the link field.
❑ The link field contains a pointer variable that refers the same node
structure. Such a reference is called as Self addressing pointer.
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
28
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖Operator new and delete.
▪ Operators new allocate memory space.
▪ Operators new [ ] allocates memory space for array.
▪ Operators delete deallocate memory space.
▪ Operators delete [ ] deallocate memory space for array.
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
29
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖Traversing a linked list.
❑ Traversing is the process of accessing each node of the linked list exactly
once to perform some operation.
❑ Traversing a linked list given a pointer to the first node of the list, we must
step through the nodes of the list.
❑ we always want head to point to the first node. If it is used in traversing,
we will lose the list.
❑ So, head must point to the first node. It follows that traversing the list
using another pointer of the same type. Suppose that current is a pointer
of the same type as head.
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
30
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Traversing a linked list . (continued)
ALGORITHM: TRAVERS (START, P)
START contains the address of the first node. Another pointer P is
temporarily used to visit all the nodes from the beginning to the end of the
linked list.
Step 1: P= START
Step 2: while P != NULL
Step 3:
PROCESS data (P) [Fetch the data]
Step 4:
P= link(P)
[Advance P to next node]
Step 5: End of while
Step 6: Return
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
31
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node into the linked list
❑ Inserting a node at the beginning of the linked list.
❑ Inserting a node at the given position.
❑ Inserting a node at the end of the linked list.
1 November
2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
32
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at the beginning of the linked list.
1.
2.
3.
4.
5.
Create a node .
Fill data into the data field of the new node .
Mark its pointer field as NULL .
Attach this newly created node to START .
Make the new node as the START node.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
33
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at the beginning of the linked list.
ALGORITHM: INS_BEG (START, P)
START contains the address of the first node.
Step 1: P  new Node;
Step 2 :data(P)  num;
Step 3: link(P)  START;
Step 4: START  P;
Step 5: Return
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
34
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at the end of the linked list.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
35
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at the end of the linked list.
ALGORITHM: INS_END (START, P)
START contains the address of the first node.
Step 1: START
Step 2: P  START
[identify the last node]
While P!= null
P  next (P)
End while
Step 3: N  new Node;
Step 4: data(N)  item;
Step 5: link(N)  null
Step 6: link(P)  N
Step 7: Return
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
36
Data Structure
‫هياكل بيانات‬
Fifth Lecture
Sunday 29/10/2023
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
1
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at a any position:
➢ The Linked List is sorted
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
37
Algorithm: INSERT(ITEM)
[This algorithm adds newnodes at any position (Beginning, in Middle and at End) in the List]
1. Create a NewNode node in memory
2. Set NewNode -> INFO =ITEM. [Copies new data into INFO of new node.]
3. Set NewNode -> NEXT = NULL. [Copies NULL in NEXT of new node.]
4. If HEAD=NULL, then HEAD=NewNode and return. [Add first node in list]
5. if NewNode-> INFO < HEAD->INFO
then Set NewNode->NEXT=HEAD and HEAD=NewNode and return
[Add node at beginning of existing list]
6. PrevNode = NULL, CurrNode=NULL;
7. for(CurrNode =HEAD; CurrNode != NULL; CurrNode = CurrNode ->NEXT)
{ if(NewNode->INFO <= CurrNode ->INFO)
{
break the loop
}
PrevNode = CurrNode;
} [ end of loop ]
[Insert after PREV node (in middle or at end) of the list]
8. Set NewNode->NEXT = PrevNode->NEXT and
9. Set PrevNode->NEXT= NewNode.
10.Exit.
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at a given position:
➢ The Linked List unsorted
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
37
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Inserting a node at a given position:
➢ Linked List unsorted
ALGORITHM: INS_POS (START, P, POS)
START contains the address of the first node.
Step 1: START
else if (POS<=Count)
Step 2: P  START [Initialize node]
P  START
count  0
For(i=1;i <= pos; i++)
Step 3: while P!= null
P  next(P);
count  count +l
end for
P  next (P)
[create] N  new node
end while
data(N)  item;
Step 4: if (POS=1)
link(N)  link(P)
Call function INS_ BEG( )
link(P)  N
else if (POS=Count + 1)
else
Call function INS_END( )
PRINT “Invalid position”
Step 5: Return
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
37
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Deleting a node from the linked list
❑ Deletion of the first node
❑ Deletion of the last node
❑ Deletion of a node from a given position
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
38
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Deletion of the first node
Point head to the next node i.e., second node
temp = head
head = head->next
Make sure to free unused memory
free(temp); or delete temp;
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
38
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Deletion of the last node
ALGORITHM: DEL_END (P1, P2, START)
This used two Pointers Pl and P2. Pointer P2 is used to traverse the
linked list And pointer P1 keeps the location of the previous node of P2.
Step 1: START
Step 2: P2  START;
Step 3: While (link(P2) ! = NULL)
Pl  P2
P2  link(P2)
end
Step 4: PRINT data(P2)
Step 5: link(P1)  NULL
Free(P2)
Step 6: STOP
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
39
Fifth Lecture
Sunday 6/11/2023
Data Structure ‫هياكل بيانات‬
LINKED LISTS : (continued)
❖ Deletion of a node from a Singly Linked List After a Specified Node
➢ The following algorithm deletes a node from any position of a sorted Linked LIST.
1 November 2023
Data Structure - Fourth Year, Computer and Control Engineering- First Term
40
Algorithm: DELETE(ITEM)
A linked list in the memory. This algorithm deletes the node where ITEM
first appear in LIST, otherwise it writes “NOT FOUND”
1. if Head =NULL then write: “Empty List” and return [Check for Empty List]
2. if ITEM = Head -> info then: [ beginning node is to delete]
Set Head = Head -> next and return
3. Set PrevNode = NULL, CurrNode=NULL.
4. for(CurrNode =HEAD; CurrNode != NULL; CurrNode = CurrNode ->NEXT)
{ if (ITEM = CurrNode ->INFO ) then:
{
break the loop.
}
Set PrevNode = CurrNode;
} [ end of loop]
5. if(CurrNode = NULL) then write : Item not found in the list and return
6. [delete the current node from the list]
Set PrevNode ->NEXT = CurrNode->NEXT
7. Exit.
ThankYou!
Download