0% found this document useful (0 votes)
12 views16 pages

lab4

The document outlines the implementation of a singly linked list using structures, pointers, and dynamic memory allocation in C programming. It details various operations such as insertion and deletion at different positions, displaying all data, and searching for data within the linked list. The conclusion emphasizes the importance of linked lists in data structures and their applications in real-world scenarios.

Uploaded by

Himal Bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

lab4

The document outlines the implementation of a singly linked list using structures, pointers, and dynamic memory allocation in C programming. It details various operations such as insertion and deletion at different positions, displaying all data, and searching for data within the linked list. The conclusion emphasizes the importance of linked lists in data structures and their applications in real-world scenarios.

Uploaded by

Himal Bhattarai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

TITLE: TO IMPLEMENT VARIOUS OPERATIONS IN

SINGLY LINKED LIST USING ARRAY

Introduction
In this lab, we will implement a singly linked list using a structure and pointer and dynamic
memory allocation and perform various operations such as insert and deletion at beginning, at
last and at specific position, display all data, and search data in linked list.

THEORY

A singly linked list is a fundamental data structure, it consists of nodes where each node
contains a data field and a reference to the next node in the linked list. The next of the last
node is null, indicating the end of the list. Linked Lists support efficient insertion and
deletion operations.
In a singly linked list, each node consists of two parts: data and a pointer to the next node.
This structure allows nodes to be dynamically linked together, forming a chain-like sequence.
The operations which are performed in singly linked list is as follows:

1. The getNode function:


we can define a function getNode() to allocate the memory for a node dynamically. It is user-
defined function that return a pointer to the newly created node.
Nodetype *getNode()
{
NodeType *p; p==(NodeType*)malloc(sizeof(NodeType)); return(p);
}
2. Creating the empty list:
void createEmptyList(NodeType *head)
{
head=NULL;
}
3. Insert at beginning:
We can use the following steps to insert a new node at beginning of the single linked list...
1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set newNode→next = NULL and head = newNode.
4. If it is Not Empty then, set newNode→next = head and head = newNode.

4. Insert at last:
We can use the following steps to insert a new node at end of the single linked list...
1. Create a newNode with given value and newNode → next as NULL.
2. Check whether list is Empty (head == NULL).
3. If it is Empty then, set head = newNode.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the last node in the list (until
temp → next is equal to NULL).
6. Set temp → next = newNode.

5. Insert at specific position :


We can use the following steps to insert a new node after a node in the single linked list...
1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set newNode → next = NULL and head = newNode.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the node after which we want to
insert the newNode (until temp1 → data is equal to location, here location is the node
value after which we want to insert the newNode).
6. Every time check whether temp is reached to last node or not. If it is reached to last node
then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate
the function. Otherwise move the temp to next node.
7. Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

6. Delete at beginning
We can use the following steps to delete a node from beginning of the single linked list...
1. Check whether list is Empty (head == NULL)
2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
3. If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
4. Check whether list is having only one node (temp → next == NULL)
5. If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
6. If it is FALSE then set head = temp → next, and delete temp.

7. Delete at last
We can use the following steps to delete a node from end of the single linked list...
1. Check whether list is Empty (head == NULL)
2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
3. If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
4. Check whether list has only one Node (temp1 → next == NULL)
5. If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
6. If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the
same until it reaches to the last node in the list. (until temp1 → next == NULL)
7. Finally, Set temp2 → next = NULL and delete temp1.

8. Delete at specific position


We can use the following steps to delete a specific node from the single linked list..
1. Check whether list is Empty (head == NULL)
2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
3. If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
4. Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node.
And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
5. If it is reached to the last node then display 'Given node not found in the list! Deletion not
possible!!!'. And terminate the function.
6. If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
7. If list has only one node and that is the node to be deleted, then set head = NULL and
delete temp1 (free(temp1)).
8. If list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 = = head).

TASK PERFORMED
Write a program to implement singly linked list usinga structure and pointer and dynamic
memory allocation in c programming.

