-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackUsingLinkedList.c
More file actions
96 lines (90 loc) · 1.79 KB
/
Copy pathstackUsingLinkedList.c
File metadata and controls
96 lines (90 loc) · 1.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void triversal(struct node *head)
{
struct node *ptr = head;
do
{
printf("%d\n", ptr->data);
ptr = ptr->next;
} while (ptr != NULL);
}
int isFull(struct node *head)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
return 1;
return 0;
}
int isEmply(struct node *top)
{
if (top == NULL)
return 1;
return 0;
}
struct node *push(struct node *head, int val)
{
if (isFull(head))
{
printf("stack overflow\n");
}
else
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = val;
ptr->next = head;
head = ptr;
return head;
}
}
//there is other method as well by maiking (struct node* head=NULL)
//as global variable
int pop(struct node **head)
{
if (isEmply(*head))
{
printf("stack underflow");
}
else
{
struct node *p = *head;
int x= p->data;
*head = (*head)->next;
free(p);
return x;
}
}
int peek( struct node* head, int pos)
{
struct node * ptr= head;
for(int i=1;(i<=pos-1 && ptr!=NULL);i++)
{
ptr=ptr->next;
}
if(ptr!=NULL)
{
return ptr->data;
}
return -1;
}
int main()
{
//this
struct node* head=NULL;
head = push(head,2);
head = push(head,34);
head = push(head,26);
head = push(head,6);
head = push(head,8);
printf("value at position 3:%d\n",peek(head,3));
printf("after\n");
triversal(head);
printf("Popped element is %d\n",pop(&head));
triversal(head);
return 0;
}