//using PUSH, POP and DISPLAY functions
#include<stdio.h>
#include<stdlib.h>

struct node{
    int data;
    struct node* next;
};
struct node* top=NULL;

void push(){
    struct node* neww= (struct node*)malloc(sizeof(struct node));
    scanf("%d",&neww->data);
    neww->next=top;
    top=neww;
    printf("Node is Inserted\n");
}

void pop(){
    struct node* temp=top;
    if(top==NULL){
        printf("EMPTY STACK");
    }
    else{
    printf("Popped Element : %d\n",temp->data);
    top=top->next;
    free(temp);
    }
}

void display(){
    if(top!=NULL){
        printf("The stack is \n");
        while(top!=NULL){
            printf("%d ",top->data);
            top=top->next;
        }
    }
    else{
        printf("EMPTY STACK");
    }
}

int main(){
    int n;
    while(true){
    scanf("%d",&n);
    switch(n){
        case 1:
        push();
        break;
        
        case 2:
        pop();
        break;
        
        case 3:
        display();
        break;
        
        case 4:
        exit(0);
        
        default:
        printf("Wrong choice");
    }
    }
    return 0;
}

//INPUT:
1
10
1
20
1
30
1
40
1
50
2
2
3
4
  
//OUTPUT:
Node is Inserted
Node is Inserted
Node is Inserted
Node is Inserted
Node is Inserted
Popped Element : 50
Popped Element : 40
The stack is 
30 20 10 // C++ program to Implement a stack
//using singly linked list
#include <bits/stdc++.h>
using namespace std;
 
// Declare linked list node
 
struct Node
{
    int data;
    struct Node* link;
};
 
struct Node* top;
 
// Utility function to add an element
// data in the stack insert at the beginning
void push(int data)
{
     
    // Create new node temp and allocate memory
    struct Node* temp;
    temp = new Node();
 
    // Check if stack (heap) is full.
    // Then inserting an element would
    // lead to stack overflow
    if (!temp)
    {
        cout << "\nHeap Overflow";
        exit(1);
    }
 
    // Initialize data into temp data field
    temp->data = data;
 
    // Put top pointer reference into temp link
    temp->link = top;
 
    // Make temp as top of Stack
    top = temp;
}
 
// Utility function to check if
// the stack is empty or not
int isEmpty()
{
    return top == NULL;
}
 
// Utility function to return top element in a stack
int peek()
{
     
    // Check for empty stack
    if (!isEmpty())
        return top->data;
    else
        exit(1);
}
 
// Utility function to pop top
// element from the stack
void pop()
{
    struct Node* temp;
 
    // Check for stack underflow
    if (top == NULL)
    {
        cout << "\nStack Underflow" << endl;
        exit(1);
    }
    else
    {
         
        // Top assign into temp
        temp = top;
 
        // Assign second node to top
        top = top->link;
 
        // Destroy connection between
        // first and second
        temp->link = NULL;
 
        // Release memory of top node
        free(temp);
    }
}
 
// Function to print all the
// elements of the stack
void display()
{
    struct Node* temp;
 
    // Check for stack underflow
    if (top == NULL)
    {
        cout << "\nStack Underflow";
        exit(1);
    }
    else
    {
        temp = top;
        while (temp != NULL)
        {
 
            // Print node data
            cout << temp->data << "-> ";
 
            // Assign temp link to temp
            temp = temp->link;
        }
    }
}
 
// Driver Code
int main()
{
     
    // Push the elements of stack
    push(11);
    push(22);
    push(33);
    push(44);
 
    // Display stack elements
    display();
 
    // Print top element of stack
    cout << "\nTop element is "
         << peek() << endl;
 
    // Delete top elements of stack
    pop();
    pop();
 
    // Display stack elements
    display();
 
    // Print top element of stack
    cout << "\nTop element is "
         << peek() << endl;
          
    return 0;
}
 
