Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee2420d

Browse files
stack program improved version which uses dynamic memory allocation concept
1 parent d8caa4f commit ee2420d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is stack program implemented in c language it uses the concept of dynamic memory allocation in c,structure and pointers.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<stdio.h>
2+
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
}*top = NULL;
9+
10+
void push(int);
11+
void pop();
12+
void display();
13+
14+
void main()
15+
{
16+
int choice, value;
17+
printf("\n:: Stack using Linked List ::\n");
18+
while(1){
19+
printf("\n****** MENU ******\n");
20+
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
21+
printf("Enter your choice: ");
22+
scanf("%d",&choice);
23+
switch(choice){
24+
case 1: printf("Enter the value to be insert: ");
25+
scanf("%d", &value);
26+
push(value);
27+
break;
28+
case 2: pop(); break;
29+
case 3: display(); break;
30+
case 4: exit(0);
31+
default: printf("\nWrong selection!!! Please try again!!!\n");
32+
}
33+
}
34+
}
35+
void push(int value)
36+
{
37+
struct Node *newNode;
38+
newNode = (struct Node*)malloc(sizeof(struct Node));
39+
newNode->data = value;
40+
if(top == NULL)
41+
newNode->next = NULL;
42+
else
43+
newNode->next = top;
44+
top = newNode;
45+
printf("\nInsertion is Success!!!\n");
46+
}
47+
void pop()
48+
{
49+
if(top == NULL)
50+
printf("\nStack is Empty!!!\n");
51+
else{
52+
struct Node *temp = top;
53+
printf("\nDeleted element: %d", temp->data);
54+
top = temp->next;
55+
free(temp);
56+
}
57+
}
58+
void display()
59+
{
60+
if(top == NULL)
61+
printf("\nStack is Empty!!!\n");
62+
else{
63+
struct Node *temp = top;
64+
while(temp->next != NULL){
65+
printf("%d--->",temp->data);
66+
temp = temp -> next;
67+
}
68+
printf("%d--->NULL",temp->data);
69+
}
70+
}
71+
72+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /