Contoh Program Queue Dengan Linked List

class item //atau node/simpul
{
public int data; // data item
public item next; // next node link in list
public item prev; // previous node link in list
public item(int id) // constructor
{
data = id; // initialize data
} // set to null)
public void displayLink() // display ourself
{
System.out.print('{' + data + '} ');
}
} // end class Link
class StackLinkList
{
private item top; // ref to first link on list
private item bottom; // ref to last link on list
public StackLinkList() // constructor
{
top = bottom = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (topnull);
}
public void push(int id) //node baru selalu di top
{ // make new link
item newitem = new item(id);
if (top null) // the first node created
{
top = bottom = newitem; // first --> newLink
}
else // the second node and the next node
{
top.next = newitem; //next dr top (awal) diarahkan ke node baru
newitem.prev = top; //prev dr node baru diarahkan ke tail (awal)
top = newitem; //top (baru) diarahkan ke node baru
}
}
public item pop() // delete first item
{ item temp = null;
if (top null) // stack is empty
System.out.println('Stack is empty');
else if (top bottom) // stack is only one data
{
temp = top;
top = bottom = null;
}
else // stack has more than one data
{
temp = top; // save reference to link
top = top.prev; // delete it: first-->old next
top.next = null;
}
return temp;
}
public void display()
{
item current = bottom; // start from the first data
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println(');
}
} // end class LinkList
class StackLinkListApp
{
public static void main(String[] args)
{
StackLinkList theStack = new StackLinkList(); // make new list
System.out.println('Initializing Stack..');
theStack.push(22); // insert four items
theStack.push(44);
theStack.push(66);
theStack.push(88);
System.out.println('Display Forward :');
theStack.display(); // display list
System.out.println('Delete Stack from Top..');
while( !theStack.isEmpty() ) // until it's empty,
{
item aLink = theStack.pop(); // delete link
System.out.print('Deleted '); // display it
aLink.displayLink();
System.out.println(');
}
theStack.display(); // display list
} // end main()
}

Download buku berbayar gratis di playstore. Here you will learn about linear queue in C++.

What is Queue?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends that are known as front and rear. The queue follows FIFO (First in First Out) concept. First element added to the queue will be first one to be removed.
In this program we will implement linear queue using linked list. It is a menu driven program that contains four options insert, delete, display and exit. The program will ask the user to enter the choice and then appropriate functions are invoked to perform specific operation according to the user’s choice.
Also Read: Circular Queue in C

Program for Linear Queue in C++

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
#include<stdlib.h>
usingnamespacestd;
structnode
intdata;
}*front=NULL,*rear,*temp;
voidins()
temp=newnode;
cin>>temp->data;
front=rear=temp;
{
rear=temp;
}
voiddel()
if(frontNULL)
else
temp=front;
cout<<'Deleted node is '<<temp->data<<'n';
}
{
cout<<'Queue is emptyn';
{
while(temp!=NULL)
cout<<temp->data<<'->';
}
}
intmain()
intch;
{
cout<<'nn*** Menu ***'<<'n1.Insertn2.Deleten3.Displayn4.Exit';
cin>>ch;
{
break;
break;
break;
break;
}
}
Output
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):1
Enter data:8
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):1
Enter data:12
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):3

8->12->

*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit

Enter your choice(1-4):4

In the insertion operation, temp points to the new node. If this is first node to be inserted then front will be NULL and now both front and rear points to this new node. If front is not NULL then insertion is similar to adding the node at the end of linked list. The next pointer of rear points to temp and rear becomes temp.
Deletion
For deletion purpose, it is first checked whether front is NULL, if it is NULL, we display the message “Queue is empty”. In case the queue is not empty, deletion is done in such a way that temp pointer points to front and front pointer points to its next node. After displaying data for the node to be deleted, node is deleted by delete(temp) function.
Display
For display, it is first checked whether front is NULL, if it is NULL, we display the message “Queue is empty”. If queue is not empty, front pointer is assigned to temp and data for all the nodes are displayed till temp does not become NULL.
If you have any doubts related to above linear queue in C++ program then you can ask it by commenting below.

You May Also Like: