Answer to Question #238000 in C for Aadi

Question #238000
Write a program to implement the Doubly linked list. Perform the following operations on
the doubly linked list:
 Creating an empty doubly linked list
 Adding the new element at the beginning of the linked list.
 Deletion of a node after a particular location.
 Counting the no of nodes.
 Displaying the linked list
1
Expert's answer
2021-09-16T16:10:05-0400
#include <stdio.h>
#include <stdlib.h>
struct DoublyList{
    int obj;
    struct DoublyList* nextNode;
    struct DoublyList* prevNode;
};
void insertAtBeg(struct DoublyList** head, int info)
{
    struct DoublyList* newNode = (struct DoublyList*)malloc(sizeof(struct DoublyList));
 
    newNode->obj = info;
 
    newNode->nextNode = (*head);
    newNode->prevNode = NULL;
 
    if ((*head) != NULL)
        (*head)->prevNode = newNode;
 
    (*head) = newNode;
    
}
void EmptyLinkedList(struct DoublyList* node){
	node->nextNode = NULL;
	node->prevNode = NULL;
}


int Size_Nodes(struct DoublyList* node){
	int x = 0;
	while(node!=NULL){
		x++;
		node = node->nextNode;
	}
	return x;
}


void deleteNodeAtLocation(struct DoublyList** head, struct DoublyList* node)
{
   
    if (*head == NULL || node == NULL)
        return;
 
    
    if (*head == node)
        *head = node->nextNode;
 
    
    if (node->nextNode != NULL)
        node->nextNode->prevNode = node->prevNode;
 
    
    if (node->prevNode != NULL)
        node->prevNode->nextNode = node->nextNode;
 
    
    free(node);
    return;
}
void printList(struct DoublyList* head)
{
    struct DoublyList* lastNode;
    printf("\nInitial Linked list \n");
    while (head != NULL) {
        printf("  ");
        printf("%d",head->obj);
        lastNode = head;
        
        head = head->nextNode;
        
    }
 
    printf("\nReversing the doubly list \n");
    while (lastNode != NULL) {
        printf("  ");
        printf("%d",lastNode->obj);
        
        lastNode = lastNode->prevNode;
        
    }
}
int main()
{
    
    struct DoublyList* N = NULL;
    insertAtBeg(&N, 5);
    insertAtBeg(&N, 7);
    insertAtBeg(&N, 10);
    insertAtBeg(&N, 15);
    insertAtBeg(&N, 3);
    printList(N);
  printf("\nThe number of nodes:  ");
  printf("%d",Size_Nodes(N));
    getchar();
    return 0;
}
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS