#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;
}
Comments
Leave a comment