PROJECT REPORT (Singly Linked List)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

PROJECT REPORT

On

singly linked list


Submitted to the Department of Computer Science & Engineering in
Fulfilment of the SUBJECT PROJECT
th
requirement under 5 Semester

Submitted by

KISSAN KUMAR-190101120092

Under the Guidance of


ABINAS PANDA SIR
Assistant Professor
Department of Computer Science & Engineering

SCHOOL OF ENGINEERING & TECHNOLOGY


CENTURION UNIVERSITY OF TECHNOLOGY & MANAGEMENT
PARALAKHEMUNDI, ODISHA

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.

Mr. Abinas Panda


CSE, SoET, CUTM

Certified that the above mentioned project has been duly carried out as per the
norms of the college and statutes of the university

Signature of Internal Signature of


External

2
EVALUATION SHEET

Title of the project: singly linked list


▪ Year of submission:2021
▪ Subject: Data Structure Using C++
▪ Name of the Department: COMPUTER SCIENCE
▪ Date of examination /viva:
▪ Student Name with regd. No:

NAME REG. NUMBER


Kishan Kumar 190101120092

▪ Name of the Guide / Subject Teacher:


Mr. Abinas Panda(Assistant Professor.)

▪ Result:APPROVED/REJECTED

Signature of Internal Signature of


External

3
CANDIDATE’S DECLARATION

I am Mr. Kishan Kumar (190101120092) of Centurion


University of Technology and Management, Paralakhemundi,
hereby declare that the project report entitled “singly linked
list” is
an original work and data provided in the study is authentic one.

Mr. Kishan kumar


Signature of Student(S)

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

1. Algorithm for singly linked list 7-12

2. Insert in begining 13-14

3. Insert at last 14-15

4. Insert at any random location 16-17

5. Delete from Beginning 17-19

6. . Delete from last 19-21

7. Delete the node after the given data 21-24

8. Traversing in singly linked list 24-27

9. Search 27-30

10.. Show 30-32

11. Exit

12. Simple Singly linked list 32-33

13. Simple Singly Linked List 33-35


Program Using functions
14. Singly Linked List program 33-35

15. Singly Linked List program: 36-40

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.

Advantages of Singly linked list


There are several points about singly linked list that makes it an important data structure.

• 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.

Disadvantages of Singly linked list


After seeing the advantages of singly linked list. Singly linked list also has some disadvantages

over other da ta structures.

• It uses more memory when compared to an array.


• Since elements are not stored sequentially hence requires more time to access each elements of list.
• Traversing in reverse is not possible in case of Singly linked list when compared to Doubly linked list.
• Requires O(n) time on appending a new node to end. Which is relatively very high when compared to array or
other linked list.

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.

1 [Create New Empty Node]


NEW  NODE

1. [Initialize fields of new node and its link to the list]


INFO (NEW)  X
LINK (NEW) 
FIRST

2. [Return address of new node]


return (NEW)

When INSERT is invoked it returns a pointer value to the variable

FIRSTFIRST  INSERT (X, 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.

1. [Create New Empty Node]


NEW  NODE

2. [Initialize field of NEW node]


INFO (NEW) 
X LINK (NEW) 
NULL

3. [Is the list empty?]


If FIRST = NULL
then return (NEW)

4. [Initialize search for a last node]


SAVE  FIRST

5. [Search for end of list]


Repeat while LINK (SAVE) ≠
NULLSAVE  LINK
(SAVE)

6. [Set link field of last node to NEW)


LINK (SAVE)  NEW

7. [Return first node pointer]


return (FIRST)

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.

1. [Create New Empty Node]


NEW  NODE

2. [Is the list is empty]


If FIRST = NULL
then LINK (NEW)  NULL
return (NEW)

3. [Does the new node precede all other node in the list?]
If INFO(NEW) ≤ INFO
(FIRST)then LINK (NEW) 
FIRST
return (NEW)

4. [Initialize temporary pointer]


SAVE  FIRST

5. [Search for predecessor of new node]


Repeat while LINK (SAVE) ≠ NULL and INFO (NEW) ≥ INFO (LINK (SAVE))
SAVE  LINK (SAVE)

6. [Set link field of NEW node and its predecessor]


LINK (NEW)  LINK
(SAVE)LINK (SAVE) 
NEW

7. [Return first node pointer]


return (FIRST)

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

Trace of construction of an ordered linked linear list using function


INSORD

Procedure : DELETE( X, FIRST)


• Algorithm that deletes node from a linked linear list:‐
1. If a linked list is empty, then write under flow and return.
2. Repeat step 3 while end of the list has not been reached and the node has not been found.
3. Obtain the next node in list and record its predecessor node.
4. If the end of the list has been reached then write node not found and return.
5. Delete the node from list.
6. Return the node into availability area.
• A new element is X and FIRST is a pointer to the first element of a linked linear list then this
procedure deletes the node whose address is given by X. SAVE is used to find the desired node,
and PRED keeps track of the predecessor of TEMP. Note that FIRST is changed only when X is the
first element of the list.

11
1. [Is Empty list?]
If FIRST = NULL
then write
(‘Underflow’)
return

2. [Initialize search for X]


SAVE  FIRST

3. [Find X]
Repeat thru step‐5 while SAVE ≠ X and LINK (SAVE) ≠ NULL

4. [Update predecessor marker]


PRED  SAVE

5. [Move to next node]


SAVE  LINK (SAVE)

6. [End of the list]


If SAVE ≠ X
then write (‘Node not
found’)return

7. [Delete X]
If X = FIRST (if X is first
node?)then FIRST  LINK
(FIRST)
else LINK (PRED)  LINK (X)

8. [Free Deleted Node]


Free (X)

Function COPY (FIRST)


• FIRST is a pointer to the first node in the linked list, this function makes a copy of the list.
• The new list is to contain nodes whose information and pointer fields are denoted by FIELD and
PTR,respectively. The address of the first node in the newly created list is to be placed in BEGIN.
NEW, SAVE and PRED are points variables.
• A general algorithm to copy a linked list
1. If the list is empty then return null
2. If the availability stack is empty then write availability stack underflow and return else copy
thefirst node.
3. Report thru step 5 while the old list has not been reached.
4. Obtain next node in old list and record its predecessor node.
5. If availability stack is empty then write availability stack underflow and return else copy
thenode and add it to the rear of new list.

12
1. Set link of the last node in the new list to null and return.

1. [Is Empty List?]


If FIRST = NULL
then return (NULL)

2. [Copy first
node]NEW 
NODE New 
AVAIL
AVAIL  LINK (AVAIL)
FIELD (NEW)  INFO
(FIRST)BEGIN  NEW

3. [Initialize traversal]
SAVE  FIRST

4. [Move the next node if not at the end if list]


Repeat thru step 6 while (SAVE) ≠ NULL

5. [Update predecessor and save pointer]


PRED  NEW
SAVE  LINK (SAVE)

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

7. [Set link of last node and return]


PTR (NEW)  NULL
return (BEGIN)

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;

header = (struct node *) malloc(sizeof(struct node));


header->data = NULL;
header->link = NULL;
insert_any();
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ", ptr->data);
}
return 0;
}
void insert_any()
{
int data_value, key;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
printf("\nEnter data of the node after which new node is to be inserted: ");
scanf("%d", &key);
temp = (struct node *) malloc(sizeof(struct node));
ptr = header;
while(ptr->link != NULL && ptr->data != key)
{
ptr = ptr->link;
}
if(ptr->data == key)
{
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
else
17
{
printf("\nValue %d not found\n",key);
}
}
O/P:

Delete from Beginning


Code:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void begdelete();
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");
scanf("%d",&item);
create(item);
18
break;
case 2:
begdelete();
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 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:

Delete the node after the given data


Code:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void delete_specified();
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);

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:

Traversing in singly linked list


Code:
1. #include<stdio.h>
2. #include<stdlib.h>
3. void create(int);
4. void traverse();
5. struct node

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:

Simple Singly linked list:


Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

struct node {
int value;
struct node *next;
};

int main() {
typedef struct node DATA_NODE;

DATA_NODE *head_node, *first_node, *temp_node = 0;


int count = 0;
int loop = 1;
first_node = 0;
int data;

printf("Singly(Single) Linked List Example - Basic\n");

while (loop) {

printf("\nEnter Element for Insert Linked List (-1 to Exit ) : \n");


scanf("%d", &data);

if (data >= 0) {

temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));

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;
}

printf("\nNo Of Items In Linked List : %d", count);

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();

typedef struct node DATA_NODE;

DATA_NODE *head_node, *first_node, *temp_node = 0;

int main() {
int loop = 1;
int data;
first_node = 0;

printf("Singly(Single) Linked List Example - Using Functions\n");

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;
}

void insert(int data) {


temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
34
temp_node->value = data;

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();

typedef struct node DATA_NODE;

DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;


int data;

int main() {
int option = 0;

printf("Singly Linked List Example - All Operations\n");

while (option < 5) {

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 = (DATA_NODE *) malloc(sizeof (DATA_NODE));

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");

printf("\nEnter Position for Delete Element : \n");


scanf("%d", &pos);

if (pos > 0 && pos <= countvalue) {


if (pos == 1) {
temp_node = temp_node -> next;
first_node = temp_node;
printf("\nDeleted Successfully \n\n");
} else {
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if(i == (countvalue - 1))
{
head_node = prev_node;
}
printf("\nDeleted Successfully \n\n");
break;
} else {

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

You might also like