-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-linkedlist.c
More file actions
53 lines (53 loc) · 1.22 KB
/
stack-linkedlist.c
File metadata and controls
53 lines (53 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<stdlib.h>
//Stack is LIFO, last in first out
//Using singly linked list either we can insert either from the beginning or from the end.
//Time Complexity= O(n)---- Beginning
//Time complexity= O(1)---- End {Bcz we need to traverse the linked list from the begg till the end}
typedef struct Node{
int data;
struct Node* nextptr;
}Node;
/// @brief This structure will be used to create the linked list.
Node* head= NULL;
Node* newnode(){
Node* temp= (Node*) malloc(sizeof(Node));
temp-> nextptr= NULL;
return temp;
}
void push(int data){ // Insertion at the begg
Node* temp= newnode();
temp->data= data;
if(head== NULL)
head= temp;
else{
temp->nextptr= head;
head= temp;
}
}
void pop(){ //Deletion from the begg
Node* ptr= head;
if(head== NULL)
return;
head= ptr->nextptr;
free(ptr);
}
void printstack(){
Node* ptr= head;
while(ptr != NULL){
printf("\n%d", ptr->data);
ptr= ptr->nextptr;
}
}
void peek(){
printf("\n%d", *(head));
}
int main(){
push(20);
push(30);
push(34);
pop();
push(56);
peek();
printstack();
}