// This code is contributed by Striverpublic class LinkedListStack {
    private Node head; // the first node
 
    // nest class to define linkedlist node
    private class Node {
        int value;
        Node next;
    }
 
    public LinkedListStack() {
        head = null;
    }
 
    // Remove value from the beginning of the list for demonstrating behaviour of stack
    public int pop() throws LinkedListEmptyException {
        if (head == null) {
            throw new LinkedListEmptyException();
        }
        int value = head.value;
        head = head.next;
        return value;
    }
 
    // Add value to the beginning of the list for demonstrating behaviour of stack
    public void push(int value) {
        Node oldHead = head;
        head = new Node();
        head.value = value;
        head.next = oldHead;
    }
 
    public static void main(String args[]) 
    {
        LinkedListStack lls=new LinkedListStack();
        lls.push(20);
        lls.push(50);
        lls.push(80);
        lls.push(40);
        lls.push(60);
        lls.push(75);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        System.out.println("Element removed from LinkedList: "+lls.pop());
        lls.push(10);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        printList(lls.head);
    }
    public static void printList(Node head) {
        Node temp = head;
        while (temp != null) {
            System.out.format("%d ", temp.value);
            temp = temp.next;
        }
        System.out.println();
    }
}
 
/**
 * 
 * Exception to indicate that LinkedList is empty.
 */
 
class LinkedListEmptyException extends RuntimeException {
    private static final long serialVersionUID = 1L;
 
    public LinkedListEmptyException() {
        super();
    }
 
    public LinkedListEmptyException(String message) {
        super(message);
    }
}#include<iostream>
using namespace std;
class node{
	public:
		int data;
		node* next;
		void push();
		void pop();
		void display();
};
node *top=NULL;
void node::push()
{
	node* newnode;
	newnode=new node();
	cout<<"Enter data : ";
	cin>>newnode->data;
	newnode->next=top;
	top=newnode;
}
void node::pop()
{
	if(top==NULL)
	{
		cout<<"Stack underflow!";
	}
	else
	{
		cout<<"poped element is : "<<top->data;
		top=top->next;
	}
}

void node::display()
{
	node *temp=top;
	if(temp==NULL)
	{
		cout<<"\nThe Stack is Empty!\n";
	}
	else
	{
	while(temp!=NULL)
	{
		cout<<temp->data<<" ";
		temp=temp->next;
	}
	}
}
int main()
{
	node obj;
 int ch;
   
    while(ch!=4){
    	cout<<"\n1) Push in stack"<<endl;
   		cout<<"2) Pop from stack"<<endl;
   		cout<<"3) Display stack"<<endl;
  		cout<<"4) Exit"<<endl;
    	cout<<"Enter choice: "<<endl;
      cin>>ch;
      switch(ch) {
         case 1: {
            obj.push();
            break;
         }
         case 2: {
            obj.pop();
            break;
         }
         case 3: {
            obj.display();
            break;
         }
         case 4: {
            exit(0);
            break;
         }
         default: {
            cout<<"Invalid Choice"<<endl;
         }
	
}
}
}

C相关代码片段

openGL create shape

script to check program running and restart

printf("")

Rewind to a commit Git

mouse click for colab

how to remove newline in the end of fgets string

fit text to conatiner flutter

gorm string array

js observe url change

clock skew detected makefile

time() function in c

worst fit code in c

first fit code in c

roem evaluation pyspark

calculator c

safet algorith im c

c copy char*

jfug function

soil moisture sensor interfacing with ESP32

flutter sidebar menu example

access images in c panel htaccess file

c include header file in the folder aboce

c include libray

gcc name output file

C data link escape char

How to type DLE in C

vscode Ctrl+d undo

C[strtol] func

pointer c++

power in c

sort three numbers

strip in c

c time duration

overleaf itemize a b c

route groups

itoa c code

queue in c

'wsgirequest' object has no attribute 'get'

c copy file

how to print array data in html using javascript

hello world program in c

how to compare a variable with a string in c

c print array

c rand range

exponents without pow c

how to reversed number in c

c random number with range

pthread mutex destroy while lock

pthread init mutex twice

insert c or p

woocommerce align add to cart buttons

vscode fold all code when open file

commit github non funziona

makefile both c and c++

scanf with space

c macro variable arguments

c printf format

check if prime c

read file line by line c

c use define in string

pattern program in c

Write the code in C to implement selection sort

c program of Goldbach conjecture

otput only one decimal point in c

trim string c

tokenize string c

firebase message in app doesn't work

implementing stack using linked list

esercizi semplici con le funzioni in c

array to linked list in c

sorted array to bst implementation in c

doubly linked list insertion in the middle

arch distro

distance vector routing algorithm

implement typedef with arrays

c exit vs _exit

explain in simple with c code

convert matrix to lower triangular matrix

Addition and subtraction of matrix

Transpose of a matrix