0% found this document useful (0 votes)
19 views11 pages

Linked Lists

This document discusses different types of linked lists including single linked lists, double linked lists, and circular linked lists. It includes functions to create nodes, insert nodes, search, delete nodes, and print the lists.

Uploaded by

armanmund27
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)
19 views11 pages

Linked Lists

This document discusses different types of linked lists including single linked lists, double linked lists, and circular linked lists. It includes functions to create nodes, insert nodes, search, delete nodes, and print the lists.

Uploaded by

armanmund27
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/ 11

Single Linked List

#include<stdio.h>

#include<stdlib.h>

struct node{

int key;

struct node* next;

};

struct node* newnode(int key){

struct node* temp = (struct node*)malloc(sizeof(struct node));

temp->key = key;

temp->next = NULL;

return temp;

void insert_at_beginning(struct node** head,int key){

struct node* temp = newnode(key);

temp->next= *head;

*head = temp;

void insert_at_end(struct node** head,int key){

struct node* temp = newnode(key);

if(*head = NULL){

*head = temp;

return 0;

struct node* last = *head;

while(last->next != NULL){

last = last->next;

last->next = temp;

}
void insert_after(struct node* prev,int key){

if(prev == NULL){

printf("Not found.");

return;

struct node* temp = newnode(key);

temp->next = prev->next;

prev->next = temp;

struct node* find_key(struct node* head,int key){

struct node* temp=head;

while(temp!=NULL){

if(temp->key == key){

return temp;

temp= temp->next;

return NULL;

void delete_key(struct node** head,int key){

struct node* temp = *head;

struct node* prev = NULL;

if(temp != NULL && temp->key == key){

*head = temp->next;

free(temp);

void print_list(struct node* head){

struct node* temp= head;

while(temp!= NULL){

printf("%d\t",temp->key);
temp= temp->next;

int main(){

struct node* head = NULL;

int key[] = {124,544,87,256,785,12,356,65};

int n = sizeof(key)/sizeof(key[0]);

for(int i=0;i<n;i++){

insert_at_end(&head,key[i]);

printf("The initial list:\n");

print_list(head);

insert_at_beginning(&head,111);

insert_at_end(&head,999);

struct node* prev = find_key(head,87);

insert_after(prev,555);

delete_key(&head,87);

delete_key(&head,356);

delete_key(&head,124);

printf("The updated list:\n");

print_list(head);

Double linked list


#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node* prev;

struct node* next;

};
struct node* createnode(int data){

struct node* newnode = (struct node*)malloc(sizeof(struct node));

newnode->data = data;

newnode->prev = NULL;

newnode->next = NULL;

return newnode;

void insert_at_beginning(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

else{

newnode->next = *head;

(*head)->prev = newnode;

*head= newnode;

void insert_at_end(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

else{

struct node* current = *head;

while(current->next != NULL){

current = current->next;

current->next = newnode;

newnode->prev = current;

}
}

int search(struct node* head,int key){

struct node* current = head;

while(current!= NULL){

if(current->data == key){

return 1;

current = current->next;

return 0;

void delete_node(struct node** head,int key){

struct node* current = *head;

while(current!= NULL && current->data != key){

current = current->next;

if(current == NULL){

printf("Element not found.\n");

if(current->prev != NULL){

current->prev->next = current->next;

else{

*head = current->next;

if(current->next != NULL){

current->next->prev = current->prev;

free(current);

void print_list(struct node* head){


struct node* current = head;

printf("Linked list\n");

while(current!=NULL){

printf("%d\t",current->data);

current= current->next;

void print_opp(struct node* tail){

printf("Traversal from back:\n");

while(tail->next != NULL){

tail= tail->next;

while(tail!=NULL){

printf("%d\t",tail->data);

tail = tail->prev;

printf("\n");

int main(){

struct node* head = NULL;

insert_at_end(&head,6);

insert_at_end(&head,8);

insert_at_end(&head,55);

insert_at_end(&head,22);

insert_at_beginning(&head,2);

insert_at_end(&head,5);

if(search(head,8)){

printf("Element 8 is present.\n");

else{

printf("Element 8 is not present.\n");


}

delete_node(&head,55);

print_list(head);

print_opp(head);

return 0;

Circular linked list


#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node* next;

};

struct node* createnode(int data){

struct node* newnode= (struct node*)malloc(sizeof(struct node));

newnode->data = data;

newnode->next = NULL;

return newnode;

void insert_at_beginning(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

newnode->next = newnode;

else{

struct node* last = (*head)->next;

while(last->next != *head){

last = last->next;

}
last->next = newnode;

newnode->next = *head;

*head = newnode;

void insert_at_end(struct node** head,int data){

struct node* newnode = createnode(data);

if(*head == NULL){

*head = newnode;

newnode->next = newnode;

else{

struct node* last = (*head)->next;

while(last->next != *head){

last = last->next;

last->next = newnode;

newnode->next= *head;

int search(struct node* head,int key){

struct node* current = head;

if(current == NULL){

return 0;

do{

if(current->data == key)

return 1;

current = current->next;

while(current!= head);
return 0;

void delete_node(struct node** head,int key){

if(*head == NULL){

return;

struct node* current = *head;

struct node* prev = NULL;

while(current->data != key){

if(current->next == *head){

printf("Element not found.\n");

return;

prev = current;

current = current->next;

if(current->next == *head && prev == NULL){

*head = NULL;

free(current);

return;

if(current == *head){

prev = (*head)->next;

while(prev->next != *head)

prev = prev->next;

*head = (*head)->next;

prev->next = *head;

free(current);

else if(current->next == *head){

prev->next = *head;
free(current);

else{

prev->next = current->next;

free(current);

void print_list(struct node* head){

struct node* current = head;

if(current == NULL)

return;

do{

printf("%d\t",current->data);

current= current->next;

}while(current!= head);

printf("\n");

int main(){

struct node* head = NULL;

insert_at_end(&head,6);

insert_at_end(&head,8);

insert_at_end(&head,55);

insert_at_end(&head,22);

insert_at_beginning(&head,2);

insert_at_end(&head,5);

if(search(head,8)){

printf("Element 8 is present.\n");

}else{

printf("Element 8 is not present.\n");

delete_node(&head,55);
printf("Linked list:");

print_list(head);

return 0;

You might also like