You are on page 1of 16

Ds lab-9

Q.1 WAP to insert at the end of the linked list.

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;
}
   

Q.2 WAP to print the elements of a linked list.

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;
        }
    }
}

Q.3 WAP to insert at the beginning of the linked list.


https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-
list/problem

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;
    
}

Q.4 WAP to insert a node at specify position in a linked list.


https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-
a-linked-list/problem

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;
}

Q.6 WAP to print the elements in reverse order in a linked list.


https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list- in-
reverse/problem

 void reversePrint(SinglyLinkedListNode* head) {

    if(head==NULL){
        return;
    }
    reversePrint(head->next);
    printf("%d\n",head->data);

Q.7 WAP to insert a node into a sorted doubly linked list.


https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-
linked-list/problem

 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;

Q.8 WAP to detect loop or cycle in a linked list.


https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-
a-cycle/problem
 bool has_cycle(SinglyLinkedListNode* 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;
}

Q.9 WAP to create the doubly linked list of n nodes.


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

struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void DlListcreation(int n);


void displayDlList();

int main()
{
int n;
stnode = NULL;
ennode = NULL;

printf(" Enter the number of nodes : ");


scanf("%d", &n);

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;

void createList(int n);


void displayList();
void insertAtBeginning(int data);
void insertAtEnd(int data);
void insertAtN(int data, int position);
void deleteFromBeginning();
void deleteFromEnd();
void deleteFromN(int position);

int main()
{
int n, data, choice=1;

head = NULL;
last = NULL;

while(choice != 0)
{
printf("DOUBLY LINKED LIST PROGRAM\n");

printf("1. Create List\n");


printf("2. Insert node - at beginning\n");
printf("3. Insert node - at end\n");
printf("4. Insert node - at N\n");
printf("5. Delete node - from beginning\n");
printf("6. Delete node - from end\n");
printf("7. Delete node - from N\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");

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));

printf("Enter data of 1 node: ");


scanf("%d", &data);

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;
}

printf("\n\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}

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;
}
}
}

void insertAtBeginning(int data)


{
struct node * newNode;

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;

printf("NODE INSERTED SUCCESSFULLY AT THE BEGINNING OF THE LIST\n");


}
}

void insertAtEnd(int data)


{
struct node * 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;

printf("NODE INSERTED SUCCESSFULLY AT THE END OF LIST\n");


}
}

void insertAtN(int data, int position)


{
int i;
struct node * newNode, *temp;

if(head == NULL)
{
printf("Error, List is empty!\n");
}
else
{
temp = head;
i=1;

while(i<position-1 && temp!=NULL)


{
temp = temp->next;
i++;
}

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;

printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);


}
else
{
printf("Error, Invalid position\n");
}
}
}
void deleteFromBeginning()
{
struct node * toDelete;

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);

printf("SUCCESSFULLY DELETED NODE FROM %d POSITION.\n", position);


}
else
{
printf("Invalid position!\n");
}
}

Q.11 WAP to create circular linked list of n nodes.


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

struct node {
int num;
struct node * nextptr;
}*stnode;

void ClListcreation(int n);


void displayClList();

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));

printf(" Enter data for node 1 : ");


scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Enter data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;

}
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;

void ClListcreation(int n);


void displayClList();
void count();

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));

printf(" Enter data for node 1 : ");


scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Enter data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;

}
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;

printf("\nTotal elemnts in a list ");


do {
temp = temp->nextptr;
c++;
} while (temp != stnode);
printf("= %d",c);
}
}

You might also like