0% found this document useful (0 votes)
1 views11 pages

Linear Single Linked Lists

The document provides an overview of linked lists, detailing their structure, advantages, and disadvantages compared to arrays. It explains operations such as creation, insertion, deletion, searching, sorting, merging, splitting, and reversing linked lists, along with example C code for each operation. Additionally, it highlights applications of linked lists, particularly in polynomial operations.

Uploaded by

kiritoarka26
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)
1 views11 pages

Linear Single Linked Lists

The document provides an overview of linked lists, detailing their structure, advantages, and disadvantages compared to arrays. It explains operations such as creation, insertion, deletion, searching, sorting, merging, splitting, and reversing linked lists, along with example C code for each operation. Additionally, it highlights applications of linked lists, particularly in polynomial operations.

Uploaded by

kiritoarka26
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/ 11

Linked Lists

 Linked List can be defined as collection of objects called nodes that are randomly stored in the
memory.
 A node contains two fields i.e. data stored at that particular address and the pointer which contains
the address of the next node in the memory.
 The last node of the list contains pointer to the null.

Uses of Linked List


 The list is not required to be contiguously present in the memory. The node can reside anywhere in
the memory and linked together to make a list. This achieves optimized utilization of space.
 List size is limited to the memory size and doesn't need to be declared in advance.
 Empty node cannot be present in the linked list.
 We can store values of primitive types or objects in the singly linked list.

Why use linked list over array?


Till now, we were using array data structure to organize the group of elements that are to be stored
individually in the memory. However, Array has several advantages and disadvantages which must be
known in order to decide the data structure which will be used throughout the program.
Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand the
size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting any
element in the array needs shifting of all its predecessors.

Linked list is the data structure which can overcome all the limitations of an array. Using linked list is
useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.

Each node divided into two parts: the first part contains data field and the 2 nd part called the „Link‟ filed
contains the address of the next node in the list. Such an address, which is used to access a particular
node, is known as a pointer. The entire linked list is accessed from an external pointer that points to
(contains the address of) the first node in the list. (By an “external” pointer, we mean one that is not
included within the node. Rather its value can be accessed directly by referencing a variable). Internal
pointer on the other hand is a pointer within the node. „next‟ is the internal pointer in the next example.

Data Structure & Algorithm, Dr. K. Bhowal


Advantage of Linked lists:
Linked lists have many advantages. Some of the very important advantages are:
1. Linked lists are dynamic data structures. That is, they can grow during the execution of a program.
2. Efficient memory utilization. Here, memory is not pre-allocated. Memory is allocated whenever it
is required. And it is de-allocated when it is no longer needed. (In case of Array, a good amount of
space is wasted)
3. Insertion and deletions are easier and efficient, linked lists provide flexibility in inserting a data
item at a specified position and deletion of a data item from the given position. (In case of Array,
right or left shifting is necessary for insertion and deletion of an element, which increase the time
complexity)
4. Many complex applications can be easily carried out with linked lists.

Disadvantage of Linked lists:


1. Extra memory is needed to store the address of the next or previous nodes. It is directly
proportional to the number of nodes in a list.
2. Access to an arbitrary data item is little bit cumbersome and also time consuming.

Representation of Linear Linked List:


Suppose we want to store list of integer numbers, then the linked list can be represents in memory with
the following declarations:
struct link
{
int data;
struct link *next;
};
typedef struct link node;
node *head;

The above declaration defines a new data type, whose each element is of type node type.
data next

Operation Linked List:


1. Creation of a Linked list
2. Display elements of a list
3. Insertion of a new node in the exiting linked list
4. Deletion of a new node
5. Searching an element in the linked list
6. Sorting a linked list
7. Merging or concatenate of two linked lists
8. Splitting a linked list into two linked lists
9. Reverse a linked list

Application of Linked List:


1. Polynomial Addition
2. Polynomial Multiplication

