PROJECT REPORT (Singly Linked List)
PROJECT REPORT (Singly Linked List)
PROJECT REPORT (Singly Linked List)
On
Submitted by
KISSAN KUMAR-190101120092
1
CENTURION UNIVERSITY OF TECHNOLOGY & MANAGEMENT
PARALAKHEMUNDI, ODISHA
SCHOOL OF ENGINEERING & TECHNOLOGY
CERTIFICATE
This is certify that the minor project entitled “singly linked list” is a bonafide
record of independent work done by Mr. Kishan Kumar (190101120092) under
my supervision and guidance, submitted to CENTURION UNIVERSITY
OFTECHNOLOGY & MANAGEMENT,PARALAKHEMUNDI in partial
fulfillment for the award of the Degree of Bachelor of Technology in Computer
Science and Engineering.
Certified that the above mentioned project has been duly carried out as per the
norms of the college and statutes of the university
2
EVALUATION SHEET
▪ Result:APPROVED/REJECTED
3
CANDIDATE’S DECLARATION
4
ACKNOWLEDGEMENT
We wish to express our profound and sincere gratitude to Mr. Abinas Panda,
Department of Computer Science Engineering, SoET, Paralakhemundi, who guided us
in the intricacies of this project nonchalantly with matchless magnanimity.
We are highly grateful to all those, who directly and indirectly helped us and who
evinced keen interest and invaluable support in the progress and successful completion
of our project work.
Kishan kumar
(190101120092)
5
INDEX
Sl.no Page no. Signature
9. Search 27-30
11. Exit
6
Singly Linked List
Singly linked lists contain nodes which have a data part as well as an address
part i.e. next, which points to the next node in the sequence of nodes.
The operations we can perform on singly linked lists are insertion, deletion
and traversal.
• Singly linked list is probably the most easiest data structure to implement.
• Insertion and deletion of element can be done easily.
• Insertion and deletion of elements doesn't requires movement of all elements when compared to an array.
• Requires less memory when compared to doubly, circular or doubly circular linked list.
• Can allocate or deallocate memory easily when required during its execution.
• It is one of most efficient data structure to implement when traversing in one direction is required.
7
Algorithm for singly linked list:
Unless otherwise stated, we assume that a typical element or node consists of two fields namely; an
information field called INFO and pointer field denoted by LINK. The name of a typical element
is denoted by NODE
Node
// c Structure to presented a node struct
info link node
{
Int info
Data Pointer to next node struct node*link
};
Function : INSERT( X, First )
X is new element and FIRST is a pointer to the first element of a linked linear list then this function
inserts X. Avail is a pointer to the top element of the availability stack; NEW is a temporary pointer
variable. It is required that X precedes the node whose address is given by FIRST.
8
Function: INSEND( X, First ) (Insert at end)
A new element is X and FIRST is a pointer to the first element of a linked linear list then this function
inserts X. AVAIL is a pointer to the top element of the availability stack; NEW and SAVE are temporary
pointer variables. It is required that X be inserted at the end of the list.
9
Function : INSORD( X, FIRST )
• There are many applications where it is desirable to maintain an ordered linear list. The ordering
is in increasing or decreasing order on INFO field. Such ordering results in more efficient
processing.
• The general algorithm for inserting a node into an ordered linear list is as below.
1. Remove a node from availability stack.
2. Set the field of new node.
3. If the linked list is empty then return the address of new node.
4. If node precedes all other nodes in the list then inserts a node at the front of the list and
returnsits address.
5. Repeat step 6 while information contain of the node in the list is less than the
informationcontent of the new node.
6. Obtain the next node in the linked list.
7. Insert the new node in the list and return address of its first node.
• A new element is X and FIRST is a pointer to the first element of a linked linear list then this
function inserts X. AVAIL is a pointer to the top element of the availability stack; NEW and SAVE
are temporary points variables. It is required that X be inserted so that it preserves the ordering
of the terms in increasing order of their INFO field.
3. [Does the new node precede all other node in the list?]
If INFO(NEW) ≤ INFO
(FIRST)then LINK (NEW)
FIRST
return (NEW)
10
By repeatedly involving function INSORD, we can easily obtains an ordered liner list for example
thesequence of statements.
FRONT NULL
FRONT INSORD (29,
FRONT) FRONT INSORD
(10, FRONT) FRONT
INSORD (25, FRONT)FRONT
INSORD (40, FRONT)
FRONT INSORD (37,
FRONT)
FRONT 29
FRONT 10 29
FRONT 10 25 29
FRONT 10 25 29 40
FRONT 10 25 29 37 40
11
1. [Is Empty list?]
If FIRST = NULL
then write
(‘Underflow’)
return
3. [Find X]
Repeat thru step‐5 while SAVE ≠ X and LINK (SAVE) ≠ NULL
7. [Delete X]
If X = FIRST (if X is first
node?)then FIRST LINK
(FIRST)
else LINK (PRED) LINK (X)
12
1. Set link of the last node in the new list to null and return.
2. [Copy first
node]NEW
NODE New
AVAIL
AVAIL LINK (AVAIL)
FIELD (NEW) INFO
(FIRST)BEGIN NEW
3. [Initialize traversal]
SAVE FIRST
6. [Copy node]
If AVAIL = NULL
then write (‘Availability stack
underflow’)return (0)
else NEW AVAIL
AVAIL LINK (AVAIL)
FIELD (NEW) INFO
(SAVE)PTR (PRED)
NEW
13
Insertion in singly linked list in begining
Code:
#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
O/p:
14
Insert at last
Code:
#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
lastinsert(item);
printf("\nPress 0 to insert more ?\n");
15
scanf("%d",&choice);
}while(choice == 0);
}
void lastinsert(int item)
{
struct node *ptr = (struct node*)malloc(sizeof(struct node));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
o/p:
16
Insert at any random location
Code:
#include<stdio.h>
#include<cstdlib>
//Create a structure for NODE
struct node
{
int data;
struct node *link;
};
struct node *header, *ptr, *temp;
void insert_any();
int main()
{
int choice;
int cont = 1;
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void begdelete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...");
}
}
O/P:
19
Delete from last
Code:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void end_delete();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\n1.Append List\n2.Delete node\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
20
scanf("%d",&item);
create(item);
break;
case 2:
end_delete();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void end_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\n Deleted Node from the last ...");
}
21
}
O/P:
22
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
delete_specified();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void delete_specified()
{
struct node *ptr, *ptr1;
int loc,i;
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nThere are less than %d elements in the list..\n",loc);
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted %d node ",loc);
}
23
O/p:
24
6. {
7. int data;
8. struct node *next;
9. };
10. struct node *head;
11. void main ()
12. {
13. int choice,item;
14. do
15. {
16. printf("\n1.Append List\n2.Traverse\n3.Exit\n4.Enter your choice?");
17. scanf("%d",&choice);
18. switch(choice)
19. {
20. case 1:
21. printf("\nEnter the item\n");
22. scanf("%d",&item);
23. create(item);
24. break;
25. case 2:
26. traverse();
27. break;
28. case 3:
29. exit(0);
30. break;
31. default:
32. printf("\nPlease enter valid choice\n");
33. }
34.
35. }while(choice != 3);
36. }
37. void create(int item)
38. {
39. struct node *ptr = (struct node *)malloc(sizeof(struct node *));
40. if(ptr == NULL)
25
41. {
42. printf("\nOVERFLOW\n");
43. }
44. else
45. {
46. ptr->data = item;
47. ptr->next = head;
48. head = ptr;
49. printf("\nNode inserted\n");
50. }
51.
52. }
53. void traverse()
54. {
55. struct node *ptr;
56. ptr = head;
57. if(ptr == NULL)
58. {
59. printf("Empty list..");
60. }
61. else
62. {
63. printf("printing values . . . . .\n");
64. while (ptr!=NULL)
65. {
66. printf("\n%d",ptr->data);
67. ptr = ptr -> next;
68. }
69. }
70. }
26
Searching:
Code:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void search();
27
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\n1.Create\n2.Search\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
search();
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
28
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
O/p:
29
Show:
Code:
#include <stdio.h>
30
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
display();
return 0;
}
31
O/P:
struct node {
int value;
struct node *next;
};
int main() {
typedef struct node DATA_NODE;
while (loop) {
if (data >= 0) {
temp_node->value = data;
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
head_node = temp_node;
fflush(stdin);
} else {
loop = 0;
32
temp_node->next = 0;
}
}
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
return 0;
}
O/p:
`
Simple Singly Linked List Program Using functions:
Code:
#include <stdio.h>
33
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert(int);
void display();
int main() {
int loop = 1;
int data;
first_node = 0;
while (loop) {
printf("\nEnter Element for Insert Linked List (-1 to Exit ) : \n");
scanf("%d", &data);
if (data >= 0) {
insert(data);
} else {
loop = 0;
temp_node->next = 0;
}
}
display();
return 0;
}
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
head_node = temp_node;
fflush(stdin);
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d", count);
}
O/P:
35
Singly Linked List program:
Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
int main() {
int option = 0;
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
36
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node->value = data;
if (first_node == 0) {
37
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
38
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
} else
printf("\nInvalid Position \n\n");
}
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
39
O/P:
40