Linked List program using C
LinkedList.txt
File size: 3.97 KB
File content type: text/plain
Category: Practical Files
Subject: Data Structure
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
} *head = NULL;
int isEmpty() {
return (head==NULL);
}
void insert_At_Beginning(int value) {
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp -> data = value;
temp -> next = head;
head = temp;
}
void insert_At_End(int value) {
struct node *temp = (struct node*)malloc(sizeof(struct node));
struct node *current = head;
temp -> data = value;
temp -> next = NULL;
if(isEmpty()) {
head = temp;
return;
}
while(current -> next != NULL)
current = current -> next;
current -> next = temp;
}
void insert_After_Value(int desired, int value) {
struct node *current = head;
while(current != NULL && current -> data != desired)
current = current -> next;
if(current == NULL)
printf("Element %d is not in the list.\n",desired);
else {
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp -> data = value;
temp -> next = current -> next;
current -> next = temp;
}
}
void delete_At_Beginning() {
if(isEmpty())
printf("List is empty.\n");
else {
struct node *temp = head;
head = head -> next;
temp -> next = NULL;
free(temp);
}
}
void delete_At_End() {
if(isEmpty()) {
printf("List is empty.\n");
return ;
}
struct node *temp = head, *prev;
if(head -> next == NULL) {
head = NULL;
free(temp);
return ;
}
while(temp -> next != NULL) {
prev = temp;
temp = temp -> next;
}
prev -> next = temp -> next;
temp -> next = NULL;
free(temp);
}
void delete_With_Value(int desired) {
if(isEmpty()) {
printf("List is empty.\n");
return ;
}
struct node *temp = head, *prev;
if(head -> data == desired) {
head = head -> next;
temp -> next = NULL;
free(temp);
return ;
}
while(temp != NULL && temp -> data != desired) {
prev = temp;
temp = temp -> next;
}
if(temp == NULL)
printf("Element %d not in the list\n",desired);
else {
prev -> next = temp -> next;
temp -> next = NULL;
free(temp);
}
}
void search(int desired) {
struct node *temp = head;
while(temp != NULL && temp -> data != desired)
temp = temp -> next;
if(temp == NULL)
printf("Element %d not found in the list.\n",desired);
else
printf("Element %d is present in the list.\n",desired);
}
void printList() {
if(isEmpty()) {
printf("List is Empty\n");
return ;
}
struct node *current = head;
while(current -> next != NULL) {
printf("%d -> ",current->data);
current = current -> next;
}
printf("%d\n",current->data);
}
void iterative_Reverse() {
struct node* prev = NULL;
struct node* current = head;
struct node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
int main() {
int i;
for(i = 0; i < 5; i++)
insert_At_Beginning(i);
printList();
for(i = 5; i < 10; i++)
insert_At_End(i);
printList();
insert_After_Value(5, 9);
insert_After_Value(10, 9);
printList();
for(i = 0; i < 3; i++)
delete_At_End();
printList();
for(i = 0; i < 3; i++)
delete_At_Beginning();
printList();
delete_With_Value(1);
delete_With_Value(5);
printList();
iterative_Reverse(head);
printList();
search(6);
search(8);
}
C Program for linked list
Last Updated: July 16, 2022
Related
Physics Akash First Semester 2015 & 2016
Category: Question PapersJuly 16, 2022
Radioactivity
Category: NotesJuly 16, 2022
IPU Syllabus CSE 5th Sem
Category: SyllabusJuly 17, 2022
B.L. Thareja-Basic electrical Engineering (Vol.1)
Category: eBooksJuly 16, 2022
Data Structures Viva Questions
Category: Practical FilesJuly 16, 2022
BBA MPOB Notes Unit 4
Category: NotesJuly 16, 2022
Applied Physics Lab Manual (Printed)
Category: Practical FilesJuly 16, 2022
FOC Practical File PDF
Category: Practical FilesJuly 16, 2022
EHVACDC unit 2 FACTS topic
Category: NotesJuly 17, 2022
Computer Graphics and Multimedia (CGMT) notes
Category: NotesJuly 16, 2022