Visitar URL original
Stack Array implementation , Stack Linked List Implementation by ShivamSGokarankar · Pull Request #2198 · TheAlgorithms/Java · GitHub
The Wayback Machine - https://web.archive.org/web/20210605234512/https://github.com/TheAlgorithms/Java/pull/2198/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack Array implementation , Stack Linked List Implementation #2198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter
Filter file types
Beta Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,120 @@
package DataStructures.Stacks;

public class ArrayImplementation {

static int stack[] = new int[5];
static int max=stack.length;
static int top=-1;
static int tmptop=0;
static int tmpstack[];
int temp ;
public void push(int data )
{

if(isFull())
{
System.out.println("stack is full");
}
else
{
top++;
stack[top]=data;
}
}
public int pop()
{
if(isEmpty())
{

return -1;
}
else
{
int i =stack[top];
stack[top]=0;
top--;
return i ;
}

}
public void show(int stack[])
{

for(int i = max-1;i>-1;i--)
{
if(stack[i]==0)
{
System.out.println(" - ");
}else
{
System.out.println(" " +stack[i]+ " ");
}
}

}
public void sort(int stack[])
{
tmpstack=new int[stack.length];

while(!isEmpty())
{
temp = pop();
while(tmptop!=-1 && tmpstack[tmptop]>temp)
{
push(tmpstack[tmptop]);
tmptop--;

}
tmpstack[tmptop]=temp;
tmptop++;

}
top=max-1;
stack=tmpstack;

while(!isEmpty())
{
System.out.println(stack[top]);
top--;
}

}
public boolean isFull()
{
if(top>=max-1)
{
return true;
}
return false;
}
public boolean isEmpty()
{
if(top==-1)
{

return true;
}
return false;

}

/*...................................................*/
public static void main(String[] args)
{

ArrayImplementation a= new ArrayImplementation();

a.push(10);
a.push(20);
a.push(30);
a.push(40);
a.push(50);
a.push(100);
a.show(stack);

//System.out.println("after sorting In ascending order ");
//a.sort(stack);
}


}
@@ -0,0 +1,127 @@
package DataStructures.Stacks;
import linkedList.Node;
public class LinkedListImplementation {
Node head;
Node top;
Node prev;
int counter =0;


public void push(int data)
{

Node n = new Node();
n.data=data;

if(isEmpty())
{
head=n;

}
else
{
top.next=n;
prev=top;


}

top=n;
counter++;
}
public void pop()
{
if(isEmpty())
{
System.out.println("stack is emtpy");
}
else
{
if(counter==1)
{
top=null;
head=null;
System.out.println("Done!!!");
}
else
{
Node i = head;
while(i.next!=null)
{
prev =i ;
i=i.next;
}
prev.next=null;
top =prev;

counter++;

}
}
}
public boolean isEmpty()
{

if(head==null)
{
return true;
}
else
{
return false;
}
}
public void show()
{

if(isEmpty())
{
System.out.println("Nothing in stack to show ");
return;
}
else
{
Node i =head;

int arr[]= new int[counter];

for(int x=counter-1; x>=0;x--)
{
arr[x]=i.data; ////
i=i.next;
}

for(int c =0;c<arr.length;c++)
{
System.out.println(arr[c]);
}
}
}


public static void main(String[] args) {

LinkedListImplementation l = new LinkedListImplementation();
l.push(10);
l.push(20);
l.push(30);
l.push(40);

l.pop();

l.show();
l.pop();
System.out.println("....");
l.show();

System.out.println("....");
System.out.println(l.prev.data);






}

}
ProTip! Use n and p to navigate between commits in a pull request.