SOURCE CODE
#include<stdio.h>
#include<stdlib.h>
void deleteal();
int a,b,c,d,num;
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node * createnode(int data){
struct node *newnode=(struct node*)malloc(sizeof(struct node)); //Creating a newnode
if(newnode==NULL){
printf("The newnode is not created");
}
else{
newnode->data=data;
newnode->next=NULL;
num++;
return newnode;
}
};
void menu(){
printf("The operations and there number is as follows:\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Display\n");
printf("4) Search\n");
printf("5) exit\n");
printf("The menu function is called\n");
}
void menu1
printf("The operations and there number is as follows:\n");
printf("1) Insert at begining\n");
printf("2) Insert at last\n");
printf("3) Insert at specific position\n");
printf("4) Go to main menu\n");
}
void menu2(){
printf("The operations and there number is as follows:\n");
printf("1) Delete at begining\n");
printf("2) Delete at last\n");
printf("3) Delete at specific position\n");
printf("4) Go to main menu\n");
}
void insertab(){
struct node *temp;
struct node *p;
int l;
printf("Enter the data for new node");
scanf("%d",&l);
p=createnode(l);
if(head==NULL){
head=p;
}
else{
p->next=head;
head=p;
}
}
void display(){
int i=1;
printf("The data is :\n");
struct node *temp;
temp=head;
while(temp!=NULL){
printf("%d) %d\n",i,temp->data);
temp=temp->next;
i++;
}
}
void insertal(){
struct node *temp1;
struct node *p;
temp1=head;
int l;
printf("Enter the data for new node");
scanf("%d",&l);
p=createnode(l);
if(head==NULL){
head=p;
}
else{
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=p;
}
}
void insertas(){
struct node *temp2;
struct node *p;
int s,i;
temp2=head;
int l;
printf("Enter the position for new node");
scanf("%d",&s);
if(s==1){
printf("Perform insertion at begining\n");
insertab();
}
else if(s==num){
printf("Perform insertion at last\n");
insertal();
}
else{ //insert at specific position
printf("Enter the data for new node");
scanf("%d",&l);
p=createnode(l);
temp2=head;
for(i=1;i<s-1;i++){
temp2=temp2->next;
}
p->next=temp2->next;
temp2->next=p;
}
}
void deleteab(){
struct node *temp3;
if(head==NULL){
printf("The linked list is already empty\n");
}
else if(head->next==NULL){
printf("Data that is deleted is %d",head->data);
head=NULL;
num--;
}
else{
temp3=head;
head=temp3->next;
temp3->next=NULL;
printf("Data that is deleted is %d",temp3->data);
free(temp3);
num--;
}
}
void search(){
struct node *temp8;
int e,i=1,f=0,g;
printf("Enter the number you want to search :");
scanf("%d",&e);
temp8=head;
while(temp8!=NULL){
if(e==temp8->data){
printf("The data has been found in %d position\n",i);
f++;
}
temp8=temp8->next;
i++;
}
if(f==0){
printf("The data has not been found \n");
}
}
void deleteas(){
struct node *temp6;
struct node *temp7;
int s,i;
printf("Enter the position for delete operation");
scanf("%d",&s);
if(s==0){
printf("Perform deletion at begining\n");
deleteab();
}
else if(s==num){
printf("Perform deletion at last\n");
deleteal();
}
else{
temp6=head;
for(i=1;i<s-1;i++){
temp6=temp6->next;
}
temp7=temp6->next;
temp6->next=temp7->next;
printf("Data that is deleted is %d",temp7->data);
free(temp7);
num--;
}
}
void deleteal(){
struct node *temp4;
struct node *temp5;
if(head==NULL){
printf("The linked list is already empty\n");
}
else if(head->next==NULL){
head=NULL;
num--;
}
else{
temp4=head;
while(temp4->next->next!=NULL){
temp4=temp4->next;
}
temp5=temp4->next;
temp4->next=NULL;
printf("Data that is deleted is %d",temp5->data);
free(temp5);
num--;
}
}
int main(){
while(1){
menu();
printf("Enter the operation number :");
scanf("%d",&a);
switch(a){
case 1:menu1();
printf("Enter the operation number:\n");
scanf("%d",&b);
switch(b){
while(1){
case 1:insertab();
break;
case 2:insertal();
break;
case 3:insertas();
break;
case 4:break;
default:printf("Enter the valid number\n\n\n");
}
}
break;
case 2:menu2();
printf("Enter the operation number:\n");
scanf("%d",&c);
switch(c){
while(1){
case 1:deleteab();
break;
case 2:deleteal();
break;
case 3:deleteas();
break;
case 4:break;
default:printf("Enter the valid number\n\n\n");
}
}
break;
case 3:display();
break;
case 4:search();
break;
case 5:printf(" Thank you!\n");
exit(0);
break;
default:printf("Enter the valid number\n\n\n");
}
}
return 0;
}

OUTPUT
Menu() for all function to performing all operation

Menu() for all function to performing insertion

Menu() for all function to performing deletion

Insert at beginning

Insert at last
Insert at specific position

Delete at beginning

Delete at last

Delete at specific position

Display all data


Search for 5 and 6

DISCUSSION
Firstly we will make three menu() function in which first will be to display all the operation
that is going to perform and other for specifying the position of delete and insert operation.
We will make a function name createnode() which will create a node using malloc function
and using a pointer. After this we will create a insertion functions for insert at beginning ,
insert at last and insert at specific position . For all insert and delete we will check for empty
and single node in deletion and insertion.
For insert at beginning we are going to perform p->next=head and head=p; to make a new
node as head for at last we will simply traverse upto last node and after that we will add
newnode after last node by pointing last node next to new node. For specific position
insertion we should traverse upto the second last node of that position and adjust the pointer .
firstly, new node will point to the temp next and temp next will point new node.
For deletion at beginning we are going to put the node to be delete in a pointer and after that
we will point head to its next and delete using free memory. For deletion at last we will
traverse upto second last node and put last node in pointer and make the second last nodes
next will be null and free last node. For specific position, we will traverse before that position
and adjust pointer and delete node using free function.
For searching we will compare data while traversing from head to last node .

For display we will traverse from head to last and displaying data of temp.

CONCLUSION

Through this lab, we were able to know the use of linked list . It is used to
implement stacks and queues which are like fundamental needs
throughout . To prevent the collision between the data in the hash map,
we use a singly linked list. If we ever noticed the functioning of a casual
notepad, it also uses a singly linked list to perform undo or redo or
deleting functions.We can think of its use in a photo viewer for having look
at photos continuously in a slide show.In the system of train, the idea is
like a singly linked list, as if you want to add a Boggie, either you have to
take a new boggie to add at last or you must spot a place in between
boggies and add it.

And the linked list provide us the dynamic data structures feature which doesnot have any
limitation in storing but it depends in the memory or hardware of computer. This is quiet
complex due to its requirement of pointer as well as malloc function uses which makes it
complex to manage the data.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy