linked list
linked list
-----------------------------------------
Program to insert a node at the beginning of linked list
------------------------------------------------------------------
-------------------------------------------
------------------------------------------------------------------
-------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
newNode->next = *head;
*head = newNode;
}
int main()
{
struct node *head = NULL;
addFirst(&head,10);
addFirst(&head,20);
addFirst(&head,30);
printList(head);
return 0;
}
------------------------------------------------------------------------------------------------------------------------
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printList(head);
return 0;
}
--------------------------------------------------------------------------
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
return 0;
}
--------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printf("Linked List Elements:\n");
printList(head);
//delete 20
deleteNode(&head,20);
printf("Deleted 20. The New Linked List:\n");
printList(head);
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
Insert Any position
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
/Linked list insertion at specific position in C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
else
{
// temp used to traverse the Linked List
struct Node *temp = *head;
int main ()
{
node2->data = 20;
node2->next = node3;
node3->data = 30;
node3->next = node4;
node4->data = 40;
node4->next = NULL;
display (head);
//Inserts data: 15 after the 1st node
insertPosition (1, 15, &head);
display (head);
return 0;
}
------------------------------------------------------------------------------------------------------------------------
Reverse Link List
------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
/**
* C program to reverse a Singly Linked List
*/
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; //Data part
struct node *next; //Address part
}*head;
int main()
{
int n, choice;
/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
/*
* Reverse the list
*/
printf("\nPress 1 to reverse the order of singly linked list\n");
scanf("%d", &choice);
if(choice == 1)
{
reverseList();
}
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
if(n <= 0)
{
printf("List size must be greater than zero.\n");
return;
}
/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Read data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
/*
* Reverse the order of nodes of a singly linked list
*/
void reverseList()
{
struct node *prevNode, *curNode;
if(head != NULL)
{
prevNode = head;
curNode = head->next;
head = head->next;
while(head != NULL)
{
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
/*
* Display entire list
*/
void displayList()
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
}
}
}