Practical 6: Implementation of Linked Lists (Singly,
Doubly, Circular)
Singly Linked List Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
void insertBegin(int data) {
struct node* n = (struct node*)malloc(sizeof(struct node));
n->data = data;
n->next = head;
head = n;
}
void insertEnd(int data) {
struct node* n = (struct node*)malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
if (head == NULL) head = n;
else {
struct node* temp = head;
while (temp->next != NULL) temp = temp->next;
temp->next = n;
}
}
void deleteBegin() {
if (head != NULL) head = head->next;
}
void display() {
struct node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertBegin(10);
insertEnd(20);
insertEnd(30);
printf("Linked List: "); display();
deleteBegin();
printf("After deletion: "); display();
}
Output:
Linked List: 10 -> 20 -> 30 -> NULL
After deletion: 20 -> 30 -> NULL