KEMBAR78
Stack implementation using linked list ppt | PPTX
Implement a stack using singly linked list
• storing the information in the form of nodes.
• need to follow the stack rules that is last in first out and all the
operations we should perform so with the help of a top variable only
A stack can be easily implemented through the linked
list.
• In stack Implementation, a stack contains a top pointer.
• which is “head” of the stack where pushing and popping items
happens at the head of the list.
• first node have null in link field and second node link have first node
address in link field and so on and last node address in “top” pointer.
Advantage of using linked list over an arrays
• it is possible to implements a stack that can shrink or grow as much
as needed.
• In using array will put a restriction to the maximum capacity of the
array which can lead to stack overflow.
• Here each new node will be dynamically allocate. so overflow is not
possible.
Stack Operations:
• push() : Insert the element into linked list nothing but which is the top
node of Stack.
• pop() : Return top element from the Stack and move the top pointer
to the second node of linked list or Stack.
• peek(): Return the top element.
• display(): Print all element of Stack.
// C program to Implement a stack
//using singly linked list
#include <stdio.h>
// Declare linked list node
struct Node
{
int data;
struct Node* link;
};
struct Node* top;
// Utility function to add an element
// data in the stack insert at the beginning
void push(int data)
{
// Create new node temp and allocate memory
struct Node* temp;
temp = new Node();
// Check if stack (heap) is full.
// Then inserting an element would
// lead to stack overflow
if (!temp)
{
cout << "nHeap Overflow";
exit(1);
}
// Initialize data into temp data field
temp->data = data;
// Put top pointer reference into temp link
temp->link = top;
// Make temp as top of Stack
top = temp;
}
// Utility function to check if
// the stack is empty or not
int isEmpty()
{
return top == NULL;
}
// Utility function to return top element in a
stack
int peek()
{
// Check for empty stack
if (!isEmpty())
return top->data;
else
exit(1);
}
// Utility function to pop top
// element from the stack
void pop()
{
struct Node* temp;
// Check for stack underflow
if (top == NULL)
{
printf( "nStack Underflow“);
exit(1);
}
else
{
// Top assign into temp
temp = top;
// Assign second node to top
top = top->link;
// Destroy connection between
// first and second
temp->link = NULL;
// Release memory of top node
free(temp);
}
}
/ Function to print all the
// elements of the stack
void display()
{
struct Node* temp;
// Check for stack underflow
if (top == NULL)
{
printf("nStack Underflow“);
exit(1);
}
else
{
temp = top;
while (temp != NULL)
{
// Print node data
printf(“%d”, temp->data << "-> ";
// Assign temp link to temp
temp = temp->link;
}
}
}
int main()
{
// Push the elements of stack
push(11);
push(22);
push(33);
push(44);
// Display stack elements
display();
// Print top element of stack
printf("nTop element is %d“,peek() );
// Delete top elements of stack
pop();
pop();
// Display stack elements
display();
// Print top element of stack
printf( "nTop element is %d“,peek());
return 0;
}
Time Complexity:
• The time complexity for all push(), pop(), and peek() operations is
O(1) as we are not performing any kind of traversal over the list.
• We perform all the operations through the current pointer only.
Queue – Linked List Implementation
• In a Queue data structure, we maintain two pointers, front and rear.
The front points the first item of queue and rear points to last item.
• In linked list, the Data field contains the elements of queue and Next
pointer points to the next element in the queue
• The following two main operations must be implemented efficiently.
• enQueue() :This operation adds a new node after rear and
moves rear to the next node.
• deQueue(): This operation removes the front node and
moves front to the next node.
Queue – Linked List Implementation
aaa bbb ccc ddd null
head
front rear
Linked lists have many advantages compared to arrays.
• Linked queue is not limited in capacity.
• It dispenses with the need to check for the OVERFLOW condition
during insertion.(dynamic memory allocation)
• Linked queue functions as a linear queue and there is no need to view
it as circular for efficient management of space.
queue
#include <stdio.h>
#include <stdlib.h>
// A linked list (LL) node to store a queue entry
struct QNode {
int key;
struct QNode* next;
};
// The queue, front stores the front node of LL and rear stores
the
// last node of LL
struct Queue {
struct QNode *front, *rear;
};
// A utility function to create a new linked list node.
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct
QNode));
temp->key = k;
temp->next = NULL;
return temp;
// A utility function to create an empty queue
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct
Queue));
q->front = q->rear = NULL;
return q;
}
// The function to add a key k to q
void enQueue(struct Queue* q, int k)
{
// Create a new LL node
struct QNode* temp = newNode(k);
// If queue is empty, then new node is front and rear
both
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
// Function to remove a key from given queue q
void deQueue(struct Queue* q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return;
// Store previous front and move front one node
ahead
struct QNode* temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as
NULL
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d n", q->front-
>key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
10 20 null
q
front
rear
30 50 null
40
q
rear
front
OUTPUT
Queue Front : 40
Queue Rear : 50
Time Complexity: Time complexity of both operations enqueue() and dequeue()
is O(1) as we only change few pointers in both operations. There is no loop in any
of the operations.
Polynomial representation using Linked List
• Polynomials and Sparse Matrix are two important applications of
arrays and linked lists.
• A polynomial is composed of different terms where each of them
holds a coefficient and an exponent.
What is Polynomial ?
• A polynomial p(x) is the expression in variable x which is in the form
(axn
+ bxn-1
+ …. + jx+ k)
• where a, b, c …., k fall in the category of real numbers and 'n' is non negative
integer, which is called the degree of polynomial.
An essential characteristic of the polynomial is that each term in the
polynomial expression consists of two parts:
• one is the coefficient
• other is the exponent
Example
• 10x2
+ 26x, here 10 and 26 are coefficients and 2, 1 is its exponential value.
• Points to keep in Mind while working with Polynomials:
• The sign of each coefficient and exponent is stored within the coefficient and the exponent itself
• Additional terms having equal exponent is possible one
• The storage allocation for each term in the polynomial must be done in ascending and descending
order of their exponent
Representation of Polynomial
Polynomial can be represented in the various ways. These are:
• By the use of arrays
• By the use of Linked List
Representation of Polynomial using Linked List
• A polynomial can be thought of as an ordered list of non zero terms.
Each non zero term is a two-tuple which holds two pieces of
information:
• The exponent part
• The coefficient part
// C program for addition of two polynomials using Linked Lists
#include <stdio.h>
// Node structure containing power and coefficient of variable
struct Node {
int coeff;
int pow;
struct Node* next;
};
// Function to create new node
void create_node(int x, int y, struct Node* temp)
{
struct Node *r, *z;
z = *temp;
if (z == NULL) {
r = (struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
// Function Adding two polynomial numbers
void polyadd(struct Node* poly1, struct Node* poly2,
struct Node* poly)
{
while (poly1->next && poly2->next) {
// If power of 1st polynomial is greater then 2nd, then store 1st as it is and
move its pointer
if (poly1->pow > poly2->pow) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st
, then store 2nd as it is and move
its pointer
else if (poly1->pow < poly2->pow) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
// If power of both polynomial numbers is same then add their coefficients
else {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Dynamically create new node
poly->next
= (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while (poly1->next || poly2->next) {
if (poly1->next) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if (poly2->next) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next
= (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}
// Display Linked list
void show(struct Node* node)
{
while (node->next != NULL) {
printf("%dx^%d", node->coeff, node-
>pow);
node = node->next;
if (node->coeff >= 0) {
if (node->next != NULL)
printf("+");
}
}
}
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
// Create first list of 5x^2 + 4x^1 + 2x^0
create_node(5, 2, &poly1);
create_node(4, 1, &poly1);
create_node(2, 0, &poly1);
// Create second list of -5x^1 - 5x^0
create_node(-5, 1, &poly2);
create_node(-5, 0, &poly2);
printf("1st Number: ");
show(poly1);
printf("n2nd Number: ");
show(poly2);
poly = (struct Node*)malloc(sizeof(struct Node));
// Function add two polynomial numbers
polyadd(poly1, poly2, poly);
// Display resultant List
printf("nAdded polynomial: ");
show(poly);
return 0;
}

Stack implementation using linked list ppt

  • 1.
    Implement a stackusing singly linked list • storing the information in the form of nodes. • need to follow the stack rules that is last in first out and all the operations we should perform so with the help of a top variable only
  • 5.
    A stack canbe easily implemented through the linked list. • In stack Implementation, a stack contains a top pointer. • which is “head” of the stack where pushing and popping items happens at the head of the list. • first node have null in link field and second node link have first node address in link field and so on and last node address in “top” pointer.
  • 6.
    Advantage of usinglinked list over an arrays • it is possible to implements a stack that can shrink or grow as much as needed. • In using array will put a restriction to the maximum capacity of the array which can lead to stack overflow. • Here each new node will be dynamically allocate. so overflow is not possible.
  • 7.
    Stack Operations: • push(): Insert the element into linked list nothing but which is the top node of Stack. • pop() : Return top element from the Stack and move the top pointer to the second node of linked list or Stack. • peek(): Return the top element. • display(): Print all element of Stack.
  • 8.
    // C programto Implement a stack //using singly linked list #include <stdio.h> // Declare linked list node struct Node { int data; struct Node* link; }; struct Node* top;
  • 9.
    // Utility functionto add an element // data in the stack insert at the beginning void push(int data) { // Create new node temp and allocate memory struct Node* temp; temp = new Node(); // Check if stack (heap) is full. // Then inserting an element would // lead to stack overflow if (!temp) { cout << "nHeap Overflow"; exit(1); } // Initialize data into temp data field temp->data = data; // Put top pointer reference into temp link temp->link = top; // Make temp as top of Stack top = temp; }
  • 10.
    // Utility functionto check if // the stack is empty or not int isEmpty() { return top == NULL; } // Utility function to return top element in a stack int peek() { // Check for empty stack if (!isEmpty()) return top->data; else exit(1); }
  • 11.
    // Utility functionto pop top // element from the stack void pop() { struct Node* temp; // Check for stack underflow if (top == NULL) { printf( "nStack Underflow“); exit(1); } else { // Top assign into temp temp = top; // Assign second node to top top = top->link; // Destroy connection between // first and second temp->link = NULL; // Release memory of top node free(temp); } }
  • 12.
    / Function toprint all the // elements of the stack void display() { struct Node* temp; // Check for stack underflow if (top == NULL) { printf("nStack Underflow“); exit(1); } else { temp = top; while (temp != NULL) { // Print node data printf(“%d”, temp->data << "-> "; // Assign temp link to temp temp = temp->link; } } }
  • 13.
    int main() { // Pushthe elements of stack push(11); push(22); push(33); push(44); // Display stack elements display(); // Print top element of stack printf("nTop element is %d“,peek() ); // Delete top elements of stack pop(); pop(); // Display stack elements display(); // Print top element of stack printf( "nTop element is %d“,peek()); return 0; }
  • 14.
    Time Complexity: • Thetime complexity for all push(), pop(), and peek() operations is O(1) as we are not performing any kind of traversal over the list. • We perform all the operations through the current pointer only.
  • 15.
    Queue – LinkedList Implementation • In a Queue data structure, we maintain two pointers, front and rear. The front points the first item of queue and rear points to last item. • In linked list, the Data field contains the elements of queue and Next pointer points to the next element in the queue • The following two main operations must be implemented efficiently. • enQueue() :This operation adds a new node after rear and moves rear to the next node. • deQueue(): This operation removes the front node and moves front to the next node.
  • 16.
    Queue – LinkedList Implementation aaa bbb ccc ddd null head front rear
  • 17.
    Linked lists havemany advantages compared to arrays. • Linked queue is not limited in capacity. • It dispenses with the need to check for the OVERFLOW condition during insertion.(dynamic memory allocation) • Linked queue functions as a linear queue and there is no need to view it as circular for efficient management of space.
  • 18.
    queue #include <stdio.h> #include <stdlib.h> //A linked list (LL) node to store a queue entry struct QNode { int key; struct QNode* next; }; // The queue, front stores the front node of LL and rear stores the // last node of LL struct Queue { struct QNode *front, *rear; }; // A utility function to create a new linked list node. struct QNode* newNode(int k) { struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode)); temp->key = k; temp->next = NULL; return temp;
  • 19.
    // A utilityfunction to create an empty queue struct Queue* createQueue() { struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue)); q->front = q->rear = NULL; return q; } // The function to add a key k to q void enQueue(struct Queue* q, int k) { // Create a new LL node struct QNode* temp = newNode(k); // If queue is empty, then new node is front and rear both if (q->rear == NULL) { q->front = q->rear = temp; return; } // Add the new node at the end of queue and change rear q->rear->next = temp; q->rear = temp;
  • 20.
    // Function toremove a key from given queue q void deQueue(struct Queue* q) { // If queue is empty, return NULL. if (q->front == NULL) return; // Store previous front and move front one node ahead struct QNode* temp = q->front; q->front = q->front->next; // If front becomes NULL, then change rear also as NULL if (q->front == NULL) q->rear = NULL; free(temp); }
  • 21.
    int main() { struct Queue*q = createQueue(); enQueue(q, 10); enQueue(q, 20); deQueue(q); deQueue(q); enQueue(q, 30); enQueue(q, 40); enQueue(q, 50); deQueue(q); printf("Queue Front : %d n", q->front- >key); printf("Queue Rear : %d", q->rear->key); return 0; }
  • 22.
  • 23.
  • 24.
    OUTPUT Queue Front :40 Queue Rear : 50 Time Complexity: Time complexity of both operations enqueue() and dequeue() is O(1) as we only change few pointers in both operations. There is no loop in any of the operations.
  • 25.
    Polynomial representation usingLinked List • Polynomials and Sparse Matrix are two important applications of arrays and linked lists. • A polynomial is composed of different terms where each of them holds a coefficient and an exponent.
  • 26.
    What is Polynomial? • A polynomial p(x) is the expression in variable x which is in the form (axn + bxn-1 + …. + jx+ k) • where a, b, c …., k fall in the category of real numbers and 'n' is non negative integer, which is called the degree of polynomial. An essential characteristic of the polynomial is that each term in the polynomial expression consists of two parts: • one is the coefficient • other is the exponent
  • 27.
    Example • 10x2 + 26x,here 10 and 26 are coefficients and 2, 1 is its exponential value. • Points to keep in Mind while working with Polynomials: • The sign of each coefficient and exponent is stored within the coefficient and the exponent itself • Additional terms having equal exponent is possible one • The storage allocation for each term in the polynomial must be done in ascending and descending order of their exponent
  • 28.
    Representation of Polynomial Polynomialcan be represented in the various ways. These are: • By the use of arrays • By the use of Linked List
  • 29.
    Representation of Polynomialusing Linked List • A polynomial can be thought of as an ordered list of non zero terms. Each non zero term is a two-tuple which holds two pieces of information: • The exponent part • The coefficient part
  • 35.
    // C programfor addition of two polynomials using Linked Lists #include <stdio.h> // Node structure containing power and coefficient of variable struct Node { int coeff; int pow; struct Node* next; }; // Function to create new node void create_node(int x, int y, struct Node* temp) { struct Node *r, *z; z = *temp; if (z == NULL) { r = (struct Node*)malloc(sizeof(struct Node)); r->coeff = x; r->pow = y; *temp = r; r->next = (struct Node*)malloc(sizeof(struct Node)); r = r->next; r->next = NULL; } else { r->coeff = x; r->pow = y; r->next = (struct Node*)malloc(sizeof(struct Node)); r = r->next; r->next = NULL; } }
  • 36.
    // Function Addingtwo polynomial numbers void polyadd(struct Node* poly1, struct Node* poly2, struct Node* poly) { while (poly1->next && poly2->next) { // If power of 1st polynomial is greater then 2nd, then store 1st as it is and move its pointer if (poly1->pow > poly2->pow) { poly->pow = poly1->pow; poly->coeff = poly1->coeff; poly1 = poly1->next; } // If power of 2nd polynomial is greater then 1st , then store 2nd as it is and move its pointer else if (poly1->pow < poly2->pow) { poly->pow = poly2->pow; poly->coeff = poly2->coeff; poly2 = poly2->next; } // If power of both polynomial numbers is same then add their coefficients else { poly->pow = poly1->pow; poly->coeff = poly1->coeff + poly2->coeff; poly1 = poly1->next; poly2 = poly2->next; }
  • 37.
    // Dynamically createnew node poly->next = (struct Node*)malloc(sizeof(struct Node)); poly = poly->next; poly->next = NULL; } while (poly1->next || poly2->next) { if (poly1->next) { poly->pow = poly1->pow; poly->coeff = poly1->coeff; poly1 = poly1->next; } if (poly2->next) { poly->pow = poly2->pow; poly->coeff = poly2->coeff; poly2 = poly2->next; } poly->next = (struct Node*)malloc(sizeof(struct Node)); poly = poly->next; poly->next = NULL; } }
  • 38.
    // Display Linkedlist void show(struct Node* node) { while (node->next != NULL) { printf("%dx^%d", node->coeff, node- >pow); node = node->next; if (node->coeff >= 0) { if (node->next != NULL) printf("+"); } } }
  • 39.
    int main() { struct Node*poly1 = NULL, *poly2 = NULL, *poly = NULL; // Create first list of 5x^2 + 4x^1 + 2x^0 create_node(5, 2, &poly1); create_node(4, 1, &poly1); create_node(2, 0, &poly1); // Create second list of -5x^1 - 5x^0 create_node(-5, 1, &poly2); create_node(-5, 0, &poly2); printf("1st Number: "); show(poly1); printf("n2nd Number: "); show(poly2); poly = (struct Node*)malloc(sizeof(struct Node)); // Function add two polynomial numbers polyadd(poly1, poly2, poly); // Display resultant List printf("nAdded polynomial: "); show(poly); return 0; }