-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linked_list.cpp
70 lines (61 loc) · 1.38 KB
/
circular_linked_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include "circular_linked_list.h"
LinkedList::LinkedList() //Defining constructor
{
ptr=nullptr;
}
bool LinkedList::isEmpty() //Returns true if the stack is empty and false otherwise
{
return ptr == nullptr;
}
void LinkedList::push(int element) //Inserts an element into the stack.
{
Node *newNode = new Node(element,ptr);
newNode->info=element;
if (this->isEmpty())
{
ptr = newNode;
}
else{
newNode->next=ptr->next;
}
ptr->next=newNode;
}
void LinkedList::pop() //Displays and deletes an element at the top of the stack.
{
if (!this->isEmpty())
{
Node *top;
top = ptr->next;
int data;
data = top->info;
if(top==ptr)
{
ptr=nullptr;
}
else
{
ptr->next=top->next;
}
std::cout <<">>> Element popped out from stack is :: "<< data <<"\n\n";
delete top;
}
else{
std::cout <<"----STACK IS EMPTY----\n\n";
}
}
void LinkedList::top() //Displays the element at the top of the stack.
{
if (!this->isEmpty())
{
Node *peek;
peek=ptr->next;
int topelement;
topelement = peek->info;
std::cout <<">>> Element in top of the stack is :: "<< topelement <<"\n\n";
}
else
{
std::cout <<"----STACK IS EMPTY----\n\n";
}
}