Unit 3 Linked List
Unit 3 Linked List
#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
automatic data
run-time stack - activation records
added and removed as program runs
(expands and shrinks in an orderly
LIFO manner)
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.
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
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
NULL
1000
8120
2712
1000
100 200 300
NULL
5412 10 NULL
1000 20 NULL
8120 30 NULL
2748 40 5412
8120
2712
1000
2712
8120
100 200 300
Polynomial and Sparse Matrix Representation:
Example:
10x + 26x, here 10 and 26 are coefficients and 2, 1
2