Data Structure & Algorithm, Dr. K. Bhowal


1. Creation of Linked list
The following C recursive function creates a linked list where each node contains an integer number:
void create (node *list)
{
char ch;
printf(“\n Enter data:”);
scanf(“%d”, &list->data);
printf(“\n Another Node(y / n)?:”);
fflush stddin);
ch=getch();
if(ch==‟n‟ || ch==‟N‟)
{
list->next=NULL;
return;
}
list->next=(node*)malloc(sizof(node));
create(list->next);
}

2. Display elements of a list


void print(node *list)
{
printf(“%d->”,list->data);
if(list->next==NULL)
return;

print(list->next);
}

3. Insertion of a new node in the existing linked list


To insert an element into the following three things should be done:
1. Allocating a new node.
2. Assigning the data for new node.
3. Adjusting the pointers.

a) Before the First Node


1. If the list is empty then create a new node.
2. If the list is not empty, allocate memory for the new1 node and assign the value to the data field of
the new1node.
3. Then set the new1->next field to point to the head node.
4. Make the new1 node as a head node as head=new1;

C code:
node *insert_b4first(node *head)
{ node *new1;
new1=(node *)malloc(sizeof(node));
printf("\n Enter data for the new node");
scanf("%d",&new1->data);
new1->next=head;
return(new1);
}

b) After Last node: (Algorithm)


1. If the list is empty then create a new node.
2. If the list is not empty, allocate memory for the new node and assign the value to the data field of
the new node.
Data Structure & Algorithm, Dr. K. Bhowal
3. Then go to the last node (say, currently last node is list) and then set the list->next field to point to
the new node.
4. Make the linked field of the new node to point to NULL;

void insert_after_last(node *list)


{ node *new1;
new1=(node *)malloc(sizeof(node));
printf("\n Enter data for the new node");
scanf("%d",&new1->data);
while(list->next!=NULL)
list=list->next;

list->next=new1;
new1->next=NULL;
}

c) After a Specified Node: (Algorithm)


Suppose we want to insert a new node after list node:
1. Allocate memory for the new node.
2. Assign value to the data field of the new node.
3. Go to the list node and then make the linked field of the new node to point to
list->next node.
4. Make the linked field of the list node to point to the new node.

Program:
node *insert(node *list)
{
int key, item;
node *new1,*n1;
printf(“\ n Enter key element after which you want to insert:”);
scanf(“%d”,&key);
n1=finda(list, key);
if(n1= =NULL)
printf(“\n key is not found”);
else
{ new1=(node *)malloc(sizeof(node));
printf(“\ n Enter new element to insert:”);
scanf(“%d”,&item);
new1->data=item;
new1->next=n1->next;
n1->next=new1;
}
return(list);
}

node *finda(node *l1,int key)


{
while(l1!=NULL)
{ if(l1->data= =key)
return(l1);
l1=l1->next;
}
return(NULL);
}

Data Structure & Algorithm, Dr. K. Bhowal


Insertion of a new node before any node:
a) Before head: (Algorithm)
1. Allocate memory for the new node.
2. Assign the value to the data field of the new node.
3. Make the linked field of the new node to point to the starting node of
the liked list.
4. Then, set the external pointer (which was pointing to the starting node)
to point to the new node.
b) Before the last node:
Exercise
c) Before a specified node:
Exercise
Program:
node *insertb(node *list)
{
int key,item;
node *new1,*n1;
printf(“\ n Enter key element before which you want to insert:”);
scanf(“%d”,&key);
new1=(node *)malloc(sizeof(node));
printf(“\n Enter new element to insert:”);
scanf(“%d”,&item);
new1->data=item;

if(key= =list->data)
{ new1->next=list;
list=new1;
return(list);
}
else
{ n1=find b(list,key);
if(n1= =NULL)
printf(“\ n key is not found”);
else
{ new1->next=n1->next;
n1->next=new1;
}
}
return(list);
}
node *find b(node *l1,int key)
{
while(l1!=NULL)
{
if(l1->next->data= =key)
return(l1);
l1=l1->next;
}
return(NULL);
}

