-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacknqueuell
More file actions
73 lines (62 loc) · 1.18 KB
/
Copy pathstacknqueuell
File metadata and controls
73 lines (62 loc) · 1.18 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
/* stack and queue using array difference */
#include<stdio.h>
#include<stdlib.h>
/* linked list node struct */
struct node {
int data;
struct node *next;
};
/* for stack we use push and pop operation and for Queue we use enqueue/dequeue
Stack is LIFO: Last In First Out
Queue is FIFO: First In First Out
/*
// stack
Push(int x)
{
stuct node *temp = (strcut node *) malloc(sizeof(struct node));
if (head == NULL)
{
head = temp;
temp->data = x;
temp->next = NULL;
return;
}
temp->data = x;
temp->next = head;
head = temp;
}
Pop()
{
struct node *temp = head;
head = temp->next;
free(temp);
}
//queue
struct node * rear = NULL;
struct node * front = NULL;
Enqueue(int x)
{
struct node *temp = (struct node*)malloc (sizeof(struct node));
temp->data = x;
temp->next = NULL;
if (font == NULL) && (rear == NULL)
{
front = rear = temp;
return;
}
rear->next = temp;
}
Dequeue(int x)
{
if (front == NULL) return;
struct node *temp = front;
if (front == rear)
{
front = rear = NULL;
}
else
{
front = front->next;
}
free(temp);
}