Professional Documents
Culture Documents
https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode *temp;
temp=(SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
temp -> data=data;
temp -> next=NULL;
if(head == NULL) {
head = temp;
return head;
}
else{
SinglyLinkedListNode *p;
p = head;
while(p->next != NULL)
p = p->next;
p->next = temp;
}
return head;
}
https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem
void printLinkedList(SinglyLinkedListNode* head) {
if(head==NULL){
return;
}
else{
while(head!=NULL){
printf("%d\n",head->data);
head=head->next;
}
}
}
SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
SinglyLinkedListNode *new_node=(SinglyLinkedListNode*)malloc(sizeof(SinglyLink
edListNode));
new_node->data=data;
new_node->next=NULL;
if(llist==NULL){
llist=new_node;
}
else {
new_node->next=llist;
llist=new_node;
}
return llist;
}
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data,
int position) {
SinglyLinkedListNode *ptr,*temp;
temp=(SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
temp->data=data;
if (head == NULL) {
return temp;
}
if(position==0){
temp->next=head;
return temp;
}
ptr=head;
while(position-1>0){
ptr=ptr->next;
position--;
}
temp->next=ptr->next;
ptr->next=temp;
return head;
}
Q.5 WAP to delete a node from given position in a linked list.
https://www.hackerrank.com/challenges/delete-a-node-from-a-linked-list/problem
SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) {
SinglyLinkedListNode *p,*temp;
if(position==0){
temp=head;
head=head->next;
free(temp);
return head;
}
else{
temp=head;
p=NULL;
while(position-1>=0){
p=temp;
temp=temp->next;
position--;
}
p->next=temp->next;
free(temp);
}
return head;
}
void reversePrint(SinglyLinkedListNode* head) {
if(head==NULL){
return;
}
reversePrint(head->next);
printf("%d\n",head->data);
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
if(head == NULL) {
DoublyLinkedListNode *temp = (DoublyLinkedListNode*)malloc(sizeof(DoublyLinked
ListNode));
temp->data = data;
return temp;
}
if(head->data <= data) {
head->next = sortedInsert(head->next, data);
head->next->prev = head;
}
else if(head->data > data)
{DoublyLinkedListNode *temp = (DoublyLinkedListNode*)malloc(sizeof(DoublyLinke
dListNode));
temp->data = data;
temp->next = head;
temp->prev = head->prev;
head->prev = temp;
head = temp;
}
return head;
if(head == NULL || head->next == NULL)
return false;
SinglyLinkedListNode *m=head;
SinglyLinkedListNode *n=head;
while(m!=NULL && m->next!=NULL){
n=n->next;
m=m->next->next;
if(n==m){
return true;
}
}
return false;
}
struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;
int main()
{
int n;
stnode = NULL;
ennode = NULL;
DlListcreation(n);
displayDlList();
return 0;
}
void DlListcreation(int n)
{
int i, num;
struct node *fnNode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL)
{
printf(" Enter data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(" enter data for node %d : ", i);
scanf("%d", &num);
fnNode->num = num;
fnNode->preptr = ennode;
fnNode->nextptr = NULL;
ennode->nextptr = fnNode;
ennode = fnNode;
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void displayDlList()
{
struct node * tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered on the list are :\n");
while(tmp != NULL)
{
printf("%d\n", tmp->num);
n++;
tmp = tmp->nextptr;
}
}
}
Q.10 Write a menu driven program for implementing doubly linked list.
1. To insert new node at beginning,
2. To insert new node after specified position
3. To insert new node at the end
4. To delete the node from beginning
5. To delete after specified position
6. To delete from the end
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * prev;
struct node * next;
}*head, *last;
int main()
{
int n, data, choice=1;
head = NULL;
last = NULL;
while(choice != 0)
{
printf("DOUBLY LINKED LIST PROGRAM\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
printf("Enter data of first node : ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 3:
printf("Enter data of last node : ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 4:
printf("Enter the position where you want to insert new node: ");
scanf("%d", &n);
printf("Enter data of %d node : ", n);
scanf("%d", &data);
insertAtN(data, n);
break;
case 5:
deleteFromBeginning();
break;
case 6:
deleteFromEnd();
break;
case 7:
printf("Enter the node position which you want to delete: ");
scanf("%d", &n);
deleteFromN(n);
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-5");
}
displayList();
printf("\n\n\n\n\n");
}
return 0;
}
void createList(int n)
{
int i, data;
struct node *newNode;
if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last;
newNode->next = NULL;
last->next = newNode;
last = newNode;
}
void displayList()
{
struct node * temp;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;
temp = temp->next;
}
}
}
if(head == NULL)
{
printf("Error, List is Empty!\n");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
head->prev = newNode;
head = newNode;
if(last == NULL)
{
printf("Error, List is empty!\n");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = last;
last->next = newNode;
last = newNode;
if(head == NULL)
{
printf("Error, List is empty!\n");
}
else
{
temp = head;
i=1;
if(position == 1)
{
insertAtBeginning(data);
}
else if(temp == last)
{
insertAtEnd(data);
}
else if(temp!=NULL)
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = temp->next;
newNode->prev = temp;
if(temp->next != NULL)
{
temp->next->prev = newNode;
}
temp->next = newNode;
if(head == NULL)
{
printf("Unable to delete. List is empty.\n");
}
else
{
toDelete = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
free(toDelete);
printf("SUCCESSFULLY DELETED NODE FROM BEGINNING OF THE LIST.\n");
}
}
void deleteFromEnd()
{
struct node * toDelete;
if(last == NULL)
{
printf("Unable to delete. List is empty.\n");
}
else
{
toDelete = last;
last = last->prev;
if (last != NULL)
last->next = NULL;
free(toDelete);
printf("SUCCESSFULLY DELETED NODE FROM END OF THE LIST.\n");
}
}
void deleteFromN(int position)
{
struct node *current;
int i;
current = head;
for(i=1; i<position && current!=NULL; i++)
{
current = current->next;
}
if(position == 1)
{
deleteFromBeginning();
}
else if(current == last)
{
deleteFromEnd();
}
else if(current != NULL)
{
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
struct node {
int num;
struct node * nextptr;
}*stnode;
int main()
{
int n;
stnode = NULL;
printf("\n\n Circular Linked List \n");
printf(" Enter the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
displayClList();
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
}
preptr->nextptr = stnode;
}
}
void displayClList()
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {
printf("%d\t", tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
Q.12 WAP to count the number of nodes in circular linked list if only start
pointer of circular linked list is given.
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;
int main()
{
int n;
stnode = NULL;
printf("\n\n Circular Linked List \n");
printf(" Enter the number of nodes : ");
scanf("%d", &n);
ClListcreation(n);
displayClList();
count();
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
}
preptr->nextptr = stnode;
}
}
void displayClList()
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {
printf("%d\t", tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}
void count(){
struct node *temp;
int c=0;
if (stnode==NULL){
printf("No Data found in the list yet .");
}
else
{ temp=stnode;