Check if stack elements are pairwise consecutive
Here given code implementation process.
//C Program
//Check if stack elements are pairwise consecutive
#include <stdio.h>
//For malloc function
#include <stdlib.h>
//Define Stack Structure
struct Stack
{
int data;
struct Stack*next;
};
//Add element into stack
void push(int data,struct Stack**top)
{
//Create a new stack element
struct Stack*new_node = (struct Stack*)malloc(sizeof(struct Stack));
if(new_node!=NULL)
{
new_node->data=data;
new_node->next=*top;
*top=new_node;
}
else
{
printf("\nMemory Overflow\n");
}
}
//remove a top stack elements and return the value of top element
int pop(struct Stack**top)
{
if(*top!=NULL)
{
struct Stack*auxiliary=*top;
int data = auxiliary->data;
*top=auxiliary->next;
//Remove node
free(auxiliary);
auxiliary=NULL;
return data;
}
return -1;
}
struct Stack* is_consecutive(struct Stack*top)
{
//This stack variables are used to store actual stack element
struct Stack*temp=NULL;
//This is two auxiliary variables which are used to get pair elements
struct Stack*first=NULL;
struct Stack*second=NULL;
//This is a flag variable which is indicate result
int status = 1;
while(top!=NULL && status==1)
{
push(pop(&top),&temp);
//Get first pair element
first = temp;
if(top != NULL)
{
//When in case have two pair element exists
push(pop(&top),&temp);
//Get second pair element
second = temp;
//Compare two stack element
if(first->data != second->data && (second->data+1 != first->data))
{
//When pairwise elements are not consecutive
status=0;
}
}
}
if(status==1)
{
printf("Pairwise sorted");
}
else
{
printf("Not Pairwise sorted");
}
//Remove element of auxiliary stack and insert to actual stack
while(temp!=NULL)
{
//insert element into actual stack
push(pop(&temp),&top);
}
printf("\n");
return top;
}
//Remove given stack elements
void remove_elements(struct Stack**top)
{
struct Stack*temp = NULL;
while(*top != NULL)
{
temp=*top;
*top = temp->next;
printf("%d ",temp->data);
free(temp);
temp=NULL;
}
printf("\n");
}
int main()
{
struct Stack *top1=NULL;
//Add element into stack
push(5, &top1);
push(6, &top1);
push(1, &top1);
push(2, &top1);
top1 = is_consecutive(top1);
//Remove remaining elements into stack
remove_elements(&top1);
struct Stack *top2 = NULL;
//Case 2
push(6, &top2);
push(7, &top2);
push(1, &top2);
push(3, &top2);
push(4, &top2);
top2 = is_consecutive(top2);
//Remove remaining elements into stack
remove_elements(&top2);
return 0;
}
Output
Pairwise sorted
2 1 6 5
Not Pairwise sorted
4 3 1 7 6
/*
C++ Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
class MyStack {
public:
Node *top;
MyStack() {
this->top = NULL;
}
//Add a new element in stack
void push(int data) {
//Make a new stack node
Node *new_node = new Node(data);
if (new_node != NULL) {
new_node->next = this->top;
this->top = new_node;
} else {
cout << "Memory overflow\n";
}
}
//Add a top element in stack
//When stack is empty it returns -1
int pop() {
int temp = -1;
if (this->top != NULL) {
temp = this->top->data;
this->top = this->top->next;
}
return temp;
}
void is_consecutive() {
//This stack variables are used to store actual stack element
MyStack temp = MyStack();
//This is two auxiliary variables which are used to get pair elements
Node *first = NULL;
Node *second = NULL;
//This is a flag variable which is indicate result
bool status = true;
while (this->top != NULL &&
status == true) {
temp.push(this->pop());
//Get first pair element
first = temp.top;
if (this->top != NULL) {
temp.push(this->pop());
//Get second pair element
second = temp.top;
//Compare two stack element
if (first->data != second->data &&
(second->data + 1 != first->data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}
if (status == true) {
cout << "Pairwise sorted : ";
} else {
cout << "Not Pairwise sorted : ";
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != NULL) {
//insert element into actual stack
this->push(temp.pop());
}
}
void remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this->top != NULL) {
cout << " " << this->pop();
}
cout << "\n";
}
};
int main() {
MyStack obj1 = MyStack();
//Add element into stack
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
MyStack obj2 = MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
return 0;
}
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
/*
Java Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public class MyStack {
public Node top;
public MyStack()
{
top = null;
}
//Add a new element in stack
public void push(int data)
{
//Make a new stack node
Node new_node = new Node(data);
if(new_node!=null)
{
new_node.next=top;
top = new_node;
}
else
{
System.out.print("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
public int pop()
{
int temp = -1;
if(top!=null)
{
temp = top.data;
top = top.next;
}
return temp;
}
public void is_consecutive()
{
//This stack variables are used to store actual stack element
MyStack temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
Node first = null;
Node second = null;
//This is a flag variable which is indicate result
boolean status = true;
while (this.top != null && status == true)
{
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null)
{
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element
if (first.data != second.data && (second.data + 1 != first.data))
{
//When pairwise elements are not consecutive
status = false;
}
}
else
{
break;
}
}
if (status == true)
{
System.out.print("Pairwise sorted : ");
}
else
{
System.out.print("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null)
{
//insert element into actual stack
this.push(temp.pop());
}
}
public void remove_nodes()
{
//This function is printing stack element and remove Existing nodes
while(this.top!=null)
{
System.out.print(" "+this.pop());
}
System.out.print("\n");
}
public static void main(String[] args) {
MyStack obj1 = new MyStack();
//Add element into stack
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
MyStack obj2 = new MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
}
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
/*
C# Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
using System;
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class MyStack {
public Node top;
public MyStack() {
top = null;
}
//Add a new element in stack
public void push(int data) {
//Make a new stack node
Node new_node = new Node(data);
if (new_node != null) {
new_node.next = top;
top = new_node;
} else {
Console.Write("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
public int pop() {
int temp = -1;
if (top != null) {
temp = top.data;
top = top.next;
}
return temp;
}
public void is_consecutive() {
//This stack variables are used to store actual stack element
MyStack temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
Node first = null;
Node second = null;
//This is a flag variable which is indicate result
Boolean status = true;
while (this.top != null &&
status == true) {
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null) {
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element
if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;;
}
}
if (status == true) {
Console.Write("Pairwise sorted : ");
} else {
Console.Write("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
this.push(temp.pop());
}
}
public void remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
Console.Write(" " + this.pop());
}
Console.Write("\n");
}
public static void Main(String[] args) {
MyStack obj1 = new MyStack();
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
MyStack obj2 = new MyStack();
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
}
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
<?php
/*
Php Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
public $data;
public $next;
function __construct($data) {
$this->data = $data;
$this->next = null;
}
}
class MyStack {
public $top;
function __construct() {
$this->top = null;
}
//Add a new element in stack
public function push($data) {
//Make a new stack node
$new_node = new Node($data);
if ($new_node != null) {
$new_node->next = $this->top;
$this->top = $new_node;
} else {
echo("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
public function pop() {
$temp = -1;
if ($this->top != null) {
$temp = $this->top->data;
$this->top = $this->top->next;
}
return $temp;
}
public function is_consecutive() {
//This stack variables are used to store actual stack element
$temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
$first = null;
$second = null;
//This is a flag variable which is indicate result
$status = true;
while ($this->top != null &&
$status == true) {
$temp->push($this->pop());
//Get first pair element
$first = $temp->top;
if ($this->top != null) {
$temp->push($this->pop());
//Get second pair element
$second = $temp->top;
//Compare two stack element
if ($first->data != $second->data &&
($second->data + 1 != $first->data)) {
//When pairwise elements are not consecutive
$status = false;
}
} else {
break;
}
}
if ($status == true) {
echo("Pairwise sorted : ");
} else {
echo("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while ($temp->top != null) {
//insert element into actual stack
$this->push($temp->pop());
}
}
public function remove_nodes() {
//This function is printing stack element and remove Existing nodes
while ($this->top != null) {
echo(" ". $this->pop());
}
echo("\n");
}
}
function main() {
$obj1 = new MyStack();
//Add element into stack
$obj1->push(5);
$obj1->push(6);
$obj1->push(1);
$obj1->push(2);
$obj1->is_consecutive();
$obj1->remove_nodes();
$obj2 = new MyStack();
//Case 2
$obj2->push(6);
$obj2->push(7);
$obj2->push(1);
$obj2->push(3);
$obj2->push(4);
$obj2->is_consecutive();
$obj2->remove_nodes();
}
main();
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
/*
Node Js Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class MyStack {
constructor() {
this.top = null;
}
//Add a new element in stack
push(data) {
//Make a new stack node
var new_node = new Node(data);
if (new_node != null) {
new_node.next = this.top;
this.top = new_node;
} else {
process.stdout.write("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
pop() {
var temp = -1;
if (this.top != null) {
temp = this.top.data;
this.top = this.top.next;
}
return temp;
}
is_consecutive() {
//This stack variables are used to store actual stack element
var temp = new MyStack();
//This is two auxiliary variables which are used to get pair elements
var first = null;
var second = null;
//This is a flag variable which is indicate result
var status = true;
while (this.top != null &&
status == true) {
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null) {
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element
if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}
if (status == true) {
process.stdout.write("Pairwise sorted : ");
} else {
process.stdout.write("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
//insert element into actual stack
this.push(temp.pop());
}
}
remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
process.stdout.write(" " + this.pop());
}
process.stdout.write("\n");
}
}
function main(args) {
var obj1 = new MyStack();
//Add element into stack
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
var obj2 = new MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
main();
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
# Python 3 Program
# Check if stack elements are pairwise consecutive
# Stack Node
class Node :
def __init__(self, data) :
self.data = data
self.next = None
class MyStack :
def __init__(self) :
self.top = None
# Add a new element in stack
def push(self, data) :
# Make a new stack node
new_node = Node(data)
if (new_node != None) :
new_node.next = self.top
self.top = new_node
else :
print("Memory overflow\n", end = "")
# Add a top element in stack
# When stack is empty it returns -1
def pop(self) :
temp = -1
if (self.top != None) :
temp = self.top.data
self.top = self.top.next
return temp
def is_consecutive(self) :
# This stack variables are used to store actual stack element
temp = MyStack()
# This is two auxiliary variables which are used to get pair elements
first = None
second = None
# This is a flag variable which is indicate result
status = True
while (self.top != None and status == True) :
temp.push(self.pop())
# Get first pair element
first = temp.top
if (self.top != None) :
temp.push(self.pop())
# Get second pair element
second = temp.top
# Compare two stack element
if (first.data != second.data and(second.data + 1 != first.data)) :
# When pairwise elements are not consecutive
status = False
else :
break
if (status == True) :
print("Pairwise sorted : ", end = "")
else :
print("Not Pairwise sorted : ", end = "")
# Remove element of auxiliary stack and insert to actual stack
while (temp.top != None) :
# insert element into actual stack
self.push(temp.pop())
def remove_nodes(self) :
# This function is printing stack element and remove Existing nodes
while (self.top != None) :
print(" ", self.pop(), end = "")
print("\n", end = "")
def main() :
obj1 = MyStack()
# Add element into stack
obj1.push(5)
obj1.push(6)
obj1.push(1)
obj1.push(2)
obj1.is_consecutive()
obj1.remove_nodes()
obj2 = MyStack()
# Case 2
obj2.push(6)
obj2.push(7)
obj2.push(1)
obj2.push(3)
obj2.push(4)
obj2.is_consecutive()
obj2.remove_nodes()
if __name__ == "__main__":
main()
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
# Ruby Program
# Check if stack elements are pairwise consecutive
# Stack Node
class Node
# Define the accessor and reader of class Node
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end
end
class MyStack
# Define the accessor and reader of class MyStack
attr_reader :top
attr_accessor :top
def initialize()
@top = nil
end
# Add a new element in stack
def push(data)
# Make a new stack node
new_node = Node.new(data)
if (new_node != nil)
new_node.next = @top
@top = new_node
else
print("Memory overflow\n")
end
end
# Add a top element in stack
# When stack is empty it returns -1
def pop()
temp = -1
if (@top != nil)
temp = @top.data
@top = @top.next
end
return temp
end
def is_consecutive()
# This stack variables are used to store actual stack element
temp = MyStack.new()
# This is two auxiliary variables which are used to get pair elements
first = nil
second = nil
# This is a flag variable which is indicate result
status = true
while (self.top != nil &&
status == true)
temp.push(self.pop())
# Get first pair element
first = temp.top
if (self.top != nil)
temp.push(self.pop())
# Get second pair element
second = temp.top
# Compare two stack element
if (first.data != second.data &&
(second.data + 1 != first.data))
# When pairwise elements are not consecutive
status = false
end
else
break
end
end
if (status == true)
print("Pairwise sorted :")
else
print("Not Pairwise sorted :")
end
# Remove element of auxiliary stack and insert to actual stack
while (temp.top != nil)
# insert element into actual stack
self.push(temp.pop())
end
end
def remove_nodes()
# This function is printing stack element and remove Existing nodes
while (self.top != nil)
print(" ", self.pop())
end
print("\n")
end
end
def main()
obj1 = MyStack.new()
# Add element into stack
obj1.push(5)
obj1.push(6)
obj1.push(1)
obj1.push(2)
obj1.is_consecutive()
obj1.remove_nodes()
obj2 = MyStack.new()
# Case 2
obj2.push(6)
obj2.push(7)
obj2.push(1)
obj2.push(3)
obj2.push(4)
obj2.is_consecutive()
obj2.remove_nodes()
end
main()
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
/*
Scala Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node(var next: Node,
var data: Int) {
def this(data: Int) {
this( null,data);
}
}
class MyStack(var top: Node) {
def this() {
this(null);
}
//Add a new element in stack
def push(data: Int): Unit = {
//Make a new stack node
var new_node: Node = new Node(data);
if (new_node != null) {
new_node.next = top;
top = new_node;
} else {
print("Memory overflow\n");
}
}
//Add a top element in stack
//When stack is empty it returns -1
def pop(): Int = {
var temp: Int = -1;
if (top != null) {
temp = top.data;
top = top.next;
}
return temp;
}
def is_consecutive(): Unit = {
//This stack variables are used to store actual stack element
var temp: MyStack = new MyStack();
//This is two auxiliary variables which are used to get pair elements
var first: Node = null;
var second: Node = null;
//This is a flag variable which is indicate result
var status: Boolean = true;
var in_out: Boolean = true;
while (this.top != null &&
status == true && in_out==true) {
temp.push(this.pop());
//Get first pair element
first = temp.top;
if (this.top != null) {
temp.push(this.pop());
//Get second pair element
second = temp.top;
//Compare two stack element
if (first.data != second.data &&
(second.data + 1 != first.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
in_out=false;
}
}
if (status == true) {
print("Pairwise sorted : ");
} else {
print("Not Pairwise sorted : ");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp.top != null) {
//insert element into actual stack
this.push(temp.pop());
}
}
def remove_nodes(): Unit = {
//This function is printing stack element and remove Existing nodes
while (this.top != null) {
print(" " + this.pop());
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj1: MyStack = new MyStack();
//Add element into stack
obj1.push(5);
obj1.push(6);
obj1.push(1);
obj1.push(2);
obj1.is_consecutive();
obj1.remove_nodes();
var obj2: MyStack = new MyStack();
//Case 2
obj2.push(6);
obj2.push(7);
obj2.push(1);
obj2.push(3);
obj2.push(4);
obj2.is_consecutive();
obj2.remove_nodes();
}
}
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
/*
Swift Program
Check if stack elements are pairwise consecutive
*/
//Stack Node
class Node {
var data: Int;
var next: Node? ;
init(_ data: Int) {
self.data = data;
self.next = nil;
}
}
class MyStack {
var top: Node? ;
init() {
self.top = nil;
}
//Add a new element in stack
func push(_ data: Int) {
//Make a new stack node
let new_node: Node? = Node(data);
if (new_node != nil) {
new_node!.next = self.top;
self.top = new_node;
} else {
print("Memory overflow\n", terminator: "");
}
}
//Add a top element in stack
//When stack is empty it returns -1
func pop() -> Int {
var temp: Int = -1;
if (self.top != nil) {
temp = self.top!.data;
self.top = self.top!.next;
}
return temp;
}
func is_consecutive() {
//This stack variables are used to store actual stack element
let temp: MyStack? = MyStack();
//This is two auxiliary variables which are used to get pair elements
var first: Node? = nil;
var second: Node? = nil;
//This is a flag variable which is indicate result
var status: Bool = true;
while (self.top != nil &&
status == true) {
temp!.push(self.pop());
//Get first pair element
first = temp!.top;
if (self.top != nil) {
temp!.push(self.pop());
//Get second pair element
second = temp!.top;
//Compare two stack element
if (first!.data != second!.data &&
(second!.data + 1 != first!.data)) {
//When pairwise elements are not consecutive
status = false;
}
} else {
break;
}
}
if (status == true) {
print("Pairwise sorted : ", terminator: "");
} else {
print("Not Pairwise sorted : ", terminator: "");
}
//Remove element of auxiliary stack and insert to actual stack
while (temp!.top != nil) {
//insert element into actual stack
self.push(temp!.pop());
}
}
func remove_nodes() {
//This function is printing stack element and remove Existing nodes
while (self.top != nil) {
print(" ", self.pop(), terminator: "");
}
print("\n", terminator: "");
}
}
func main() {
let obj1: MyStack? = MyStack();
//Add element into stack
obj1!.push(5);
obj1!.push(6);
obj1!.push(1);
obj1!.push(2);
obj1!.is_consecutive();
obj1!.remove_nodes();
let obj2: MyStack? = MyStack();
//Case 2
obj2!.push(6);
obj2!.push(7);
obj2!.push(1);
obj2!.push(3);
obj2!.push(4);
obj2!.is_consecutive();
obj2!.remove_nodes();
}
main();
Output
Pairwise sorted : 2 1 6 5
Not Pairwise sorted : 4 3 1 7 6
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment