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

Programs

The document contains C code examples for managing a singly linked list, including functions to insert nodes at the beginning and end, as well as delete nodes from the beginning and end. Each section includes a main function that demonstrates the functionality of the linked list operations. The code also provides display functions to visualize the linked list contents.

Uploaded by

jkola2
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 views5 pages

Programs

The document contains C code examples for managing a singly linked list, including functions to insert nodes at the beginning and end, as well as delete nodes from the beginning and end. Each section includes a main function that demonstrates the functionality of the linked list operations. The code also provides display functions to visualize the linked list contents.

Uploaded by

jkola2
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

SINGLY LINKED LIST

/* Insert First Node (When List is Empty) */

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

struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

void insert_first(int value)


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

newnode->data = value;
newnode->next = NULL;

head = newnode; // First node becomes head


}

void display()
{
struct node *temp = head;

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
insert_first(10);
display();
}
/* Insert at Beginning */

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

struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

void insert_begin(int value)


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

newnode->data = value;
newnode->next = head;

head = newnode;
}

void display()
{
struct node *temp = head;

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
insert_begin(10);
insert_begin(20);
display();
}
/* Insert at End */

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;

void insert_end(int value)


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

newnode->data = value;
newnode->next = NULL;

if(head == NULL)
{
head = newnode;
return;
}
struct node *temp = head;

while(temp->next != NULL)
temp = temp->next;

temp->next = newnode;
}
void display()
{
struct node *temp = head;

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
insert_end(10);
insert_end(20);
insert_end(30);
display();
}
/* Delete from Beginning */

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;

void insert_begin(int value)


{
struct node *newnode = malloc(sizeof(struct node));
newnode->data = value;
newnode->next = head;
head = newnode;
}
void delete_begin()
{
if(head == NULL)
{
printf("List Empty\n");
return;
}
struct node *temp = head;
head = head->next;

free(temp);
}
void display()
{
struct node *temp = head;

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
insert_begin(10);
insert_begin(20);

delete_begin();
display();
}
/* Delete from End */
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *next; };
struct node *head = NULL;

void insert_end(int value)


{ struct node *newnode = malloc(sizeof(struct node));
newnode->data = value;
newnode->next = NULL;
if(head == NULL)
{ head = newnode;
return; }
struct node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode; }
void delete_end() {
if(head == NULL) {
printf("List Empty\n");
return; }
struct node *temp = head;
struct node *prev = NULL;
while(temp->next != NULL)

{ prev = temp;
temp = temp->next; }
if(prev == NULL)
head = NULL;
else
prev->next = NULL;
free(temp); }
void display() {
struct node *temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next; }
printf("NULL\n"); }

int main() {
insert_end(10);
insert_end(20);
insert_end(30);
delete_end();
display(); }

You might also like