4 Marks
4 Marks
4 Marks
Dynamic Size: Linked lists can easily grow or shrink in size as elements
are added or removed. In contrast, arrays often have a fixed size, and
resizing an array can be expensive.
Ease of Merging and Splitting: When merging or splitting two linked lists,
it can be done efficiently by updating a few pointers, while merging or
splitting arrays would require copying elements and resizing.
Slower Traversal: Traversing a linked list can be slower than iterating over
an array due to the scattered nature of memory allocation in linked lists,
which can result in cache inefficiencies.
Subtraction:
To subtract two polynomials, we can simply subtract the corresponding
terms from each list. For example, to subtract the polynomial x^2 + 3x from
the polynomial 3x^2 + 2x + 1, we would subtract the first terms, the second
terms, and the third terms, respectively. This would give us the following
polynomial:
2x^2 - 1x
Multiplication:
To multiply two polynomials, we can use the distributive property. For
example, to multiply the polynomials 3x^2 + 2x + 1 and x^2 + 3x, we would
multiply each term in the first polynomial by each term in the second
polynomial. This would give us the following polynomial:
3x^4 + 11x^3 + 7x^2 + 6x + 1
Division:
To divide two polynomials, we can use long division. For example, to divide
the polynomial 3x^4 + 11x^3 + 7x^2 + 6x + 1 by the polynomial x^2 + 3x,
we would use long division to get the following quotient and remainder:
Quotient: 3x^2 + 2x + 1
Remainder: 2
8) Insertion into a singly linked list
To insert an element into a singly linked list, we need to perform the
following steps:
1. Create a new node containing the element to be inserted.
2. Find the node before which the new node is to be inserted.
3. Make the next pointer of the new node point to the node after the
insertion point.
4. Make the next pointer of the insertion point point to the new node.
9) Insertion
To insert a new node into a cursor-based linked list, we perform the
following steps:
1. Move the cursor to the node before which the new node is to be
inserted.
2. Create a new node containing the element to be inserted.
3. Make the next pointer of the new node point to the node after the
insertion point.
4. Make the previous pointer of the new node point to the node before
the insertion point.
5. Make the next pointer of the insertion point point to the new node.
6. Make the previous pointer of the node after the insertion point point
to the new node.
Deletion
To delete a node from a cursor-based linked list, we perform the following
steps:
1. Move the cursor to the node to be deleted.
2. Make the next pointer of the node before the deleted node point to
the node after the deleted node.
3. Make the previous pointer of the node after the deleted node point to
the node before the deleted node.
4. Delete the deleted node.
10) • Music players: Linked lists are used to store playlists in music
players. Each song in the playlist is represented as a node in the linked list,
and the next pointer of each node points to the next song in the playlist.
This allows users to easily navigate between songs in the playlist.
• Image viewers: Linked lists are also used to store images in image
viewers. Each image is represented as a node in the linked list, and the
next pointer of each node points to the next image in the sequence. This
allows users to easily view images in order.
• Web browsers: Linked lists are used to implement the back and
forward buttons in web browsers. When a user clicks on the back button,
the browser uses a linked list to navigate to the previous page in the
browsing history. When a user clicks on the forward button, the browser
uses a linked list to navigate to the next page in the browsing history.
• Operating systems: Linked lists are used in operating systems to
implement various data structures, such as the memory management
system and the file system. For example, the memory management system
uses a linked list to keep track of free memory blocks. The file system uses
a linked list to represent the directory structure of a file system.
• Compilers: Linked lists are also used in compilers to implement
various data structures, such as the symbol table and the syntax tree. The
symbol table is a data structure that stores information about symbols, such
as variables and functions. The syntax tree is a data structure that
represents the structure of a program.
11) In a stack, insertion and deletion are performed using two fundamental
operations: "push" and "pop." A stack is a data structure that follows the
Last-In-First-Out (LIFO) principle, meaning that the most recently added
item is the first one to be removed.
1. Push Operation (Insertion):
The "push" operation is used to add (insert) an element onto the top of the
stack.
2. Pop Operation (Deletion):
The "pop" operation is used to remove (delete) the top element from the
stack.
•void push (stack *s, int element);
/* Insert an element in the stack */
•int pop (stack *s);
/* Remove and return the top element */