0% found this document useful (0 votes)
31 views8 pages

Applications of Artificial Intelligence

Uploaded by

sanjuomg8
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)
31 views8 pages

Applications of Artificial Intelligence

Uploaded by

sanjuomg8
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/ 8

SINGLY LINKED LISTS

Singly linked list can be defined as the collection of ordered set of


elements. The number of elements may vary according to need of the
program. A node in the singly linked list consists of two parts: data part
and link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address
of its immediate successor.

One way chain or singly linked list can be traversed only in one direction.
In other words, we can say that each node contains only next pointer,
therefore we can’t traverse the list in the reverse direction.

Consider an example where the marks obtained by the student in three


subjects are stored in a linked list as shown in the figure.

In the above figure, the arrow represents the links. The data part of every
node contains the marks obtained by the student in the different subject.
The last node in the list is identified by the null pointer which is present in
the address part of the last node. We can have as many elements we
require, in the data part of the list.

COMPLEXITY:

Data Time Complexity Space


Structure complexity

Average Worst Worst

Acces Searc Insertio Deletio Acces Searc Insertio Deletio


s h n n s h n n

Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked
List
OPERATIONS ON SINGLY LINKED LIST:
There are various operations which can be performed on singly linked list.
A list of all such operations is given below.

NODE CREATION
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *) malloc(sizeof(struct node *));

INSERTION:
The insertion into a singly linked list can be performed at different
positions. Based on the position of the new node being inserted, the
insertion is categorized into the following categories.

S Operation Description
N

1 Insertion at It involves inserting any element at the front of the list. We just
beginning need to a few link adjustments to make the new node as the
head of the list.

2 Insertion at the It involves insertion at the last of the linked list. The new node
end of the list can be inserted as the only node in the list or it can be inserted
as the last one. Different logics are implemented in each
scenario.

3 Insertion after It involves insertion after the specified node of the linked list. We
specified node need to skip the desired number of nodes in order to reach the
node after which the new node will be inserted. .

DELETION AND TRAVERSING:


The Deletion of a node from a singly linked list can be performed at
different positions. Based on the position of the node being deleted, the
operation is categorized into the following categories.

SN Operation Description

1 Deletion at It involves deletion of a node from the beginning of the list. This is
beginning the simplest operation among all. It just need a few adjustments in
the node pointers.

2 Deletion at the It involves deleting the last node of the list. The list can either be
end of the list empty or full. Different logic is implemented for the different
scenarios.

3 Deletion after It involves deleting the node after the specified node in the list. we
specified node need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through
the list.

4 Traversing In traversing, we simply visit each node of the list at least once in
order to perform some specific operation on it, for example,
printing data part of each node present in the list.

5 Searching In searching, we match each element of the list with the given
element. If the element is found on any of the location then
location of that element is returned otherwise null is returned. .

SINGLY LINKED LIST PROGRAM:


#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the beginning


struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
newNode->next = head;
return newNode;
}

// Function to insert a node at the end


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
return newNode;
}

struct Node* current = head;


while (current->next != NULL) {
current = current->next;
}

current->next = newNode;
return head;
}

// Function to delete a node by value


struct Node* deleteNode(struct Node* head, int data) {
struct Node *current = head, *prev = NULL;

// Search for the node to be deleted


while (current != NULL && current->data != data) {
prev = current;
current = current->next;
}

// If the node is not found


if (current == NULL) {
printf("Node with data %d not found.\n", data);
return head;
}

// If the node is the first node


if (prev == NULL) {
head = current->next;
} else {
// If the node is in the middle or end
prev->next = current->next;
}

free(current);
return head;
}

// Function to display the linked list


void displayList(struct Node* head) {
printf("Linked List: ");
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

// Function to free memory allocated for the linked list


void freeList(struct Node* head) {
struct Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}

// Main function
int main() {
struct Node* head = NULL;
// Insert nodes at the beginning
head = insertAtBeginning(head, 3);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);

// Display the list


displayList(head);

// Insert nodes at the end


head = insertAtEnd(head, 4);
head = insertAtEnd(head, 5);

// Display the list


displayList(head);

// Delete a node
head = deleteNode(head, 3);

// Display the updated list


displayList(head);

// Free memory
freeList(head);

return 0;
}

OUTPUT:
Linked List: 1 -> 2 -> 3 -> NULL
Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Linked List: 1 -> 2 -> 4 -> 5 -> NULL

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