Professional Documents
Culture Documents
List is a ordered collections of objects. The linked list is a data structure which consists of a series
of structures, which are not necessarily adjacent in memory. Each structure contains the data and a pointer
to a structure containing its successor. We call this the next pointer. The last cell's next pointer points to
NULL
Arrays
In an array each node (element) follows the previous one physically (i.e. contiguous spaces in the
memory)
Arrays are fixed size: either too big (unused space ) or not big enough (overflow problem)
Maximum size of the array must be predicted which is sometimes not possible.
Inserting and deleting elements into an array is difficult. Have to do lot of data movement, if in array
of size 100, an element is to be inserted after the 10th lement, then all remaining 90 have to be shifted
down by one position.
Linked Lists
Linked lists are appropriate when the number of data elements to be represented in the data structure
are not known in advance.
Linked lists are dynamic, so the length of a list can increase or decrease as necessary.
A linked list is a collection of nodes, each node containing a data element.
Each node does not necessarily follow the previous one physically in the memory.
Nodes are scattered at random in memory.
Insertion and Deletion can be made in Linked lists, by just changing links of a few nodes, without
disturbing the rest of the list. This is the greatest advantage.
But getting to a particular node may take large number of operations.
Every node from start needs to be traversed to reach the particular node.
Types:
Single linked list
Circular linked list
Doubly linked list
Node structure:
A node in a linked list is a structure that has at least two fields. One of the fields is a data field; the
other is a pointer that contains the address of the next node in the sequence.
struct node
{
int data;
struct node *next;
}
The pointer variable next is called a link. Each structure is linked to a succeeding structure by way
of the field next. The pointer variable next contains an address of the location in memory of the successor
structnode element or the special value NULL.
Linked
list with actual pointer values
struct node
{
int number;
struct node * link;
};
struct student
{
char name[20];
int id;
double grdPts;
struct student *next_student;
};
3. A structure in a node:
struct person
{
char name[20];
char address[30];
char phone[10];
};
struct person_node
{
struct person data;
struct person_node *next;
};
• The head pointer addresses the first node of the list, and each node points at its successor node.
• The last node has a link value NULL.
Empty List:
1. Add a Node:
There are four steps to add a node to a linked list:
1. Allocate memory for the new node.
2. Determine the insertion point (you need to know only the new node’s predecessor)
3. Point the new node to its successor.
4. Point the predecessor to the new node.
To form linked list which contains two integers 39 and 60, first define a structure to hold two pieces
of information in each node- an integer value, and the address corresponding to the next structure of same
type.
struct node
{
int data;
struct node *next;
};
struct node *pNew, *pHead;
Use malloc to fetch one node from the memory, and let pNew point to this node.
At this moment there are no elements in the list. Pointer pHead points to NULL.
First element 39 is stored in node pNew. So make it the head node.
pNew->next = pHead;
/* set link to NULL*/
pHead = pNew;
/* point list to first node*/
Now we have got one node in the list, which is being pointed to by pHead. To put the next value
60 on the list, we must use malloc to fetch another node from the memory.
Now we need to link this new node to the pHead node, which can be done by following
statement:
pHead->next = pNew;
let us say we are interested in adding a node containing 90 at the end of this list. As a first step, use
malloc to get a new node pNew from the memory and load 90 in the data field and NULL in the next field.
pNew->data = 90;
pNew->next = NULL;
Now, the last node containing 80 would become the predecessor of the node containing
90 after the node is added. Let us call this node as pPre.
Use a temporary variable pPre and initialize it to pHead. Now hop through the nodes till you get a node
whose next link is NULL. Then pPre will be pointing to the last node.
pPre = pHead;
while ( pPre->next != NULL )
pPre = pPre->next;
pPre->next = pNew;
This would result in the node pNew to be attached to the list as the last node.
So far we have been discussing separate codes for inserting a node in the middle, at the end or at front of a
list. We can combine all the codes and write a general code for inserting a node anywhere in the list. Given
the head pointer (pHead), the predecessor (pPre) and the data to be inserted (item), we first allocate memory
for the new node (pNew) and adjust the link pointers.
struct node
{
int data;
struct node *next;
};
struct node *pHead;
pHead=NULL:
if (pHead == NULL)
{
/*Adding to empty list*/
pHead = pNew;
pHead->next= NULL;
}
else
{
/* Adding as first node*/
pNew->next = pHead;
pHead = pNew;
}
}
if (pHead == NULL)
{
/*Adding to empty list*/
pHead = pNew;
pHead->next= NULL;
}
else
{
/* Adding as last node*/
pTemp = pHead;
while(pTemp->next!=NULL)
pTemp = pTemp->next;
pTemp->next = pNew;
pNew->next = NULL;
}
}
{
pHead = pNew;
pHead->next= NULL;
}
else
{
pTemp = pHead; /* Searching insertion point*/
while(pTemp!=NULL)
{
if(pTemp->data == key)
break;
else
{
pPre = pTemp;
pTemp = pTemp->next;
}
}
if( pTemp==NULL)
printf(“ The insertion point can’t be found\n”);
else
{
if ( pTemp==pHead) // inserted before head node
{
pNew->next = pTemp;
pHead = pNew;
}
else // insert as intermediate or last node
{
pNew->next = pTemp->next;
pTemp->next = pNew;
}
}
}
}
2. Delete a node:
Deleting a node requires that we logically remove the node from the list by changing various link pointers
and then physically deleting the node from the heap.
We can delete
• the first node
• any node in the middle
• the end node
2. change the predecessor’s link field to point to successor of the current node.
if (pHead == NULL)
printf(“ Empty linked list\n”);;
else // Search node
{
pTemp = pHead;
while (pTemp!=NULL)
{
if(pTemp->data == key)
break;
else
{
pPre = pTemp
pTemp = pTemp->next;
}
}
if( pTemp==NULL)
printf(“Node not found\n”);
else
{
if ( pTemp == pHead )
pHead = pTemp->next;
else
pPre->next = pTemp->next;
free(pTemp)
printf(“Node deleted\n” );
}
}
}
/* Given the item and the pointer to the head of the list, the function returns a pointer to
the node which matches the item, or returns NULL if the item is not found
*/
Struct node *Search (int item, struct node *pHead)
{
struct node *pTemp;
int i=1;
pTemp=pHead;
if (pHead == NULL)
printf(“ Empty linked list\n”);;
else // Search node
{
pTemp = pHead;
while (pTemp!=NULL)
{
if(pTemp->data == key)
break;
else
{
pTemp = pTemp->next;
i = i+1;
}
}
if( pTemp==NULL)
printf(“Node can’t be found\n”);
else
printf(“Node found at location %d\n”, i );
}
return(pTemp)
}
#include"stdio.h"
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *next;
}*pHead;
switch(i)
{
case 1:
printf("ENTER THE NUMBER :-");
scanf("%d",&num);
insert_begin(num);
break;
case 2:
printf("ENTER THE NUMBER :-");
scanf("%d",&num);
insert_last(num);
break;
case 3:
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
printf("PLEASE ENTER THE LOCATION NUMBER :-");
scanf("%d",&loc);
insert_after(num,loc);
break;
case 4:
printf(" THE ELEMENTS IN THE LIST ARE : ");
display( );
break;
case 5:
printf(" Total No Of Elements In The List
are%d",count());
break;
case 6:
printf(" PLEASE ENTER A NUMBER to be deleted :");
scanf("%d",&num);
delete(num);
break;
case 7:
exit();
} /* end if switch */
} /* end of while */
} /* end of main */
if (pHead == NULL)
{
/*Adding to empty list*/
pHead = pNew;
pHead->next= NULL;
}
else
{
/* Adding as first node*/
pNew->next = pHead;
pHead = pNew;
}
}
if (pHead == NULL)
{
/*Adding to empty list*/
pHead = pNew;
pHead->next= NULL;
}
else
{
/* Adding as last node*/
pTemp = pHead;
while(pTemp->next!=NULL)
pTemp = pTemp->next;
pTemp->next = pNew;
pNew->next = NULL;
}
}
if (pHead == NULL)
printf(“ Empty linked list\n”);;
else // Search node
{
pTemp = pHead;
while (pTemp!=NULL)
{
if(pTemp->data == key)
break;
else
{
pPre = pTemp
pTemp = pTemp->next;
}
}
if( pTemp==NULL)
printf(“Node not found\n”);
else
{
if ( pTemp == pHead )
pHead = pTemp->next;
else
pPre->next = pTemp->next;
free(pTemp)
printf(“Node deleted\n” );
}
}
}
return;
}
}
{
pHead = pNew;
pHead->next= NULL;
}
else
{
pTemp = pHead; /* Searching insertion point*/
while(pTemp!=NULL)
{
if(pTemp->data == key)
pNew->next = pTemp->next;
pTemp->next = pNew
else
{
pPre = pTemp;
pTemp = pTemp->next;
}
}
if( pTemp==NULL)
printf(“ The insertion point can’t be found\n”);
else
{
if ( pTemp==pHead) // inserted before head node
{
pNew->next = pTemp;
pTemp->next = pNew;
}
else // insert as intermediate or last node
{
pNew->next = pTemp->next;
pTemp->next = pNew;
}
}
}
}
/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */
void display( )
{
struct node *pTemp;
pTemp=pHead;
if(pTemp ==NULL)
{
printf("NO ELEMENT IN THE LIST :");
return;
}
else /* traverse the entire linked list */
while(pTemp!=NULL)
{
printf("%d \n",r->data);
pTemp = pTemp ->next;
}
}
void count()
{
struct node *pTemp;
int c=0;
*pTemp=phead;
while(pTemp!=NULL)
{
pTemp = pTemp ->next;
c++;
}
return(c);
}
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int coeff;
int pow;
struct node *next;
};
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;
}
main()
{
char ch;
In a circularly linked list, all nodes are linked in a continuous circle, without using null.
Various operations that can be performed with circular linked list are
Creation of circular linked list
Insertion of a node in circular linked list
Deletion of any node from the list
Display of circular linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *next;
};
struct node *pHead;
void main()
{
int ch,n,m,key,i,cou
pHead = NULL;
while(1)
{
printf(" 1. CREATE A LIST \n”);
printf(" 2.INSERT A NUMBER AT BEGINNING;\n");
printf(" 3.INSERT A NUMBER AT LAST:\n");
printf(" 4.INSERT A NUMBER AFTER A PARTICULAR VALUE IN LIST:\n");
printf(" 5.DELETE A NODE IN THE LINKED LIST:\n");
printf(" 6.PRINT THE ELEMENTS IN THE LIST :\n");
printf(" 7.PRINT THE NUMBER OF ELEMENTS IN THE LIST\n");
printf(" 8.GET OUT OF LINKED LIST:\n");
printf(" PLEASE, ENTER THE NUMBER:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter no of items");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&m);
create(m);
}
break;
case 2:
printf("enter the element");
scanf("%d",&m);
insert_begin(m);
break;
case 3:
printf("enter the element");
scanf("%d",&m);
insert_last(m);
break;
case 4:
printf("enter the element");
scanf("%d",&m);
printf("enter the value after which you want to insert");
scanf("%d",&key);
insert_after(m,key);
break;
case 5:
printf("Enter the element to delete");
scanf("%d",&m);
delete(m);
break;
case 6:
display();
break;
case 7:
cou=count();
printf("No. of elements in the list%d ", cou);
break;
case 8:
exit(0);
break;
default:
printf("wrong choice");
}
}
}
if(pHead == NULL)
{
pHead = pNew;
pNew ->next= pHead;
}
else
{
pTemp = pHead;
while(pTemp->next!=pHead)
pTemp = pTemp->next;
pTemp->next = pNew;
pNew->next = pHead;
}
return;
}
if (pHead == NULL)
{
/*Adding to empty list*/
pHead = pNew;
pHead->next= pHead;
}
else
{
/* Adding as last node*/
pTemp = pHead;
while(pTemp->next!=pHead)
pTemp = pTemp->next;
pTemp->next = pNew;
pNew->next = pHead;
printf(“Node inserted\n”);
}
return;
}
if( pTemp==pHead)
printf(" The insertion point can’t be found\n”);
}
return;
}
}
else
{
pPre = pTemp;
pTemp = pTemp->next;
}
} while(pTemp!=pHead);
if( pTemp==pHead)
printf(" The deletion point can’t be found\n");
}
return;
}
Application of Circular Linked List:
Josephus' Problem:
This algorithm is named for a historian of the first century, Flavius Josephus, who survived the Jewish-
Roman war due to his mathematical talents. Legend has it that he was one out of 41 Jewish rebels trapped
by the Romans. His companions preferred suicide to escape, so they decided to form a cycle and to kill every
third person and to proceed around the circle until no one was left. Josephus wasn't excited by the idea of
killing himself, so he calculated where he has to stand to survive the vicious circle.
Algorithm:
There are n people standing in a circle waiting to be executed. After the first man is executed, k−1 people
are skipped and the k-th man is executed. Then again, k−1 people are skipped and the k-th man is executed.
The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people
are removed), until only the last man remains, who is given freedom. In Euclidean geometry, a circle is the
set of all points in a plane at a fixed distance, called the radius, from a fixed point, called the centre. ...
The task is to choose the place in the initial circle so that you survive (remain the last one), given n and k.
1 2 1 2 1 2
8 8
3 3 3
7 7 7
4
6 6 6
5 5 5
Delete(4) delete(8)
1 2 1
3 3 3
7 7 7
6 6 6
6 6
switch(ch)
{
case 1:
printf("\nEnter the total no. of soilders");
scanf("%d",&n);
create_list(n);
break;
case 2:
display();
getch();
break;
case 3:
if (tot_soilders <= 1)
printf("There Should Be Atleast 2 Soilders in the
List");
else
{
printf("\nEnter the no by which sucide is to be
commited");
scanf("%d",&n);
if ( n < 1 )
printf("\nInvalid Number!");
else
printf("\nThe only Soilder left after
sucide session is %d ",left_after_sucide(n));
}
getch();
break;
case 0:
return;
default :
printf("\nINVALID CHOICE");
getch();
}
}
}
create_list(int data)
{
struct node *last,*New;
int i;
head=NULL;
for(i=1;i<=data;i++)
{
New=(struct node *)malloc(sizeof(struct node));
New->soilder=i;
New->next=NULL;
tot_soilders=tot_soilders+1;
if(head==NULL)
{
head=New;
last=New;
New->next=head;
}
else
{
New->next=last->next;
last->next=New;
last = New;
}
}
}
display()
{
if (head == NULL)
{
printf("\nNo Soilders in the Queue");
return;
}
printf("%d",head->soilder);
printf("%c ",2);
current = head->next;
while( current != head)
{
printf("%d ",current->soilder);
current = current->next;
}
return;
}
int left_after_sucide(int by_n)
{
int i=1,j,dead_sol;
struct node *save;
current = head;
Doubly Linked List (DLL) is a type of Linked List in which each data item points to the
next and also to the previous data item. This "linking" is accomplished by keeping two
address variables (pointers) together with each data item. These pointers are used to store the
address of the previous and the address of the next data items in the list.
The structure that is used to store one element of a linked list is called a node like in Linked
Lists.
A node has three parts: data, previous address and next address.
The data part contains the necessary information about the items of the list, the previous
address part contains the address of the previous node, the next address part contains the
address of the next node.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int no;
struct node *pre,*next;
};
struct node *head=NULL,*tail=NULL;
void main()
{
int ch;
clrscr();
do
{
printf("\n\n\n 1 Append");
printf("\n 2 Forward Traverse");
printf("\n 3 Insert after the given number");
printf("\n 4 Insert before the given number");
printf("\n 5 Insert By Position ");
printf("\n 6 Delete By Value ");
printf("\n 7 Delete by Position");
printf("\n 0 EXIT");
printf("\n Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
Append();
break;
case 2:
ftraverse();
break;
case 3:
insertafter();
break;
case 4:
insertbefore();
break;
case 5:
insertbypos();
break;
case 6:
delByValue();
break;
case 7:
delByPos();
break;
case 0:
break;
default:
printf("\n Invalid Choice");
}
}while(ch!=0);
getch();
}
void insertbypos()
{
int val;
struct node *p =head,* pNew;
int non=0,i,pos;
if(head==NULL)
{
printf("\nEmpty LINK lIST");
return;
}
if (pos<1&&pos>non+1)
{
printf("\n Invalid Position");
return;
}
if(pos==1)
{
pNew =(struct node*)malloc(sizeof(struct node));
printf("\nENTER number");
scanf("%d",& pNew ->no);
head->pre= pNew;
pNew ->next=head;
head= pNew;
head->pre=NULL;
printf("\n Node inserted");
return;
}
if(pos==non+1)
{
pNew =(struct node*)malloc(sizeof(struct node));
printf("\nEnter number");
scanf("%d",& pNew ->no);
tail->next=n;
pNew ->pre=tail;
tail= pNew;
tail->next=NULL;
printf("\n Node inserted");
return;
}
p=head;
for(i=1;i<pos-1;i++)
{
p=p->next;
}
pNew =(struct node*)malloc(sizeof(struct node));
printf("\nENTER number");
scanf("%d",& pNew ->no);
pNew ->next=p->next;
pNew ->pre=p;
p->next= pNew;
pNew ->next->pre= pNew;
printf("\n NODE Inserted");
return;
}
}
while(p->next!=NULL)
{
if(val==p->no)
{
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
printf("\nNode Deleted");
}
p=p->next;
}
printf("\n Node not Found") ;
return;
}
if(head==tail&&pos==1)
{
free(p);
head=tail=NULL;
printf("\nNODE Deleted");
return;
}
else if (pos==1)
{
head=head->next;
head->pre=NULL;
free(p);
printf("\nNode DELeted");
return;
}
else if(pos==non)
{
p=tail;
tail=tail->pre;
tail->next=NULL;
free(p);
printf("\nNode DELeted");
return;
}
for(i=0;i<pos-1;i++)
{
p=p->next;
}
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
printf("\nNode Deleted");
return;
}
Application :
Palindrome checking using doubly linked list