4. Deletion of node
Deletion of a node from linked list has the following three instances:
a) Deletion of head
b) Deletion of the last node
c) Deletion of a specified node.

Data Structure & Algorithm, Dr. K. Bhowal


In order to delete a node from the list, it is necessary to search for location of deletion. The steps involved
in deleting the node from the linked list are as follows:
1. If the linked list is empty then display the message “Deletion is not possible”.
2. If the node to be deleted is the first node (pointed to by head pointer) then set the pointer head to
point to the second node in the listed list.
3. If the node to be deleted is the last node, then go on locating the last but one node and set its link
field to point to UNLL pointer.
4. If the situation is other than the above three, then delete the node from specified position within
the linked list.

a) Deletion of head (Algorithm) :


1. If the list is not empty, then check whether the element below to the first node of the list.
2. Move the head pointer to the head->next node (head=head>next).
3. Free the first node.

b) Deletion of the node (Algorithm) :


Exercise
c) Deletion of a specified node (Algorithm) :
Exercise
Program:
node *del(node *list)
{ int key,item;
node *1,*n1;
printf(“\n Enter the key which you want to delete:”);
scanf(“%d”,&key);
if(key==list->data)
{ 1=list; list=list->next;
free(1);
return (list);
}
else
{ n1=find b(list,key);
if(n1==NULL)
printf(“\n Node with given data value does not exist”);
else
{ 1=n1->next;
n1->next=n1->next->next;
free(1);
}
}
return(list);
}

5. Searching an element in the linked list:


void search(node *list)
{ int key;
node *n1;
n1=list;
printf(“\n Enter key element to search:”);
scanf(“%d”,&key);
while(n1!=NULL)
{
if(n1->data==key)
{
printf(”\n Search is successful”);
Data Structure & Algorithm, Dr. K. Bhowal
return;
}
n1=n1->next;
}
printf(“\n Element does not exist”);
return;
}

6. Sorting a linked list:


void sort(node *list)
{ int temp;
node *11,*12;
11-12=list;
for(;11->next->data!=0;11->next)
{
12=list;
for(;12->next->data!=0;12=12->next)
{
if(12->data>12->next->data)
{
temp =12->data;
12->data=12->next->data;
12->next->data=temp;
}
}
}
print(head);
}

7. Merging or concatenate of two linked lists:


Suppose we have two linked lists with starting nodes named list1, list2 respectively. We have to merge
these two linked lists to single linked list.

void merge()
{
node *11, *list1,*list2;
list1=(node *)malloc(sizeof(nodel));
list2=(node *)malloc (sizof(node));
printf(“\n Enter data for List:”);
create(list);
printf(“\n Enter data for list2:”);
create(list2);
11=list;
while(11->next!=NULL)
11=11->next;
11->next=list2;
print(list);
}

8. Splitting a linked list into two inked lists:


void spilt(node *list)
{
int key;
node *head1,*head2;
printf(“\nEnter key for 2nd head:”);
scanf(“%d”,&key);
head1=list;
while (list->next->data!=key)
list=list->next;
head2=list->next;
Data Structure & Algorithm, Dr. K. Bhowal
list->next=NULL;
printf(“\n The first List:”);
print(head2);
}

9. Reverse a linked list:


node * reverse(node *h)
{
node *c,*p;
p = NULL;
c = h;
while(h != NULL) {
h=h->next;
c -> next = p;
p = c;
c = h;
}
return p;
}

Complete Program of Single Linked List:


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct link


{
int data;
struct link *next;
}node;

char ch;
void create(node *);
void print (node *);
node *insert(node *);
node *inserta(node *);
node *inserta(node *);
node *finda(node *,int);
node *findb(node *,int);
node *del(node *);

void main()
{
node *head;
node *list,*new1,*n1;
char ans;
int opt, item;
while(1)
{
printf(“\n Press 1 to create a Linear Link List:”);
printf(“\n Press 2 to display Link List:”);
print(“\n Press 3 to insert a new node”);
print(“\n Press 4 to delete a node”);
print(“\n Press 5 to exit”);
print(“\n Choose your choice:”);
scanf(“%d”,&opt);
switch(opt)
{
case 1:
head=(node *)malloc(sizeof(node));
create(head);

Data Structure & Algorithm, Dr. K. Bhowal


break;
case 2:
print(head);
break;
case 3:
head=insert(head);
printf(“\n After insertion list is:\n”);
print(head);
break;
case 4:
head=del(head);
printf(“\n After Deletion the list is:”);
print(head);
break;
case 5: exit();
default: printf(“\ n Your choice out of range”);
}
printf(“\n Continue(y/n?:”);
and=getch();
if(and==‟n‟)
break;
}
}

node *insert(node *list)


{
char ch,ans;
while(1)
{ printf(“\n Press „a‟ for after insertion”);
printf(“\m press „b‟ for before insertion”);
fflush(stdin);
ch=getch();
if(ch==‟a‟)
{
list=inserta(list);
return(list);
}
else
{
if(ch==‟b‟)
{
list=insertb(list);
return(list);
}
else
{
printf(“\n Not a valid cghoice”);
printf(“\n Do you wnat to try again(y / n)?:”);
ans=getch();
if(ans==‟n‟)
return (NULL);
}
}
}
}

/* Linear Link List Application*/


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct link
{
Data Structure & Algorithm, Dr. K. Bhowal
int data;
struct link *next;
}node;

typedef struct poly


{
int cof;
int power;
struct poly *next;
}pnode;

node *head, *head1, *head2;


void create(node *);
void print(node *);
void sort(node *);
void printpoly(pnode *);
void search(node *);
int count(node *);
void merge();
void split(node *);
void reverse(node *);
void polynomialadd();
char ch;
void main()
{
node *list,new1,*n1;
char ans;
int opt, item, cnt=0;
while(1)
{
printf(“\n Press 1 to create a Linear Link List:”);
printf(“\n Press 2 to display Link List:”);
printf(“\n Press 3 to search an elem,ent”);
printf(“\n Press 4 to count number of nodes”);
printf(“\n Press 5 to sort a link list”);
printf(“\n Press 6 to merge two link lists”);
printf(“\n Press 7 to split a link list”);
printf(“\n Press 8 to reverse a link list”);
printf(“\n Press 9 to add two polynomial”);
printf(“\n Press 10 to exit”);
printf(“\n Choose your choice:”);
scanf(“%d”,&opt);
switch(opt)
{
case 1:
head =(node *)malloc(sizeof(hode));
create(head);
break;
case 2:
print(head);
break;
case 3:
search(head);
break;
case 4:
cnt=count(head);
printf(“\n Number of Nodes = %d”,cnt);
break;
case 5:
sort(head);
printf(“\n After sorting the list is:\ n”);
print(head);
Data Structure & Algorithm, Dr. K. Bhowal
break;
case 6:
merge();
break;
case 7;
split(head);
break;
case 8:
reverse(head);
break;
case 9:
polynomialadd();
break;
case 10: exit();
default: printf(“\n Your choice out of range”);
}

}
}

int count(node *list)


{
static int cnt=0;
if(list==NULL)
return(cnt);
cnt++;
count(list->next);
}

Advantage & Disadvantage of Single Linked Lists:

Advantages:
1. Accessibility of a node in the forward direction is easier.
2. Insertion and deletion of nodes are easier.

Disadvantages:
1. Accessing the preceding node of a current node is not possible as there is no backward
traversal.
2. Accessing a node is time-consuming.

Data Structure & Algorithm, Dr. K. Bhowal

You might also like