0% found this document useful (0 votes)
3 views27 pages

Single Linked List

The document provides various C code snippets for creating and manipulating a singly linked list, including adding nodes at the beginning and end, displaying the list, inserting at specific positions, and deleting nodes from both the beginning and end. Each section includes function definitions and example outputs demonstrating the functionality of the linked list operations. The code is attributed to Swetanjali Maharana, an Assistant Professor.

Uploaded by

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

Single Linked List

The document provides various C code snippets for creating and manipulating a singly linked list, including adding nodes at the beginning and end, displaying the list, inserting at specific positions, and deleting nodes from both the beginning and end. Each section includes function definitions and example outputs demonstrating the functionality of the linked list operations. The code is attributed to Swetanjali Maharana, an Assistant Professor.

Uploaded by

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

Create a single linked list:

#include <stdio.h>
#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()
{

Swetanjali Maharana (Asst. prof)


addNode(100);
addNode(52);
addNode(38);
addNode(40);
display();
return 0;
}
Output:
Nodes of singly linked list:
100 52 38 40

Adding a new node at the beginning of a single linked list


#include <stdio.h>
#include <stdlib.h>
void beginsert(int item);
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");
Swetanjali Maharana (Asst. prof)
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
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");
}
}
int main()
{
addNode(20);
addNode(2);
addNode(36);
addNode(45);
display();
int item;
printf("\nEnter the item which you want to insert?\n");
scanf("%d", &item);
beginsert(item);
display();

Swetanjali Maharana (Asst. prof)


return 0;
}
Output:
Nodes of singly linked list:
20 2 36 45

Enter the item which you want to insert?


50
Node inserted
Nodes of singly linked list:
50 20 2 36 45

Adding a new node at the end of a single linked list


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

void addAtEnd(int data);


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

Swetanjali Maharana (Asst. prof)


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");
}
void addAtEnd(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;
}
}
int main()
{
addNode(20);
addNode(2);
addNode(36);
addNode(45);
display();
int data;
printf("\nEnter the item which you want to insert at end?\n");
scanf("%d", &data);
addAtEnd(data);

Swetanjali Maharana (Asst. prof)


display();
return 0;
}

Output:
Nodes of singly linked list:
20 2 36 45

Enter the item which you want to insert at end?


60
Nodes of singly linked list:
20 2 36 45 60

Create, Adding a new node at the end and beginning of a single


linked list
#include <stdio.h>
#include <stdlib.h>
void beginsert(int item);
void addAtEnd(int data);
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;
}

Swetanjali Maharana (Asst. prof)


}
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");
}
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");
}
}
void addAtEnd(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}

Swetanjali Maharana (Asst. prof)


else {
tail->next = newNode;
tail = newNode;
}
}
int main()
{
addNode(20);
addNode(2);
addNode(36);
addNode(45);
display();
int item;
printf("\nEnter the item which you want to insert at beginning?\n");
scanf("%d", &item);
beginsert(item);
display();
int data;
printf("\nEnter the item which you want to insert at end?\n");
scanf("%d", &data);
addAtEnd(data);
display();
return 0;
}
Output:
Nodes of singly linked list:
20 2 36 45

Enter the item which you want to insert at beginning?


80
Node inserted
Nodes of singly linked list:
80 20 2 36 45

Enter the item which you want to insert at end?


60
Nodes of singly linked list:

Swetanjali Maharana (Asst. prof)


80 20 2 36 45 60

Traversing a single linked list:


#include <stdio.h>
#include <stdlib.h>
void addAtEnd(int data);
void traverse();
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 traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
printf("printing values . . . . .\n");
while (ptr!=NULL)

Swetanjali Maharana (Asst. prof)


{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
int main()
{
addNode(20);
addNode(2);
addNode(36);
addNode(45);
traverse() ;
return 0;
}

Output:
printing values . . . . .

20
2
36
45

Insert a node at a specific position in a linked list


#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int size = 0;
struct Node* getNode(int data)
{
struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

Swetanjali Maharana (Asst. prof)


return newNode;
}
void insertAtPosition(struct Node* head, int pos, int data)
{
if (pos < 1 || pos > size + 1)
printf( "Invalid position!\n" );
else {
struct Node *curr=head;
for(int i=1;i<pos-1;i++)
curr=curr->next;
struct Node* temp=getNode(data);
temp->next=curr->next;
curr->next=temp;
if(pos=1)
head=temp;
size++;
}
}
void printList(struct Node* head)
{
while (head != NULL) {
printf("%d ",head->data);
head = head->next;
}
printf("\n");
}
int main()
{
struct Node* head = NULL;
head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(5);
size = 5;
printf("Linked list before insertion: ");
printList(head);

Swetanjali Maharana (Asst. prof)


int data, pos;
printf("\nEnter the item which you want to insert ?\n");
scanf("%d", &data);
printf("\nEnter the position\n");
scanf("%d", &pos);
insertAtPosition(head, pos, data);
printf("Linked list after insertion : ");
printList(head);
return 0;
}

Output:
Linked list before insertion: 1 2 3 4 5

Enter the item which you want to insert ?


20
Enter the position
2
Linked list after insertion: 1 20 2 3 4 5

Delete a node from the beginning of a single linked list


#include <stdio.h>
#include <stdlib.h>
void begdelete() ;
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;
}

Swetanjali Maharana (Asst. prof)


else {
tail->next = newNode;
tail = newNode;
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("\nNodes of singly linked list: ");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\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 ...");
}
}
int main()
{
addNode(20);
addNode(2);

Swetanjali Maharana (Asst. prof)


addNode(36);
addNode(45);
display();
begdelete();
display();
return 0;
}

Output:
Nodes of singly linked list: 20 2 36 45

Node deleted from the beginning...


Nodes of singly linked list: 2 36 45

Delete a node from the end of a single linked list


#include <stdio.h>
#include <stdlib.h>
void end_delete() ;
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;
}
}

Swetanjali Maharana (Asst. prof)


void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("\nNodes of singly linked list: ");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\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 ...");

Swetanjali Maharana (Asst. prof)


}
}
int main()
{
addNode(20);
addNode(2);
addNode(36);
addNode(45);
display();
end_delete();
display();
return 0;
}

Output:
Nodes of singly linked list: 20 2 36 45

Deleted Node from the last ...


Nodes of singly linked list: 20 2 36

Delete a node at a specific position in a linked list

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

void insert(int);
void display_List();
void delete (int);

struct node // Structure declaration


{
int data;
struct node* next;
}* head = NULL,*tail= NULL;
void delete (int pos)
{
struct node* temp = head;

Swetanjali Maharana (Asst. prof)


int i;
if (pos == 0) {
printf("\nElement deleted is : %d\n", temp->data);
head = head->next;
temp->next = NULL;
free(temp);
}
else {
for (i = 0; i < pos - 1; i++) {
temp = temp->next;
}
struct node* del= temp->next;
temp->next = temp->next->next;
printf("\nElement deleted is : %d\n", del->data);
del->next = NULL;
free(del);
}
printf("\nUpdated Linked List is : \n");
display_List();
return;
}
void insert(int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;
if (head == NULL)
{
head = newnode;
tail = newnode;
}
else
{
tail->next = newnode;
tail = newnode;
}

Swetanjali Maharana (Asst. prof)


return;
}
void display_List()
{
struct node* temp;
temp = head;
while (temp != NULL) {
if (temp->next == NULL) {
printf(" %d NULL", temp->data);
}
else {
printf(" %d", temp->data);
}
temp = temp->next;
}
printf("\n");
return;
}
int main()
{
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
insert(60);
printf("\n--Created Linked List--\n");
display_List();
int pos;
printf("\nEnter the position ");
scanf("%d",&pos);
delete (pos);
return 0;
}

Output:
--Created Linked List--

Swetanjali Maharana (Asst. prof)


10 20 30 40 50 60 NULL

Enter the position 2


Element deleted is : 30

Updated Linked List is :


10 20 40 50 60 NULL

Linked list implementation of stack


#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Stack Overflow! Memory allocation failed.\n");
return;
}
newNode->data = data;
newNode->next = top;
top = newNode;
printf("%d pushed onto stack.\n", data);
}
void pop() {
if (top == NULL) {
printf("Stack Underflow! No elements to pop.\n");
return;
}
struct Node* temp = top;
printf("%d popped from stack.\n", top->data);
top = top->next;
free(temp);
}

Swetanjali Maharana (Asst. prof)


