C++ Algorithms
C++ Algorithms
Algorithms
Page 1 of 15
Lec.1
Page 2 of 15
Lec.1
Page 3 of 15
Lec.1
currentMax ← A[0]
for i ← 1 to n-1 do
if currentMax < A[i] then currentMax ← A[i]
return currentMax
Page 4 of 15
Lec.1
Page 5 of 15
Lec.1
Assignment -1-
Write an algorithm for the following
a- count even& odd numbers in given range
Fig.5.2.shows a schematic diagram of a linked list with 3 nodes. Each node is pictured with two
parts. The left part of each node contains the data items and the right part represents the address of
the next node; there is an arrow drawn from it to the next node. The next pointer of the last node
contains a special value, called the NULL pointer, which does not point to any address of the node.
That is NULL pointer indicates the end of the linked list. START pointer will hold the address of the
1st node in the list START = NULL if there is no list (i.e.; NULL list or empty list).
Page 6 of 15
Lec.1
Explanation:
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. However, 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 nodename
{
int info;
nodename *link;
};
Page 7 of 15
Lec.1
TABLE 5-1 Values of head and some of the nodes of the linked list in Figure 5-4
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.
Therefore, 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.) See Figure 5-5.
Table 5-2 shows the values of current, head, and some other nodes in Figure 5-5
TABLE 5-2 Values of current, head, and some of the nodes of the linked list in Figure 5-5
Page 8 of 15
Lec.1
For example, suppose that head points to a linked list of numbers. The following code outputs the
data stored in each node:
current = head;
while (current != NULL)
{
cout << current->info << " ";
current = current->link;
}
struct nodename
{
int info;
nodename *link;
};
We will use the following variable declaration:
nodename *head, *p, *q, *newNode;
Page 9 of 15
Lec.1
Suppose START is the first position in linked list. Let DATA be the element to be inserted in the
new node. POS is the position where the new node is to be inserted. TEMP is a temporary pointer to
hold the node address.
Page 10 of 15
Lec.1
Suppose that p points to the node with info 65, and a new node with info 50 is to be created and
inserted after p. Consider the following statements:
newNode = new nodename; //create newNode
newNode->info = 50; //store 50 in the new node
newNode->link = p->link;
p->link = newNode;
Note that the sequence of statements to insert the node, that is,
newNode->link = p->link;
p->link = newNode;
Page 11 of 15
Lec.1
is very important because to insert newNode in the list we use only one pointer, p, to adjust the links
of the nodes of the linked list. Suppose that we reverse the sequence of the statements and execute the
statements in the following order:
p->link = newNode;
newNode->link = p->link;
Figure 5-7 shows the resulting list after these statements execute.
From Figure 5-7, it is clear that newNode points back to itself and the remainder of the list is lost.
Using two pointers, we can simplify the insertion code somewhat. Suppose q points to the node with
info 34. (See Figure 5-8.)
Page 12 of 15
Lec.1
Suppose that the node with info 34 is to be deleted from the list. The following
statement removes the node from the list:
p->link = p->link->link;
Figure 5-10 shows the resulting list after the preceding statement executes.
From Figure 5-10, it is clear that the node with info 34 is removed from the list.
However, the memory is still occupied by this node and this memory is inaccessible; that is, this node
is dangling. To deallocate the memory, we need a pointer to this node. The following statements delete
the node from the list and deallocate the memory occupied by this node:
q = p->link;
p->link = q->link;
delete q;
Table 5-5 shows the effect of these statements.
Page 13 of 15
Lec.1
Deletion of a Node
Suppose START is the first position in linked list. Let DATA be the element to be
deleted. TEMP, HOLD is a temporary pointer to hold the node address.
1. Input the DATA to be deleted
2. if ((START -> DATA) is equal to DATA)
(a) TEMP = START
(b) START = START -> Next
(c) Set free the node TEMP, which is deleted
(d) Exit
3. HOLD = START
4. while ((HOLD -> Next -> Next) not equal to NULL))
(a) if ((HOLD -> NEXT -> DATA) equal to DATA)
(i) TEMP = HOLD -> Next
(ii) HOLD -> Next = TEMP -> Next
(iii) Set free the node TEMP, which is deleted
(iv) Exit
(b) HOLD = HOLD -> Next
5. if ((HOLD -> next -> DATA) == DATA)
(a) TEMP = HOLD -> Next
(b) Set free the node TEMP, which is deleted
(c) HOLD -> Next = NULL
(d) Exit
6. Disply “DATA not found”
7. Exit
Suppose START is the address of the first node in the linked list and DATA is the
information to be searched. After searching, if the DATA is found, POS will contain the
corresponding position in the list.
1. Input the DATA to be searched
2. Initialize TEMP = START; POS =1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP → DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP → Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
Page 14 of 15
Lec.1
Suppose START is the address of the first node in the linked list. Following algorithm will visit all
nodes from the START node to the end.
1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP == NULL )
4. Display “TEMP → DATA”
5. TEMP = TEMP → Next
6. Exit
Page 15 of 15