0% found this document useful (0 votes)
25 views

Write An Algorithm To Insert A New Node at The Beginning of A Singly Linked List Give Example

hi am study in computer en

Uploaded by

jackmarquina080
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)
25 views

Write An Algorithm To Insert A New Node at The Beginning of A Singly Linked List Give Example

hi am study in computer en

Uploaded by

jackmarquina080
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/ 5

Write an algorithm to insert a new node at the beginning of a singly linked list give

example

Algorithm to Insert a New Node at the Beginning of a Singly Linked List

1. Create a new node: Allocate memory for the new node and assign it the given data.
2. Make the new node's next pointer point to the current head: The new node will now
point to the existing head of the list.
3. Update the head: Change the head to point to the new node, making it the first node in
the list.

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int new_data) {
// Step 1: Allocate memory for a new node
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

// Step 2: Put data in the new node


new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
)
struct Node* head = NULL;
insertAtBeginning(&head, 30); // List: 30
insertAtBeginning(&head, 20); // List: 20 -> 30
insertAtBeginning(&head, 10); // List: 10 -> 20 -> 30
insertAtBeginning(&head, 5); // List: 5 -> 10 -> 20 -> 30

printf("Linked list after insertion: ");


printList(head);

return 0;
}

***Write an algorithm to search an element in linked list with


Suitable program**
1. Start from the head: Initialize a pointer to the head of the linked list.
2. Traverse the list: Move through the list node by node, comparing each node's data with
the target value.
3. Element Found: If a node with the target value is found, return the position (index) of
that node.
4. Element Not Found: If the end of the list is reached and no match is found, return -1.

Program--
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;

new_node->next = *head;

*head = new_node;
}
int searchElement(struct Node* head, int target) {
int position = 0; // To track the position of the node

struct Node* current = head;


while (current != NULL) {
if (current->data == target) {
return position; // Element found at the current position
}
position++;
current = current->next; // Move to the next node
}

return -1; // Element not found


}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;

insertAtBeginning(&head, 30); // List: 30


insertAtBeginning(&head, 20); // List: 20 -> 30
insertAtBeginning(&head, 10); // List: 10 -> 20 -> 30
insertAtBeginning(&head, 5); // List: 5 -> 10 -> 20 -> 30

printf("Linked List: ");


printList(head);

int target = 20;


int position = searchElement(head, target);

if (position != -1) {
printf("Element %d found at position: %d\n", target, position);
} else {
printf("Element %d not found in the list.\n", target);
}
return 0;
}
***Write an program to implement stack with push, pop, and
Display operation***
#include <stdio.h>

#include <stdlib.h>

#define MAX 5

struct Stack {

int arr[MAX];

int top;

};

void initializeStack(struct Stack* stack) {

stack->top = -1; // Stack is empty, so the top is -1

int isFull(struct Stack* stack) {

return stack->top == MAX - 1;

int isEmpty(struct Stack* stack) {

return stack->top == -1; // Stack is empty if top is -1

void push(struct Stack* stack, int value) {

if (isFull(stack)) {

printf("Stack Overflow! Cannot push %d onto the stack.\n", value);

} else {

stack->arr[++stack->top] = value; // Increment top and add element

printf("Pushed %d onto the stack.\n", value);

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack Underflow! No elements to pop.\n");

return -1; // Return -1 if stack is empty

} else {

int value = stack->arr[stack->top--]; // Return the top element and decrement top
return value;

void display(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty. Nothing to display.\n");

} else {

printf("Stack elements: ");

for (int i = 0; i <= stack->top; i++) {

printf("%d ", stack->arr[i]);

printf("\n");

int main() {

struct Stack stack;

initializeStack(&stack);

push(&stack, 10);

push(&stack, 20);

push(&stack, 30);

push(&stack, 40);

push(&stack, 50);

push(&stack, 60);

display(&stack);

int poppedValue = pop(&stack);

if (poppedValue != -1) {

printf("Popped %d from the stack.\n", poppedValue);

display(&stack);

return 0;

You might also like