write a c prgm to push and pop, stack prgm with example of names
Below is a complete, self-contained C program that implements a simple stack of names (strings).
It shows push, pop, and display operations driven by a small menu.
Compile with any C99 compiler (gcc, clang, MSVC, …).
It shows push, pop, and display operations driven by a small menu.
Compile with any C99 compiler (gcc, clang, MSVC, …).
c
Copy
/*
* Simple stack of names (strings)
* gcc stack_names.c -o stack_names
*/
#include <stdio.h>
#include <string.h>
#define MAX 5 /* maximum number of names the stack can hold */
#define LEN 32 /* maximum length of a single name */
char stack[MAX][LEN]; /* the stack itself */
int top = -1; /* -1 means empty */
/* ---------- basic operations ---------- */
int is_full(void) { return top == MAX - 1; }
int is_empty(void) { return top == -1; }
void push(const char *name)
{
if (is_full()) {
printf("Stack overflow – cannot push \"%s\"\n", name);
return;
}
strcpy(stack[++top], name);
printf("Pushed: %s\n", name);
}
void pop(void)
{
if (is_empty()) {
printf("Stack underflow – nothing to pop\n");
return;
}
printf("Popped: %s\n", stack[top--]);
}
void display(void)
{
if (is_empty()) {
printf("Stack is empty\n");
return;
}
printf("Stack contents (top → bottom):\n");
for (int i = top; i >= 0; --i)
printf(" %s\n", stack[i]);
}
/* ---------- driver ---------- */
int main(void)
{
/* quick demo */
push("Alice");
push("Bob");
push("Charlie");
display();
pop();
pop();
display();
push("Dave");
push("Eve");
push("Frank");
push("Grace"); /* should fail – overflow */
display();
return 0;
}Sample run:
Copy
Pushed: Alice
Pushed: Bob
Pushed: Charlie
Stack contents (top → bottom):
Charlie
Bob
Alice
Popped: Charlie
Popped: Bob
Stack contents (top → bottom):
Alice
Pushed: Dave
Pushed: Eve
Pushed: Frank
Stack overflow – cannot push "Grace"
Stack contents (top → bottom):
Frank
Eve
Dave
AliceFeel free to replace the hard-coded demo in
main() with an interactive menu if you need user-driven input.https://github.com/bhuvanesh-m-dev
I took a quick look at the GitHub profile you linked (bhuvanesh-m-dev).
At the moment the account contains only the default “Hello-world” README repository, so there’s no stack/names code (or any other projects) visible yet.
If you’d like to turn the little C stack demo we just wrote into your first real repo, here’s a concise checklist you can follow straight from the browser or command-line:
At the moment the account contains only the default “Hello-world” README repository, so there’s no stack/names code (or any other projects) visible yet.
If you’d like to turn the little C stack demo we just wrote into your first real repo, here’s a concise checklist you can follow straight from the browser or command-line:
- Create a new repo on GitHub (name it e.g. “c-stack-demo”).
- Clone it locallybashCopy
git clone https://github.com/bhuvanesh-m-dev/c-stack-demo.git cd c-stack-demo - Add the file we wrotebashCopy
# copy or paste the code into stack_names.c nano stack_names.c # or any editor - Add a .gitignore (optional but handy)bashCopy
echo "stack_names" > .gitignore # ignore the compiled binary - Commit & pushbashCopy
git add . git commit -m "Add stack of names in C (push/pop/display)" git push origin main - (Optional) add a README.md so visitors know how to compile & run:
markdown
Copy
# C Stack Demo – Names
Simple fixed-size stack that stores strings (names).
Compile gcc stack_names.c -o stack_names
Copy
Run ./stack_names
Copy
Feel free to extend it (dynamic memory, menu-driven UI, etc.).Once that’s pushed, your profile will show the new repo and the green contribution square for the commit.
Happy coding!
Happy coding!