File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed
data_structures/linked_list Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -36,40 +36,40 @@ int main()
36
36
}
37
37
}
38
38
39
- void push (struct node * p )
39
+ void push (struct node * p ) // push function will add a new node at the head of the list, time complexity O(1).
40
40
{
41
41
int item ;
42
42
struct node * temp ;
43
- temp = (struct node * )malloc (sizeof (struct node ));
43
+ temp = (struct node * )malloc (sizeof (struct node )); //creating memory for temp node.
44
44
printf ("enter element to be inserted\n" );
45
45
scanf ("%d" , & item );
46
46
temp -> info = item ;
47
47
48
48
temp -> link = top ;
49
- top = temp ;
49
+ top = temp ; //top of the stack points to newly added node.
50
50
51
51
printf ("inserted succesfully\n" );
52
52
}
53
- void pop (struct node * p )
53
+ void pop (struct node * p ) // pop function deletes the first node from the head, time complexity O(1).
54
54
{
55
55
int item ;
56
56
struct node * temp ;
57
57
58
- if (top == NULL )
58
+ if (top == NULL ) // Underflow condition
59
59
printf ("stack is empty\n" );
60
60
else
61
61
{
62
62
item = top -> info ;
63
63
temp = top ;
64
- top = top -> link ;
65
- free (temp );
64
+ top = top -> link ; // new top will be the node pointed by previous top.
65
+ free (temp ); // free the memory of node using free().
66
66
printf ("Element popped is%d\n" , item );
67
67
}
68
68
}
69
69
70
70
void display (struct node * p )
71
71
{
72
- if (top == NULL )
72
+ if (top == NULL ) // Underflow condition
73
73
printf ("stack is empty\n" );
74
74
else
75
75
{
@@ -79,6 +79,5 @@ void display(struct node *p)
79
79
printf ("%d\n" , p -> info );
80
80
p = p -> link ;
81
81
}
82
- // printf("%d\n",p->info);
83
82
}
84
83
}
You can’t perform that action at this time.
0 commit comments