void display() {
struct Node* temp = top;
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void peek() {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Top element is: %d\n", top->data);
}
int main() {
push(10);
push(20);
push(30);
push(40);
display();
pop();
display();
peek();
return 0;
}
Output:
10 pushed onto stack.
20 pushed onto stack.
30 pushed onto stack.
40 pushed onto stack.
Stack elements: 40 30 20 10

Swetanjali Maharana (Asst. prof)


40 popped from stack.
Stack elements: 30 20 10
Top element is: 30
Linked list implementation of Queue
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed. Queue is full.\n");
return;
}
newNode->data = data;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued into queue.\n", data);
}
void dequeue() {
if (front == NULL) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}
struct Node* temp = front;
printf("%d dequeued from queue.\n", front->data);
front = front->next;
if (front == NULL) {

Swetanjali Maharana (Asst. prof)


rear = NULL;
}
free(temp);
}
void display() {
struct Node* temp = front;
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void peek() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Front element is: %d\n", front->data);
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
display();
dequeue();
display();
peek();
return 0;
}
Output:
10 enqueued into queue.

Swetanjali Maharana (Asst. prof)


20 enqueued into queue.
30 enqueued into queue.
40 enqueued into queue.
Queue elements: 10 20 30 40
10 dequeued from queue.
Queue elements: 20 30 40
Front element is: 20
insertion sort using link list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insert(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = head;
head = newNode;
}
void insertionSort() {
if (head == NULL || head->next == NULL) {
return;
}
struct Node* sorted = NULL;
struct Node* current = head;
while (current != NULL) {
struct Node* nextNode = current->next;
if (sorted == NULL || sorted->data >= current->data) {
current->next = sorted;
sorted = current;
} else {

Swetanjali Maharana (Asst. prof)


struct Node* temp = sorted;
while (temp->next != NULL && temp->next->data < current->data) {
temp = temp->next;
}
current->next = temp->next;
temp->next = current;
}
current = nextNode; // Move to the next node
}
head = sorted; // Update global head to the sorted list
}
void printList() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insert(12);
insert(11);
insert(64);
insert(25);
insert(22);
printf("Original Linked List:\n");
printList();
insertionSort();
printf("Sorted Linked List:\n");
printList();
return 0;
}
Output:
Original Linked List:
22 25 64 11 12 NULL

Sorted Linked List:

Swetanjali Maharana (Asst. prof)


11 12 22 25 64 NULL

Polynomial Addition Using Linked List


#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int pow;
struct Node* next;
};
struct Node* insertTerm(struct Node* head, int coeff, int pow) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->pow = pow;
newNode->next = NULL;
if (head == NULL) {
return newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
}
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
struct Node *p1 = poly1, *p2 = poly2, *temp;
while (p1 != NULL || p2 != NULL) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = NULL;
if (p1 != NULL && (p2 == NULL || p1->pow > p2->pow)) {
newNode->coeff = p1->coeff;
newNode->pow = p1->pow;
p1 = p1->next;
} else if (p2 != NULL && (p1 == NULL || p2->pow > p1->pow)) {

Swetanjali Maharana (Asst. prof)


newNode->coeff = p2->coeff;
newNode->pow = p2->pow;
p2 = p2->next;
} else {
newNode->coeff = p1->coeff + p2->coeff;
newNode->pow = p1->pow;
p1 = p1->next;
p2 = p2->next;
}
if (result == NULL) {
result = newNode;
temp = result;
} else {
temp->next = newNode;
temp = newNode;
}
}
return result;
}
void display(struct Node* poly) {
if (poly == NULL) {
printf("0\n");
return;
}
while (poly != NULL) {
printf("%d*x^%d", poly->coeff, poly->pow);
if (poly->next != NULL) {
printf(" + ");
}
poly = poly->next;
}
printf("\n");
}

int main() {
struct Node *poly1 = NULL, *poly2 = NULL, *result = NULL;
// Creating first polynomial: 3x^3 + 5x^2 + 6

Swetanjali Maharana (Asst. prof)


poly1 = insertTerm(poly1, 3, 3);
poly1 = insertTerm(poly1, 5, 2);
poly1 = insertTerm(poly1, 6, 0);
// Creating second polynomial: 4x^3 + 2x + 7
poly2 = insertTerm(poly2, 4, 3);
poly2 = insertTerm(poly2, 2, 1);
poly2 = insertTerm(poly2, 7, 0);
printf("Polynomial 1: ");
display(poly1);
printf("Polynomial 2: ");
display(poly2);
result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial after Addition: ");
display(result);
return 0;
}

Output:
Polynomial 1: 3*x^3 + 5*x^2 + 6
Polynomial 2: 4*x^3 + 2*x + 7
Resultant Polynomial after Addition: 7*x^3 + 5*x^2 + 2*x + 13

Swetanjali Maharana (Asst. prof)

You might also like