2
Most read
6
Most read
7
Most read
For More Visit: Https://www.ThesisScientist.com
Unit 4
Linked List
Static and Dynamic Memory Allocation
In static memory allocation memory is allocated at compile time. If we declare a stack through an array of
100 elements (all integers) then the statement will be :
int stk[100];
This declaration would typically be used if 100 records are to be stored in memory. The moment we make
this declaration 200 bytes are reserved in memory for storing 100 integers in it. However it may so happen
that when we actually run the program we might be interested in storing only 60 integers, even in this case
200 bytes would get reserved in memory which would result in wastage of memory. There may be cases
when we need to store more than 100 records, in this case array would fall short in size.
We can overcome these problems by allocating memory at Run Time (instead of compile time), this is
called Dynamic Memory Allocation. It is done by using Standard Library functions malloc () and calloc ().
Syntax for Allocating memory through malloc :
p = (data type*) malloc (n*size of (<datatype>));
Example 1
p = (int *) malloc (n * 2);
The expression (int *) is used to typecast the address being returned as the address of an integer.
The calloc() function is exactly similar to malloc() except for the fact that it needs two arguments as against
the one argument required by malloc().
Example 2
int *p
p = (int *) calloc (100, 2);
Here 2 indicates that we wish to allocate memory for storing integers, since an integer is a 2 byte entity, and
100 indicates that we want to reserve space for storing 100 integers.
Another difference between malloc() and colloc() is that, by default the memory allocated by malloc()
contains garbage values whereas that allocated by calloc() contains all zeros.
Pointers
For More Visit: Https://www.ThesisScientist.com
Pointer is a variable that gives the Address (location or link or reference) of some other variable. As other
variables, pointer also cannot be used before declaration. We can declare the pointer variable as:
datatype *p;
This declaration specifies that p is a pointer variable which, will store the address of variable of particular
datatype. '*' stands for 'value stored at address'. Thus if we declare int *p; then it would mean, the value at
the address contained in p is an integer.
Static and Dynamic Variables
Static variables are declared and named while writing the program. (Space for them exists as long as the
program, in which they are declared, is running.) Static variables cannot be created or destroyed during
execution of the program in which they are declared.
Dynamic variables are created (and may be destroyed) during program execution since dynamic variables
do not exist while the program is compiled, but only when it is run, they cannot be assigned names while it
is being written. The only way to access dynamic variables is by using pointers. Once it is created,
however, a dynamic variable does contain data and must have a type like any other variable. If a dynamic
variable is created in a function, then it can continue to exist even after the function terminates.
Linked Linear List
We saw in previous chapters how static representation of linear ordered list through Array leads to wastage
of memory and in some cases overflows. Now we don't want to assign memory to any linear list in advance
instead we want to allocate memory to elements as they are inserted in list. This requires Dynamic
Allocation of memory and it can be achieved by using malloc() or calloc() function.
But memory assigned to elements will not be contiguous, which is a requirement for linear ordered list, and
was provided by array representation. How we could achieve this?
We have to consider a logical ordered list, i.e. elements are stored in different memory locations but they
are linked to each other and form a logical list as in Fig. 1.1. This link represents that each element has
A1 A2 A3 A4
……
.
AnA1 A2 A3 A4
……
.
An
Figure 1.1: Logical List
the address of its logical successor element in the list. We can understand this concept through a real life
example. Suppose their is a list of 8 friends, x1, x2......x8. Each friend resides at different locations of the
city. x1 knows the address of x2, x2 knows the address of x3 and so on .... x7 has the address of x8. If one
wants to go to the house of x8 and he does not know the address he will go to x2 and so on Fig 1.2.
The concept of linked list is like a family despaired, but still bound together. From the above discussion it is
clear that Link list is a collection of elements called nodes, each of
x1 Add.of x2 x2 Add.of x3 X3
……. X8 NULLx1 Add.of x2 x2 Add.of x3 X3
……. X8 NULL
Figure 1.2
which stores two items of information:
 An element of the list
For More Visit: Https://www.ThesisScientist.com
 A link or address of another node
Link can be provided through a pointer that indicates the location of the node containing the successor of
this list element. The NULL in the last node indicates that this is the last node in the list.
Implementation of Linked List
Link List is a linear Data Structure with the following operations defined on it:
 Insertion of a node at the beginning
 Insertion of a node at the end
 Insertion of a node after a specified node
 Deletion of a particular node from the list
 Traversing of entire link list.
Insertion of a Node at the Beginning
We have a linked list in which the first element is pointed by list pointer.
We can take node data as Input, from user and point this node through temp. Now we can attach temp to the
list by putting address of List in the link field of node pointed by temp Fig. 1.3. Then we can update the
……. NULLlist
temp
……. NULLlist
temp
Figure 1.3
Insertion of a Node at the End
We traverse the list until NULL (i.e. end of the list) is found. We traverse the list through an additional
pointer 'p' and, fix the start pointer list at the beginning of linked list. When p reaches the end, we will
attach temp to p by putting the address of node pointed by temp in the link field of p Fig. 1.4.
NULL
……. NILLlist
xtemp
p ® ………. p ® …………………… p ®
NULL
……. NILLlist
xtemp
p ® ………. p ® …………………… p ®
……. NILLlist
xtemp
p ® ………. p ® …………………… p ®
Figure 1.4
Insertion of a Node After a Specified Node
For More Visit: Https://www.ThesisScientist.com
Traverse the list until node with specified value is found or the end of list is found. If end of list is found
then print the message that "Given No. is not present" otherwise insert node pointed by temp between nodes
pointed by p and p -> link (p is used to traverse the list) Fig. 1.5.
NULLn
xtemp NULL
p
NULLn
xtemp NULL
p
p ® link
NULLn
xtemp NULL
p
NULLn
xtemp NULL
p
p ® link
Figure 1.5
Deletion of a Node from Linked List
Search the node, which is to be deleted from the list by traversing the list through pointer p. If end of List is
found then print the message the 'Given No. is not found' otherwise store the address of node successor to
the node to be deleted in the link field of p. Free the node to be deleted, Fig. 1.6
NULLnum
Node to be
deleted
NULLnum
Node to be
deleted
Figure 1.6
Concatenation of Linked Lists
Consider a case where we have two Linked Lists pointed to by two different pointers, say p and q
respectively, and we want to concatenate 2nd list at the end of the first list. We can do it by traversing first
list till the end and then store the address of first node in the second list, in the link field of last node of first
list. Suppose we are traversing first list by pointer temp, then we can concatenate the list by the
statement(Fig. 1.7)
temp -> link = q;
For More Visit: Https://www.ThesisScientist.com
NULL
(a)
NULL
Temp ®
(b)
NULLq ®
p ®
q ®
p ® NULL
(a)
NULL
Temp ®
(b)
NULLq ®
p ®
q ®
p ®
Figure 1.7 (a) Lists before Concatenation, (b) List after Concatenation
Applications of Linked List
Linked Stack
Stack is a very common and popular linear data structure. Since it is a linear data structure it can be
represented through Array and linked list. We have studied in previous chapters the Array representation of
stacks. It has some drawbacks, major one being fixed size. We have to cope with memory wastage and
conditions of overflow while implementing stacks through array. In this section we implement stacks
through linked list.
Linked list uses dynamic memory allocation. Hence, it overcomes the drawbacks of array representation of
stack.
In stack we maintain a pointer 'top' of struct node type. All the insertions and deletions are made on one
end, which is to be pointed by 'top' as in Fig. 1.8.
NULL
top ®
NULL
top ®
Figure 1.8: Stack
Functions for insertion (push operation) and deletion (pop operation) are given below:
For More Visit: Https://www.ThesisScientist.com
Linked Queue
Like stack, queue is also very popular linear Data structure and can be represented by linked List. We have
to maintain two pointers 'front and rear' of struct node type Fig 15.9. We have to increment 'rear pointer' on
insertion and increment 'front' on deletion from the Queue.
NULL
rearfont
NULL
rearfont
Figure 1.9: Linked Queue
Polynomial Representation
Polynomials in general are represented by the following equation:
f(x) = cixei
+ ci-1xei-1
+....+c1xe1
where Ci are non zero coefficients of variable x And ei are exponents such that
ei > ei-1 > ei-2 >.........> e1 > 0.
For example :
F1(x) = 3x7
-7x5
+3x2
+1
F2(x) =5x8
+6x5
+4x2
+13
These Polynomials can be added to form another Polynomial
Let f3(x) = f1(x) +f2(x)
f3(x) = 5x8
+ 3x7
–x5
+ 7x2
+14
This is achieved by adding coefficients of variables with same exponent value.
These Polynomials can be maintained using a Linked List. To achieve this each term will be represented by
a node and each node should consist of three elements namely coefficients, exponents and a link to the next
term (represented by another node)
For instance, the polynomial
f(x)= 4x11
-3x9
+2x3
+1 will be stored as :
ii)
iii)
4 11 -3 9 2 3 1 0 NULL
f (x)
4 11 -3 9 2 3 1 0 NULL
f (x)
5 8z Now exp (p) > exp (q) Hence-4 55 8z Now exp (p) > exp (q) Hence-4 5
5 8z -4 5-4 5 9 35 8z -4 5-4 5 9 3
Coef. Exp. LinkCoef. Exp. Link
For More Visit: Https://www.ThesisScientist.com
iv)
v)
Doubly Linked Lists
In the single linked list each node provides information about where the next node is in the list. It faces
difficulty if we are pointing to a specific node, then we can move only in the direction of the links. It has no
idea about where the previous node lies in memory. The only way to find the node which precedes that
specific node is to start back at the beginning of the list. The same problem arises when one wishes to
delete an arbitrary node from a single linked list. Since in order to easily delete an arbitrary node one must
know the preceding node. This problem can be avoided by using Doubly Linked List, we can store in each
node not only the address of next node but also the address of the previous node in the linked list. A node
in Doubly Linked List has three fields Fig 1.10.
 Data
 Left Link
 Right Link
L LINK DATA R LINKL LINK DATA R LINK
Figure 1.10: Node of Doubly Linked List
Left link keeps the address of previous node and Right Link keeps the address of next node. Doubly Linked
List has following property figure 15.11.
p=p->llink->rlink=p->rlink->llink.
L LINK R LINK
p
L LINK R LINKL LINK R LINKL LINK R LINK
p
Figure 15.11
This formula reflects the essential virtue of this structure, namely, that one can go back and forth with equal
ease.
Circular Linked List
Circular Linked List is another remedy for the drawbacks of the Single Linked List besides Doubly Linked
List. A slight change to the structure of a linear list is made to convert it to circular linked list; link field in
the last node contains a pointer back to the first node rather than a NULL. See Figure 1.12
Figure 1.12: Circular Linked List
5 8z -4 5-4 5 9 3 7 2
Now p exhorted Hence,
5 8z -4 5-4 5 9 3 7 25 8z -4 5-4 5 9 3 7 2
Now p exhorted Hence,
5 8z -4 5 9 3 7 2 2 15 8z -4 5 9 3 7 2 2 1

More Related Content

PPTX
File handling in Python
PPTX
Linked list
PPT
Abstract data types
PPTX
System Programming Unit II
PPTX
Presentation on Breadth First Search (BFS)
PPTX
AI_Session 7 Greedy Best first search algorithm.pptx
PPTX
Graph in data structure
PPT
Data structures using c
File handling in Python
Linked list
Abstract data types
System Programming Unit II
Presentation on Breadth First Search (BFS)
AI_Session 7 Greedy Best first search algorithm.pptx
Graph in data structure
Data structures using c

What's hot (20)

