0% found this document useful (0 votes)
11 views54 pages

Unit 3 Linked List

Uploaded by

srinath RD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views54 pages

Unit 3 Linked List

Uploaded by

srinath RD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

 Consider the following example:

 #include<stdio.h>
 main( )
 {
 int num_array[50],i;
 for( i=0; i < 50; i++ )
 {
 num_array[i]=0;
 scanf( "%d",&num_array[i] );
 fflush(stdin);
 }
 }
 When this program is compiled, the compiler
estimates the amount of memory required for
the variables, and also the instructions defined
by you as part of the program.
 The compiler writes this information into the
header of the executable file that it creates.
When the executable is loaded into memory at
runtime, the specified amount of memory is set
aside.
 A part of the memory allocated to the program
is in an area of memory called a runtime stack
or the call stack. Once the size of the stack is
fixed, it cannot be changed dynamically.
 Therefore, arrays present the classic problem of
the programmer having allocated too few
elements in the array at the time of writing the
program, and then finding at run time that more
values are required to be stored in the array
than what had originally been defined.
 The other extreme is of the programmer
allocating too many elements in the array and
then finding at run time that not many values
need to be stored in the array thereby resulting
in wastage of precious memory.
 Unlike an array, a linked list does not store
its nodes in consecutive memory locations
 Array manipulation (in terms of insertion
and deletion of elements from the array) is
more complex and tedious, and a better
alternative to all this is to go for dynamic
data structures.
 A linked list does not allow random access
of data. Nodes in a linked list can be
accessed only in a sequential manner
 is a run-time concept
 period of time during which a variable has
memory space associated with it
 begins when space is allocated
 ends when space is de-allocated

 three categories of "lifetime"


 static - start to end of program execution
 automatic (stack) - start to end of declaring function's
execution
 heap -variable declared dynamic at runtime, and also
de-allocated dynamically at runtime.
static data
space for global variables

automatic data
run-time stack - activation records
added and removed as program runs
(expands and shrinks in an orderly
LIFO manner)

space for variables allocated at run-


time (allocation and de-allocation
heap data requests occur in unpredictable order)
 Space for heap variables is allocated from an
area of runtime memory known as the heap or
free store.
 Heap variables do not have an explicit name,
and are accessed indirectly via a pointer.
 Memory space for heap variables is explicitly
allocated at runtime using malloc( ).
 Space occupied by heap variables must be
explicitly returned back to the heap to avoid
memory leaks. This is done using the free( )
function.
 Rather than pre-define array on the stack at
compile time, the alternative should be to define
a structure type, and dynamically declare at
runtime as many instances of the structure
variables as needed by the application.
 When the code containing the structure type is
compiled, what the compiler sees is only a
structure type declaration. It therefore, does not
allocate memory for the structure type since no
variable has been defined based on the
structure type.
 When the program begins execution, it will
need to create variables of the structure type.
Therefore, the language must support runtime
declaration of variables.
 The problem with these variables is that they
cannot be accommodated into the stack, as they
were not declared at the time of compilation,
and the stack would have been sized based on
the stack variables already declared at compile-
time.
 The C language provides the malloc() function
which a program can use to declare variables
dynamically.
 The parameter to malloc( ) is an unsigned
integer which represents the number of bytes
that the programmer has requested malloc( ) to
allocate on the heap.
 A more effective way of passing the number of
bytes to malloc( ) would be to pass the structure
type along with the sizeof( ) operator to malloc(
).
 The sizeof( ) operator can be used to
determine the size of any data type in C, instead
of manually determining the size and using that
value. Therefore, the benefit of using sizeof( )
operator in any program makes it portable.
Consider the following example:
#include<stdio.h>
main()
{
struct marks_data
{
char name[11];
int marks;
};
struct marks_data *ptr;
/* declaration of a stack variable */
ptr = (struct marks_data *)
malloc(sizeof(structmarks_data));
/* declaration of a block of memory on the heap and the
block in turn being referenced by ptr */
}
Stack Heap

ptr 100

100

Name Marks
 The malloc( ) function also returns the
starting address to the marks_data structure
variable on the heap.
 However, malloc( ) returns this not as a
pointer to a structure variable of type
marks_data , but as a void pointer.
 Therefore, the cast operator was used on the
return value of malloc( ) to cast it as a pointer
to a structure of type marks_data before
being assigned to ptr, which has accordingly
been defined to be a pointer to a structure of
type marks_data.
 Suppose, you have been given a task to store a
list of marks. The size of the list is not known.

 If it were known, then it would have facilitated


the creation of an array of the said number of
elements and have the marks entered into it.

 Elements of an array are contiguously located,


and therefore, array manipulation is easy using
an integer variable as a subscript, or using
pointer arithmetic.
 However, when runtime variables of a
particular type are declared on the heap, let's
say a structure type in which we are going to
store the marks, each variable of the structure
type marks will be located at a different
memory location on the heap, and not
contiguously located.
 Therefore, these variables cannot be
processed the way arrays are processed, i.e.,
using a subscript, or using pointer arithmetic.
 An answer to this is a self-referential structure.
 A self-referential structure is so defined that one
of the elements of the structure variable is able
to reference another subsequent structure
variable of the same type, wherever it may be
located on the heap.
 In other words, each variable maintains a link to
another variable of the same type, thus forming
a non-contiguous, loosely linked data structure.
 This self-referential data structure is also called
a linked list.
 Let us define a self-referential structure to store a list
