0% found this document useful (0 votes)
11 views23 pages

) Write A Program To Create Linked List, Add Node To Linked List and Remove Node From Linked List

The document contains C code for creating and managing a linked list, including functions to insert and delete nodes at various positions, print the list, and search for data. It also includes a separate implementation for stack and queue operations using linked lists, with functions for pushing, popping, enqueuing, and dequeuing elements. The program features a user interface for interacting with these data structures through a menu-driven approach.

Uploaded by

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

) Write A Program To Create Linked List, Add Node To Linked List and Remove Node From Linked List

The document contains C code for creating and managing a linked list, including functions to insert and delete nodes at various positions, print the list, and search for data. It also includes a separate implementation for stack and queue operations using linked lists, with functions for pushing, popping, enqueuing, and dequeuing elements. The program features a user interface for interacting with these data structures through a menu-driven approach.

Uploaded by

abhijitmaskar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

5 ] Write a program to create linked list, add node to linked list and

Remove node from linked list.


#include<stdio.h> void update_node_data(int, int);
#include<malloc.h> void empty_message(void);
int size_of_list();
//type declaration of a node int getData();
struct node int getPosition();
{
int data;
struct node* next; int main()
}; {
char user_active = 'Y';
//global head pointer int user_choice;
struct node* head = NULL; int data, position;

//prototyping of the functions while(user_active == 'Y' ||


user_active == 'y')
struct node* create_node(int);
{
void insert_at_beginning(int);
void insert_at_end(int);
printf("\n\n------ Singly Linked
void insert_at_position(int, int);
List -------\n");
void delete_at_beginning();
printf("\n1. Insert a node at
void delete_at_end(); beginning");
void delete_at_position(int); printf("\n2. Insert a node at
void print_from_beginning(); end");

void print_from_end(struct node*); printf("\n3. Insert a node at


given position");
void search_data(int);
printf("\n\n4. Delete a node insert_at_beginning(data);
from beginning");
break;
printf("\n5. Delete a node from
end");
case 2:
printf("\n6. Delete a node from
given position"); printf("\nInserting a node
at end");
printf("\n\n7. Print list from
beginning"); data = getData();

printf("\n8. Print list from insert_at_end(data);


end"); break;
printf("\n9. Search a node
data");
case 3:
printf("\n10. Update a node
data"); printf("\nInserting a node
at the given position");
printf("\n11. Exit");
data = getData();
printf("\n\
n------------------------------\n"); position = getPosition();
insert_at_position(data,
position);
printf("\nEnter your choice: ");
break;
scanf("%d", &user_choice);

case 4:
printf("\
n------------------------------\n"); printf("\nDeleting a node
from beginning\n");
switch(user_choice)
delete_at_beginning();
{
break;
case 1:
printf("\nInserting a node
at beginning"); case 5:

data = getData(); printf("\nDeleting a node


from end\n");
delete_at_end(); break;
break;
case 10:
case 6: printf("\nUpdating the
node data");
printf("\nDelete a node
from given position\n"); data = getData();
position = getPosition(); position = getPosition();
update_node_data(data,
delete_at_position(position); position);
break; break;

case 7: case 11:


printf("\nPrinting the list printf("\nProgram was
from beginning\n\n"); terminated\n\n");
print_from_beginning(); return 0;
break;

default:
case 8: printf("\n\tInvalid Choice\
printf("\nPrinting the list n");
from end\n\n"); }
print_from_end(head);
break; printf("\n...............................\
n");

case 9: printf("\nDo you want to


continue? (Y/N) : ");
printf("\nSearching the
node data"); fflush(stdin);

data = getData(); scanf(" %c", &user_active);

search_data(data); }
void insert_at_beginning(int data)
return 0; {
} struct node* new_node = NULL;
void empty_message() new_node = create_node(data);
{
printf("\n\tList is Empty!\n"); if(new_node != NULL)
} {
void memory_message() new_node->next = head;
{ head = new_node;
printf("\nMemory can't be printf("\n* Node with data %d
allocated\n"); was Inserted\n", data);
} }
struct node* create_node(int data) }
{
struct node* new_node = (struct void insert_at_end(int data)
node*) malloc(sizeof(struct node));
{
struct node* new_node = NULL;
if(new_node == NULL)
new_node = create_node(data);
{
memory_message();
if(new_node != NULL)
return NULL;
{
}

if(head == NULL)
new_node->data = data;
{
new_node->next = NULL;
head = new_node;
return new_node;
}
}
else
{ }
struct node* last = head; if(head != NULL && (pos <= 0 ||
pos > list_size))
{
while(last->next != NULL)
printf("\nInvalid position to
{
insert a node\n");
last = last->next;
return;
}
}

last->next = new_node;
struct node* new_node = NULL;
}
new_node = create_node(data);
printf("\n* Node with data %d
was Inserted\n", data);
if(new_node != NULL)
}
{
}
struct node* temp = head;
void insert_at_position(int data, int
pos) int count = 1;
{ while(count < pos-1)
{
int list_size = 0; temp = temp -> next;
list_size = size_of_list(); count += 1;
}
if(head == NULL && (pos <= 0 || if(pos == 1)
pos > 1))
{
{
new_node->next = head;
printf("\nInvalid position to
head = new_node;
insert a node\n");
}
return;
else
{ {
new_node->next = temp- if(head == NULL)
>next;
{
temp->next = new_node;
empty_message();
}
return;
printf("\n* Node with data %d
}
was Inserted\n", data);
}
struct node* temp = head;
}
struct node* prev = NULL;
void delete_at_beginning()
int data;
{
while(temp->next != NULL)
if(head == NULL)
{
{
prev = temp;
empty_message();
temp = temp->next;
return;
}
}

data = temp->data;
struct node* temp = head;
int data = head->data;

if(temp == head)
head = head->next;
{
free(temp);
free(temp);
head = NULL;
printf("\n* Node with data %d
was Deleted\n", data); }

}
void delete_at_end() else
{ count += 1;
free(temp); }
prev->next = NULL;
} int data = temp->data;
printf("\n* Node with data %d
was Deleted\n", data);
if(temp == head)
}
{
void delete_at_position(int pos)
head = head->next;
{
free(temp);
int list_size = 0;
}
list_size = size_of_list();

else
if(pos <= 0 || pos > list_size)
{
{
prev->next = temp->next;
printf("\nInvalid position to
free(temp);
delete a node\n");
}
return;
printf("\n* Node with data %d
}
was Deleted\n", data);

struct node* temp = head;


}
struct node* prev = NULL;
void search_data(int data)
int count = 1;
{
int position = 0;
while(count < pos)
int flag = 0;
{
prev = temp;
struct node* temp = head;
temp = temp->next;
while(temp != NULL) if(pos <= 0 || pos > list_size)
{ {
position += 1; printf("\nInvalid position to
update a node\n");
if(temp->data == data)
return;
{
}
flag = 1;
break;
struct node* temp = head;
}
int count = 1;
temp = temp->next;
}
while(count < pos)
{
if(flag == 0)
temp = temp->next;
{
count += 1;
printf("\nNode with data %d
was not found!\n", data); }
}
else temp->data = new_data;
{ printf("\nUpdated node data is
%d\n", new_data);
printf("\nFound data at %d
position\n", position); }
} void print_from_beginning()
} {
void update_node_data(int if(head == NULL)
new_data, int pos)
{
{
empty_message();
int list_size = 0;
return;
list_size = size_of_list();
}
struct node* temp = head; temp = temp->next;
}
while(temp != NULL) return count;
{ }
printf("%d ", temp->data); int getData()
temp = temp->next; {
} int data;
} printf("\n\nEnter Data: ");
void print_from_end(struct node* scanf("%d", &data);
head)
{
return data;
if(head == NULL)
}
{
int getPosition()
return;
{
}
int pos;
print_from_end( head->next );
printf("%d ", head->data);
printf("\nEnter Position: ");
}
scanf("%d", &pos);
int size_of_list()
{
return pos;}
struct node* temp = head;
int count = 0;

while(temp != NULL)
{
count += 1;
6).

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

int main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;

NODE *head, *first, *temp = 0;


int count = 0;
int choice = 1;
first = 0;

while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);

}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}

Output:
7).
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Stack functions
struct Node* push(struct Node* top, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap overflow!\n");
return top;
}
newNode->data = value;
newNode->next = top;
top = newNode;
return top;
}

struct Node* pop(struct Node* top) {


if (!top) {
printf("Stack underflow!\n");
return top;
}
struct Node* temp = top;
printf("Popped: %d\n", top->data);
top = top->next;
free(temp);
return top;
}
void displayStack(struct Node* top) {
if (!top) {
printf("Stack is empty.\n");
return;
}
printf("Stack: ");
while (top) {
printf("%d ", top->data);
top = top->next;
}
printf("\n");
}

// Queue functions
struct Node* enqueue(struct Node* rear, int value, struct Node** front) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap overflow!\n");
return rear;
}
newNode->data = value;
newNode->next = NULL;
if (!rear) {
*front = newNode;
} else {
rear->next = newNode;
}
return newNode;
}

struct Node* dequeue(struct Node* front, struct Node** rear) {


if (!front) {
printf("Queue underflow!\n");
return front;
}
struct Node* temp = front;
printf("Dequeued: %d\n", front->data);
front = front->next;
if (!front) {
*rear = NULL;
}
free(temp);
return front;
}

void displayQueue(struct Node* front) {


if (!front) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
while (front) {
printf("%d ", front->data);
front = front->next;
}
printf("\n");
}

int main() {
struct Node* stackTop = NULL;
struct Node* queueFront = NULL;
struct Node* queueRear = NULL;

int choice, value;

while (1) {
printf("\nMenu:\n");
printf("1. Push to Stack\n");
printf("2. Pop from Stack\n");
printf("3. Display Stack\n");
printf("4. Enqueue to Queue\n");
printf("5. Dequeue from Queue\n");
printf("6. Display Queue\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
stackTop = push(stackTop, value);
break;
case 2:
stackTop = pop(stackTop);
break;
case 3:
displayStack(stackTop);
break;
case 4:
printf("Enter value to enqueue: ");
scanf("%d", &value);
queueRear = enqueue(queueRear, value, &queueFront);
break;
case 5:
queueFront = dequeue(queueFront, &queueRear);
break;
case 6:
displayQueue(queueFront);
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}

return 0;
}

Output:

You might also like