PDF
Lab report for Prolog program in artificial intelligence.
DOC
Time and space complexity
PDF
Double ended queue
PPTX
Asymptotic Notations
PPTX
daa-unit-3-greedy method
PPT
1.1 binary tree
PDF
Data structure using c++
PDF
Topological Sort
PPT
Data structure lecture 1
PPTX
Three Address code
PPTX
Data Structures and Algorithm - Module 1.pptx
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPTX
Insertion Sorting
PDF
Functions and modules in python
ODP
Evolutionary process models se.ppt
PPT
Mining Frequent Patterns, Association and Correlations
PPT
Multidimensional array in C
PPTX
Dfs presentation
PDF
Python programming : Files
PPT
Backtracking Algorithm.ppt
Lab report for Prolog program in artificial intelligence.
Time and space complexity
Double ended queue
Asymptotic Notations
daa-unit-3-greedy method
1.1 binary tree
Data structure using c++
Topological Sort
Data structure lecture 1
Three Address code
Data Structures and Algorithm - Module 1.pptx
Introduction to Dynamic Programming, Principle of Optimality
Insertion Sorting
Functions and modules in python
Evolutionary process models se.ppt
Mining Frequent Patterns, Association and Correlations
Multidimensional array in C
Dfs presentation
Python programming : Files
Backtracking Algorithm.ppt
Ad

Similar to Linked List Static and Dynamic Memory Allocation (20)

PPTX
linkedlist.pptx
PPT
Lecture 3 List of Data Structures & Algorithms
PPTX
Linked lists
PPTX
mbit_Unit-2_Linked List.pptx
PPT
Algo>ADT list & linked list
PPTX
VCE Unit 02 (1).pptx
PPTX
module 3-.pptx
PPTX
Linked List.pptx
PPTX
DS_LinkedList.pptx
PPTX
Unit 5 linked list
PPT
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
DOCX
Link list assi
PPTX
Data Structures-UNIT Four_Linked_List.pptx
PDF
DS Module 03.pdf
PDF
Unit - 2.pdf
PDF
Linked list (introduction) 1
PPTX
RPT_03_A_Linked List presentation for FE
PPTX
Data Structures Introduction & Linear DS
PPT
linkedlist.pptx
Lecture 3 List of Data Structures & Algorithms
Linked lists
mbit_Unit-2_Linked List.pptx
Algo>ADT list & linked list
VCE Unit 02 (1).pptx
module 3-.pptx
Linked List.pptx
DS_LinkedList.pptx
Unit 5 linked list
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
Link list assi
Data Structures-UNIT Four_Linked_List.pptx
DS Module 03.pdf
Unit - 2.pdf
Linked list (introduction) 1
RPT_03_A_Linked List presentation for FE
Data Structures Introduction & Linear DS
Ad

More from Prof Ansari (20)

PDF
Sci Hub New Domain
PDF
Sci Hub cc Not Working
PDF
basics of computer network
PDF
JAVA INTRODUCTION
PDF
Project Evaluation and Estimation in Software Development
PDF
Stepwise Project planning in software development
PDF
Database and Math Relations
PDF
Normalisation in Database management System (DBMS)
PDF
Entity-Relationship Data Model in DBMS
PDF
A Detail Database Architecture
PDF
INTRODUCTION TO Database Management System (DBMS)
PDF
Master thesis on Vehicular Ad hoc Networks (VANET)
PDF
Master Thesis on Vehicular Ad-hoc Network (VANET)
PDF
INTERFACING WITH INTEL 8251A (USART)
PDF
HOST AND NETWORK SECURITY by ThesisScientist.com
PDF
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
PDF
INTRODUCTION TO VISUAL BASICS
PDF
introduction to Blogging ppt
PDF
INTRODUCTION TO SOFTWARE ENGINEERING
PDF
Introduction to E-commerce
Sci Hub New Domain
Sci Hub cc Not Working
basics of computer network
JAVA INTRODUCTION
Project Evaluation and Estimation in Software Development
Stepwise Project planning in software development
Database and Math Relations
Normalisation in Database management System (DBMS)
Entity-Relationship Data Model in DBMS
A Detail Database Architecture
INTRODUCTION TO Database Management System (DBMS)
Master thesis on Vehicular Ad hoc Networks (VANET)
Master Thesis on Vehicular Ad-hoc Network (VANET)
INTERFACING WITH INTEL 8251A (USART)
HOST AND NETWORK SECURITY by ThesisScientist.com
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
INTRODUCTION TO VISUAL BASICS
introduction to Blogging ppt
INTRODUCTION TO SOFTWARE ENGINEERING
Introduction to E-commerce

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
Applications of Equal_Area_Criterion.pdf
PPTX
ai_satellite_crop_management_20250815030350.pptx
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
wireless networks, mobile computing.pptx
PPTX
Building constraction Conveyance of water.pptx
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PPTX
mechattonicsand iotwith sensor and actuator
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
Unit1 - AIML Chapter 1 concept and ethics
PPTX
Amdahl’s law is explained in the above power point presentations
PPTX
Module 8- Technological and Communication Skills.pptx
Exploratory_Data_Analysis_Fundamentals.pdf
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
Applications of Equal_Area_Criterion.pdf
ai_satellite_crop_management_20250815030350.pptx
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
wireless networks, mobile computing.pptx
Building constraction Conveyance of water.pptx
Abrasive, erosive and cavitation wear.pdf
MLpara ingenieira CIVIL, meca Y AMBIENTAL
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
20250617 - IR - Global Guide for HR - 51 pages.pdf
mechattonicsand iotwith sensor and actuator
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
Unit1 - AIML Chapter 1 concept and ethics
Amdahl’s law is explained in the above power point presentations
Module 8- Technological and Communication Skills.pptx

Linked List Static and Dynamic Memory Allocation

  • 1. For More Visit: Https://www.ThesisScientist.com Unit 4 Linked List Static and Dynamic Memory Allocation In static memory allocation memory is allocated at compile time. If we declare a stack through an array of 100 elements (all integers) then the statement will be : int stk[100]; This declaration would typically be used if 100 records are to be stored in memory. The moment we make this declaration 200 bytes are reserved in memory for storing 100 integers in it. However it may so happen that when we actually run the program we might be interested in storing only 60 integers, even in this case 200 bytes would get reserved in memory which would result in wastage of memory. There may be cases when we need to store more than 100 records, in this case array would fall short in size. We can overcome these problems by allocating memory at Run Time (instead of compile time), this is called Dynamic Memory Allocation. It is done by using Standard Library functions malloc () and calloc (). Syntax for Allocating memory through malloc : p = (data type*) malloc (n*size of (<datatype>)); Example 1 p = (int *) malloc (n * 2); The expression (int *) is used to typecast the address being returned as the address of an integer. The calloc() function is exactly similar to malloc() except for the fact that it needs two arguments as against the one argument required by malloc(). Example 2 int *p p = (int *) calloc (100, 2); Here 2 indicates that we wish to allocate memory for storing integers, since an integer is a 2 byte entity, and 100 indicates that we want to reserve space for storing 100 integers. Another difference between malloc() and colloc() is that, by default the memory allocated by malloc() contains garbage values whereas that allocated by calloc() contains all zeros. Pointers
  • 2. For More Visit: Https://www.ThesisScientist.com Pointer is a variable that gives the Address (location or link or reference) of some other variable. As other variables, pointer also cannot be used before declaration. We can declare the pointer variable as: datatype *p; This declaration specifies that p is a pointer variable which, will store the address of variable of particular datatype. '*' stands for 'value stored at address'. Thus if we declare int *p; then it would mean, the value at the address contained in p is an integer. Static and Dynamic Variables Static variables are declared and named while writing the program. (Space for them exists as long as the program, in which they are declared, is running.) Static variables cannot be created or destroyed during execution of the program in which they are declared. Dynamic variables are created (and may be destroyed) during program execution since dynamic variables do not exist while the program is compiled, but only when it is run, they cannot be assigned names while it is being written. The only way to access dynamic variables is by using pointers. Once it is created, however, a dynamic variable does contain data and must have a type like any other variable. If a dynamic variable is created in a function, then it can continue to exist even after the function terminates. Linked Linear List We saw in previous chapters how static representation of linear ordered list through Array leads to wastage of memory and in some cases overflows. Now we don't want to assign memory to any linear list in advance instead we want to allocate memory to elements as they are inserted in list. This requires Dynamic Allocation of memory and it can be achieved by using malloc() or calloc() function. But memory assigned to elements will not be contiguous, which is a requirement for linear ordered list, and was provided by array representation. How we could achieve this? We have to consider a logical ordered list, i.e. elements are stored in different memory locations but they are linked to each other and form a logical list as in Fig. 1.1. This link represents that each element has A1 A2 A3 A4 …… . AnA1 A2 A3 A4 …… . An Figure 1.1: Logical List the address of its logical successor element in the list. We can understand this concept through a real life example. Suppose their is a list of 8 friends, x1, x2......x8. Each friend resides at different locations of the city. x1 knows the address of x2, x2 knows the address of x3 and so on .... x7 has the address of x8. If one wants to go to the house of x8 and he does not know the address he will go to x2 and so on Fig 1.2. The concept of linked list is like a family despaired, but still bound together. From the above discussion it is clear that Link list is a collection of elements called nodes, each of x1 Add.of x2 x2 Add.of x3 X3 ……. X8 NULLx1 Add.of x2 x2 Add.of x3 X3 ……. X8 NULL Figure 1.2 which stores two items of information:  An element of the list
  • 3. For More Visit: Https://www.ThesisScientist.com  A link or address of another node Link can be provided through a pointer that indicates the location of the node containing the successor of this list element. The NULL in the last node indicates that this is the last node in the list. Implementation of Linked List Link List is a linear Data Structure with the following operations defined on it:  Insertion of a node at the beginning  Insertion of a node at the end  Insertion of a node after a specified node  Deletion of a particular node from the list  Traversing of entire link list. Insertion of a Node at the Beginning We have a linked list in which the first element is pointed by list pointer. We can take node data as Input, from user and point this node through temp. Now we can attach temp to the list by putting address of List in the link field of node pointed by temp Fig. 1.3. Then we can update the ……. NULLlist temp ……. NULLlist temp Figure 1.3 Insertion of a Node at the End We traverse the list until NULL (i.e. end of the list) is found. We traverse the list through an additional pointer 'p' and, fix the start pointer list at the beginning of linked list. When p reaches the end, we will attach temp to p by putting the address of node pointed by temp in the link field of p Fig. 1.4. NULL ……. NILLlist xtemp p ® ………. p ® …………………… p ® NULL ……. NILLlist xtemp p ® ………. p ® …………………… p ® ……. NILLlist xtemp p ® ………. p ® …………………… p ® Figure 1.4 Insertion of a Node After a Specified Node
  • 4. For More Visit: Https://www.ThesisScientist.com Traverse the list until node with specified value is found or the end of list is found. If end of list is found then print the message that "Given No. is not present" otherwise insert node pointed by temp between nodes pointed by p and p -> link (p is used to traverse the list) Fig. 1.5. NULLn xtemp NULL p NULLn xtemp NULL p p ® link NULLn xtemp NULL p NULLn xtemp NULL p p ® link Figure 1.5 Deletion of a Node from Linked List Search the node, which is to be deleted from the list by traversing the list through pointer p. If end of List is found then print the message the 'Given No. is not found' otherwise store the address of node successor to the node to be deleted in the link field of p. Free the node to be deleted, Fig. 1.6 NULLnum Node to be deleted NULLnum Node to be deleted Figure 1.6 Concatenation of Linked Lists Consider a case where we have two Linked Lists pointed to by two different pointers, say p and q respectively, and we want to concatenate 2nd list at the end of the first list. We can do it by traversing first list till the end and then store the address of first node in the second list, in the link field of last node of first list. Suppose we are traversing first list by pointer temp, then we can concatenate the list by the statement(Fig. 1.7) temp -> link = q;
  • 5. For More Visit: Https://www.ThesisScientist.com NULL (a) NULL Temp ® (b) NULLq ® p ® q ® p ® NULL (a) NULL Temp ® (b) NULLq ® p ® q ® p ® Figure 1.7 (a) Lists before Concatenation, (b) List after Concatenation Applications of Linked List Linked Stack Stack is a very common and popular linear data structure. Since it is a linear data structure it can be represented through Array and linked list. We have studied in previous chapters the Array representation of stacks. It has some drawbacks, major one being fixed size. We have to cope with memory wastage and conditions of overflow while implementing stacks through array. In this section we implement stacks through linked list. Linked list uses dynamic memory allocation. Hence, it overcomes the drawbacks of array representation of stack. In stack we maintain a pointer 'top' of struct node type. All the insertions and deletions are made on one end, which is to be pointed by 'top' as in Fig. 1.8. NULL top ® NULL top ® Figure 1.8: Stack Functions for insertion (push operation) and deletion (pop operation) are given below:
  • 6. For More Visit: Https://www.ThesisScientist.com Linked Queue Like stack, queue is also very popular linear Data structure and can be represented by linked List. We have to maintain two pointers 'front and rear' of struct node type Fig 15.9. We have to increment 'rear pointer' on insertion and increment 'front' on deletion from the Queue. NULL rearfont NULL rearfont Figure 1.9: Linked Queue Polynomial Representation Polynomials in general are represented by the following equation: f(x) = cixei + ci-1xei-1 +....+c1xe1 where Ci are non zero coefficients of variable x And ei are exponents such that ei > ei-1 > ei-2 >.........> e1 > 0. For example : F1(x) = 3x7 -7x5 +3x2 +1 F2(x) =5x8 +6x5 +4x2 +13 These Polynomials can be added to form another Polynomial Let f3(x) = f1(x) +f2(x) f3(x) = 5x8 + 3x7 –x5 + 7x2 +14 This is achieved by adding coefficients of variables with same exponent value. These Polynomials can be maintained using a Linked List. To achieve this each term will be represented by a node and each node should consist of three elements namely coefficients, exponents and a link to the next term (represented by another node) For instance, the polynomial f(x)= 4x11 -3x9 +2x3 +1 will be stored as : ii) iii) 4 11 -3 9 2 3 1 0 NULL f (x) 4 11 -3 9 2 3 1 0 NULL f (x) 5 8z Now exp (p) > exp (q) Hence-4 55 8z Now exp (p) > exp (q) Hence-4 5 5 8z -4 5-4 5 9 35 8z -4 5-4 5 9 3 Coef. Exp. LinkCoef. Exp. Link
  • 7. For More Visit: Https://www.ThesisScientist.com iv) v) Doubly Linked Lists In the single linked list each node provides information about where the next node is in the list. It faces difficulty if we are pointing to a specific node, then we can move only in the direction of the links. It has no idea about where the previous node lies in memory. The only way to find the node which precedes that specific node is to start back at the beginning of the list. The same problem arises when one wishes to delete an arbitrary node from a single linked list. Since in order to easily delete an arbitrary node one must know the preceding node. This problem can be avoided by using Doubly Linked List, we can store in each node not only the address of next node but also the address of the previous node in the linked list. A node in Doubly Linked List has three fields Fig 1.10.  Data  Left Link  Right Link L LINK DATA R LINKL LINK DATA R LINK Figure 1.10: Node of Doubly Linked List Left link keeps the address of previous node and Right Link keeps the address of next node. Doubly Linked List has following property figure 15.11. p=p->llink->rlink=p->rlink->llink. L LINK R LINK p L LINK R LINKL LINK R LINKL LINK R LINK p Figure 15.11 This formula reflects the essential virtue of this structure, namely, that one can go back and forth with equal ease. Circular Linked List Circular Linked List is another remedy for the drawbacks of the Single Linked List besides Doubly Linked List. A slight change to the structure of a linear list is made to convert it to circular linked list; link field in the last node contains a pointer back to the first node rather than a NULL. See Figure 1.12 Figure 1.12: Circular Linked List 5 8z -4 5-4 5 9 3 7 2 Now p exhorted Hence, 5 8z -4 5-4 5 9 3 7 25 8z -4 5-4 5 9 3 7 2 Now p exhorted Hence, 5 8z -4 5 9 3 7 2 2 15 8z -4 5 9 3 7 2 2 1