Linked List Operation in C

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

Department of Computer Science and Engineering

Experiment No. : 03
Experiment Name: Implementation of Linked List Operations in C
Course Code : CSE135
Course Tittle : Data Structure Lab
Semester : Fall-2023

Submitted To: Submitted By:


Mr. Md. Mubtasim Fuad Tomal Ahmed Sajib
Lecturer, Dept. of CSE ID: 221-15-5406
Daffodil International University. Section: 61_U

Submission Date: 04 September,2023


Implementation of Linked List Operations in C

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node;

void printer(Node* temp){


while(temp != NULL){
printf("%d->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* InsertAtBegin(Node*temp,int data){
Node* firstNode = (Node*)malloc(sizeof(Node));
firstNode->data = data;
firstNode->next = temp;
return firstNode;
}
void insertAtEnd(Node* temp,int data){
Node* lastNode=(Node*)malloc(sizeof(Node));
lastNode->data = data;
lastNode->next=NULL;

while(temp->next != NULL){
temp = temp->next;
}
temp->next = lastNode;
}
void insertAtMid(Node* head, int data) {
int position;
printf("Enter the position: ");
scanf("%d", &position);

Node* newNode = (Node*)malloc(sizeof(Node));


newNode->data = data;

if (position <= 0) {
printf("Error: Invalid position!\n");
return;
}
if (position == 1) {
newNode->next = head->next;
head->next = newNode;
return;
}
Node* current = head;
int currentPosition = 1;

while (current != NULL && currentPosition < position - 1) {


current = current->next;
currentPosition++;
}
if (current == NULL) {
printf("Error: Position exceeds the length!\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
int main()
{
Node* head=NULL;
Node *first = (Node*)malloc(sizeof(Node));
Node *second = (Node*)malloc(sizeof(Node));
Node *third = (Node*)malloc(sizeof(Node));

first->data = 1;
first->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;

head = first;
printer(head);

insertAtEnd(head,4);
printer(head);

head=InsertAtBegin(head,0);
printer(head);

int data;
printf("To insert an element in middle:\n");
printf("Enter the data:");
scanf("%d", &data);

insertAtMid(head,data);
printer(head);

return 0;
}
Output:

Discussion:

In this experiment, we have implemented a Linked List operation in C. Through this


operation, we have inserted elements at begin, end and in the middle of a linked list.
Firstly, a structure called ‘Node’ is defined which represents a node in the singly
linked list. Each node contains an integer data element (int data) and a pointer to the
next node (struct Node* next). The printer function is used for traversing and
printing the elements of the linked list from the beginning to the end. The
insertAtBegin function is used to insert elements at the beginning of the linked list. It
creates a new node, sets its data and makes it pointed to the head of the list. The
insertAtEnd function inserts a new node with the specified data at the end of the
linked list. It iterates through the list to find the last node and adds the new node
after it. The insertAtMid function is used to insert elements in the middle of a linked
list. This function receives two parameters from main function. In main function,
prompt takes the data which will be inserted in the middle of the linked list. After
receiving the parameters from main function, a new node is created using malloc.
The user-specified data is assigned to the data field of this new node. Then the
function takes the position from the user to insert data. After that, the function will
check the given position whether it is invalid or not. If the position is 0 or exists the
length, the function will a print an error message. If it is 1, it will insert the data at
the first position. Otherwise, the function enters a loop to find the node just before
the expected position; if it is found, the data will be inserted at the specific position.
Other functions are called and passes the parameter from Main function to the
specific functions. After each calling, printer function is called and print the result of
our program. Thus, we’ve implemented linked list operation in our program.

------------------------------------End-------------------------------

You might also like