of marks the size of which may not be known.
struct marks_list
{
int marks;
struct marks_list *next;
};
 We have defined a structure of type marks_list. It
consists of two elements, one integer element marks
and the other element, a pointer next, which is a
pointer to a structure of the same type, i.e., of type
marks_list itself.
Its derived, Linear and dynamic data
structure.
It’s a chain of dynamically allocated nodes.

Where each node contains

Information Link
1000 2748
20 NULL
8120 40 NULL
Start
NULL
5412

5412

10 NULL
1000
8120
30 NULL
2748
5412 1000 8120 2748
Start

NULL
5412 10 NULL
1000 20 NULL
8120 30 NULL
2748 40 NULL

typedef struct list


{
int info;
struct list *link;
} node;

node *start = NULL;


/* typedef struct list * NODE;
NODE start = NULL; */
 Inserting a newnode at front
Start
8120
2210
8120 1020 7000
10 1020 20 7000 30 NULL

void insert_front( )
{
2210 node *newnode;
newnode = (node*)malloc(sizeof(node));
40 NULL
8120
newnode->link = start;
printf("Enter the value:");
Newnode
scanf("%d",&newnode->info);
start = newnode;
}
 Inserting a new node at rear
Start
8120
8120 1020 7000
10 1020 20 7000 30 NULL
2210

void insert_end( )
{
node *newnode, *temp=start;
newnode=(node*) malloc (sizeof(node)); 2210
newnode->link=NULL;
if(start==NULL) 40 NULL
start=newnode;
else
{ Newnode
while(temp->link!=NULL)
temp=temp->link;
temp->link = newnode;
}
}
Start
8120
1020
8120 1020 7000
10 1020 20 7000 30 NULL

8120
Temp void delete_front()
{
node *temp=start;
if(start != NULL)
{
start=start->link;
printf("Deleted node is %d\n",temp->info);
free(temp);
}
}
void display()
{
node *temp;
for(temp=start;temp!=NULL;temp=temp->link)
printf("%d\t",temp->info);
printf("\n");
}

int main( )
{
int choice;
printf("1-Insert_front, 2-insert_end, 3-delete_front\n");
printf("4-delete_rear, 5-display,6-exit\n");
while(1)
{
printf("Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert_front( ); break;
case 2: insert_end( ); break;
case 3: delete_front( ); break;
// case 4: delete_rear( ); break;
case 5: display( ); break;
case 6: exit(0);
}
}
}
Start
8120
8120 1020 7000
10 1020 20 NULL
7000 30 NULL

void delete_rear()
{
node *temp=start,*prev;
if(start!=NULL)
{
if(start->link==NULL)
start=NULL;
else
{
for(prev=temp; temp->link!=NULL; temp=temp->link)
prev=temp;
prev->link=NULL;
}
printf("Deleted node is %d\n", temp->info);
free(temp);
}
}
 Insert
• to left of Given node value
• to the right of given node vale
• into ordered list
• based on the position specified

 Delete
• the node with the node value specified
• the node to the left of node value
• the node to the right of node value
• the node based on position

 Search based on node value and update


 Stack using Linked List
• PUSH – Insert at front
• POP - Delete at front

 Queue using Linked List


• Insert – Insert at rear
• Delete – Delete from front

 For Double ended queue?


 For Priority Queue?
 Reversing the list?
 Representing List as long Integers?
 Representing List for polynomials?
 Toovercome the drawback of SLL
 DLL can perform traversal from both side
start
8120 1000 2712
8120
NULL

NULL
1000

8120

2712

1000
100 200 300

Operation performed on typedef struct Dlist


DLL is similar to SLL {
operations int info;
struct Dlist *llink, *rlink;
} Dnode;

Dnode *start = NULL;


void insert_front( )
{
node *newnode;
newnode = (node*)malloc(sizeof(node));
newnode->llink= NULL;
newnode->rlink = start;
start->llink = newnode;
printf("Enter the value:");
scanf("%d",&newnode->info);
start = newnode;
}
ptr=start; ptr-> rlink = new_node;
while(ptr-> rlink != NULL) new_node-> llink = ptr;
ptr = ptr-> rlink; new_node-> rlink = NULL;
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start-> rlink;
start-> llink = NULL;
free(ptr);
return start;
}
ptr = start;
while(ptr-> rlink != NULL)
ptr = ptr-> rlink;
ptr-> llink-> rlink = NULL; ptr1=ptr->llink;
free(ptr); ptr1->rlink=NULL:
 Singly Linked Circular List
5412 1000 8120 2748
Start

NULL
5412 10 NULL
1000 20 NULL
8120 30 NULL
2748 40 5412

 Doubly Linked Circular List


start
8120 1000 2712
8120
1000

8120

2712

1000
2712

8120
100 200 300
Polynomial and Sparse Matrix Representation:

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.

Example:
10x + 26x, here 10 and 26 are coefficients and 2, 1
2

is its exponential value.


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

 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
A sparse matrix is a matrix where a
large proportion of the elements have a
value of zero.
 In a linked list representation of such
a matrix, zero value elements are not
represented, saving space.
Each node will belong to exactly two
singly linked lists: a row linked list and a
column linked list.
Represntation of Sparse Matrix Using Linked List
In linked list, each node has four fields. These four fields are defined
as:
•Row: Index of row, where non-zero element is located
•Column: Index of column, where non-zero element is located
•Value: Value of the non zero element located at index – (row,column)
•Next node: Address of the next node

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy