0% found this document useful (0 votes)
24 views9 pages

sample programs

C programmers

Uploaded by

gopiuma123456
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)
24 views9 pages

sample programs

C programmers

Uploaded by

gopiuma123456
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
You are on page 1/ 9

#include <stdio.

h>
#include <stdlib.h>

//Represent a node of singly linked list


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

//Represent the head and tail of the singly linked list


struct node *head, *tail = NULL;

//addNode() will add a new node to the list


void addNode(int data)
{
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty


if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


void display() {
//Node current will point to head
struct node *current = head;

if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);

//Displays the nodes present in the list


display();

return 0;
}

The line of code struct node* newnode = (struct node*) malloc(sizeof(struct node)); is used
in C programming to allocate memory dynamically for a new node of a linked list (or any
similar data structure). Let's break it down:

: This declares a pointer named newnode that will point to a struct node. The struct node is
presumably a user-defined structure that you've defined elsewhere in your code.
:
- malloc is a standard library function that allocates a specified number of bytes in memory.
It returns a pointer to the beginning of the allocated memory block.
- sizeof(struct node) computes the size (in bytes) of the struct node. This ensures that the
correct amount of memory is allocated to hold a single instance of struct node.
: This is a type cast. malloc returns a pointer of type void*, which can point to any data type.
To make it explicit that the pointer returned by malloc should be treated as a pointer to
struct node, we cast it to (struct node*).
// // C Program for Implementation of Singly Linked List
#include <stdio.h>
#include <stdlib.h>

// Define the Node structure


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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new element at the beginning of the singly linked list
void insertAtFirst(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to insert a new element at the end of the singly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to insert a new element at a specific position in the singly linked list
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 0) {
insertAtFirst(head,data);
return;
}
struct Node* temp = *head;
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode; }
// Function to delete the first node of the singly linked list
void deleteFromFirst(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}

// Function to delete the last node of the singly linked list


void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}

// Function to delete a node at a specific position in the singly linked list


void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
deleteFromFirst(head);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
return;
}
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}

// Function to print the LinkedList


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

// Driver Code
int main() {
struct Node* head = NULL;

insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);

printf("Linked list after inserting the node:20 at the end \n");


insertAtEnd(&head, 20);
print(head);

printf("Linked list after inserting the node:5 at the end \n");


insertAtEnd(&head, 5);
print(head);

printf("Linked list after inserting the node:30 at the end \n");


insertAtEnd(&head, 30);
print(head);

printf("Linked list after inserting the node:15 at position 2 \n");


insertAtPosition(&head, 15, 2);
print(head);
printf("Linked list after deleting the first node: \n");
deleteFromFirst(&head);
print(head);

printf("Linked list after deleting the last node: \n");


deleteFromEnd(&head);
print(head);

printf("Linked list after deleting the node at position 1: \n");


deleteAtPosition(&head, 1);
print(head);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy