AAD Unit4
AAD Unit4
AAD Unit4
-Anjali Deshpande
Click to add Text
Memory Allocation
❑ Static Memory Allocation:
Static Memory is allocated for declared variables by the compiler. The
address can be found using the addressof operator and can be assigned to a
pointer. The memory is allocated during compile time.
Head
data pointer
A Simple Linked List Class
We use two classes: Node and List
Declare Node class for the nodes
data: double-type data in this example
next: a pointer to the next node in the list
class Node {
public:
double data; // data
Node* next; // pointer to next
};
A Simple Linked List Class
Declare List, which contains
head: a pointer to the first node in the list.
Since the list is empty initially, head is set to NULL
Operations on List
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor
Operations of List
IsEmpty: determine whether the list is empty
InsertNode: insert a new node at a particular
position
FindNode: find a node with a given value
DeleteNode: delete a node with a given value
DisplayList: print all the nodes in the list
Inserting a new node
Node* InsertNode(int index, double x)
Insert a node with data equal to x after the index’th elements.
(i.e., when index = 0, insert the node as the first element;
when index = 1, insert the node after the first element, and so on)
If the insertion is successful, return the inserted node.
Otherwise, return NULL.
(If index is < 0 or > length of the list, the insertion will fail.)
Steps
index’th
1. Locate index’th element element
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
result
Using List
int main(void)
{
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
6
7 result
Using List
5
Number of nodes in the list: 3
5.0 found
4.5 not found
6
int main(void) 5
{ Number of nodes in the list: 2
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
Variations of Linked Lists
Circular linked lists
The last node points to the first node of the list
A B C
Head
A B C
Head
Array versus Linked Lists
• When we add them together, we can group the like terms and generate
the result 3 - 2x^2 + 5x^2 - 3x^3:
In this algorithm, we first create two pointers, p1 and p2, to the head
pointers of the two input polynomials. Then, we generate the new
polynomial nodes based on the powers of these two pointers. There are
three cases:
1. p1‘s power is greater than p2‘s power: In this case, we append a new
node with p1‘s power and coefficient. Also, we move p1 to the next
node.
2. p2‘s power is greater than p1‘s power: In this case, we append a new
node with p2‘s power and coefficient. Also, we move p2 to the next
node.
3. p1 and p2 have the same power: In this case, the new coefficient is
the total of p1‘s coefficient and p2‘s coefficient. If the new coefficient is
not 0, we append a new node with the same power and the new
coefficient. Also, we move both p1 and p2 to the next nodes.
After that, we continue to append the remaining nodes from p1 or p2 until
we finish the calculation on all nodes.
The append function can create a new linked list node based on the input
power and coefficient. Also, it appends the new node to the tail node
and returns the new tail node:
Generalized Linked List
• In GLL, there is a need of an extra field called
as tag to indicate whether the element is an
atom or a sublist
• Tag field contains value either 0 or 1
• 1 indicates sublist
• 0 indicates atom
• When the node represents a sublist, then it
stores address of first element of the sublist
Data or
Tag Pointer
Down Pointer
EXAMPLES…
Why Generalized Linked List?