#include <iostream>
using namespace std;
struct DoublyList{
int item;
struct DoublyList* nextNode;
struct DoublyList* prevNode;
};
void addAtBeginning(struct DoublyList** head, int data)
{
struct DoublyList* newNode = (struct DoublyList*)malloc(sizeof(struct DoublyList));
newNode->item = data;
newNode->nextNode = (*head);
newNode->prevNode = NULL;
if ((*head) != NULL)
(*head)->prevNode = newNode;
(*head) = newNode;
}
//Creating Empty linked list
void EmptyLinkedList(struct DoublyList* node){
node->nextNode = NULL;
node->prevNode = NULL;
}
//Counting the number of nodes
int countNodes(struct DoublyList* node){
int i = 0;
while(node!=NULL){
i++;
node = node->nextNode;
}
return i;
}
//Deleting nodes after a particular location
void deleteNodeAtLocation(DoublyList** head, 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 displayList(struct DoublyList* head)
{
struct DoublyList* lastNode;
cout <<"\nDisplaying from the beggining \n";
while (head != NULL) {
cout <<" "<< head->item;
lastNode = head;
head = head->nextNode;
}
cout <<"\nDisplaying from the end \n";
while (lastNode != NULL) {
cout <<" "<< lastNode->item;
lastNode = lastNode->prevNode;
}
}
int main()
{
struct DoublyList* list = NULL;
addAtBeginning(&list, 20);
addAtBeginning(&list, 30);
addAtBeginning(&list, 40);
displayList(list);
cout<<"\nThe number of nodes: "<<countNodes(list)<<endl;
getchar();
return 0;
}
Comments
Leave a comment