Home > Enterprise >  Deletion in queue from front in Python
Deletion in queue from front in Python

Time:11-06

I have written a program for implementaion of queue using Python. Insertion and display of the queue works fine. But I can't delete element from front of queue. I have tried del keyword as well as pop method. In both the ways, the element is deleted from fron of the queue, insted it deleted from front 1. For example, the queue is [15, 25, 78, 93, 41, 589]. If I chose to delete element from fron, the element 15 is deleted. But if I again chose to delete element from front, the element 78 is deleted insted of 25. Following is the function of deleteion operation from front:

def dequeue():
    global front, rear, queue
    if (is_queue_empty()):
        print("\nQueue is empty.")
    else:
        item = queue[front]
        queue.pop(front)
        if (front == rear):
            front = -1
            rear = -1
        else:
            front = front   1
        print("\nDeleted element is ", item)

Follwing is my original program:

def is_queue_empty():
    if (front == -1):
        return 1
    else:
        return 0


def is_queue_full():
    if (rear == maxsize - 1):
        return 1
    else:
        return 0


def enqueue():
    global front, rear, queue, item
    if (is_queue_full()):
        print("\nQueue is full.")
    else:
        item = int(
            input("\nEnter the item at position "   str(rear   1)   ": "))
        if (front == -1):
            front = 0
            rear = 0
        else:
            rear = rear   1
        queue.append(item)


def dequeue():
    global front, rear, queue
    if (is_queue_empty()):
        print("\nQueue is empty.")
    else:
        item = queue[front]
        queue.pop(front)
        if (front == rear):
            front = -1
            rear = -1
        else:
            front = front   1
        print("\nDeleted element is ", item)


# print the list
def display():
    print("\nQueue Size = ", maxsize, ", Number Of Elements In The Queue = ",
          (rear - front)   1, ", Occupied Space In The Queue = ", rear   1,
          ", Available Size In The Queue = ", maxsize - (rear   1))
    if (is_queue_empty()):
        print("\nQueue is empty.")
    else:
        print("\nYour queue: ", queue)


def set_queue_size():
    global maxsize
    maxsize = int(input("Enter the size of the queue: "))


queue = []  # Initializing an empty queue
front = rear = -1

# Driver Code
set_queue_size()

print("\n1. EnQueue")
print("\n2. DeQueue")
print("\n3. Display")
print("\n4. Update the size of the queue")
print("\n5. Exit")

while (1):
    ch = int(input("\nEnter your choice: "))

    if (ch == 1):
        enqueue()
        continue
    elif (ch == 2):
        dequeue()
        continue
    elif (ch == 3):
        display()
        continue
    elif (ch == 4):
        set_queue_size()
        continue
    elif (ch == 5):
        break
    else:
        print("\nWrong choice. Try again")
        continue

NOTE: I am trying to write the program without importing any Python library

CodePudding user response:

Look at below. You don't need front index. When you popped front item. it will remove that item completely from queue. That is why you don't need it. But in your code you every dequeue increased front.

But python will automatically removes it from queue.

front=0 always. So just use queue.pop(0).

Look at below.

def is_queue_empty():
    if (rear == 0):
        return 1
    else:
        return 0


def is_queue_full():
    if (rear == maxsize):
        return 1
    else:
        return 0


def enqueue():
    global rear, queue, item
    if (is_queue_full()):
        print("\nQueue is full.")
    else:
        item = int(
            input("\nEnter the item at position "   str(rear)   ": "))
        rear = rear   1
        queue.append(item)


def dequeue():
    global rear, queue
    if (is_queue_empty()):
        print("\nQueue is empty.")
    else:
        item = queue[0]
        queue.pop(0)
        rear = rear - 1
        print("\nDeleted element is ", item)


# print the list
def display():
    print("\nQueue Size = ", maxsize, ", Number Of Elements In The Queue = ",
        (rear)   1, ", Occupied Space In The Queue = ", rear,
        ", Available Size In The Queue = ", maxsize - rear)
    if (is_queue_empty()):
        print("\nQueue is empty.")
    else:
        print("\nYour queue: ", queue)


def set_queue_size():
    global maxsize
    maxsize = int(input("Enter the size of the queue: "))


queue = []  # Initializing an empty queue
rear = 0

# Driver Code
set_queue_size()

print("\n1. EnQueue")
print("\n2. DeQueue")
print("\n3. Display")
print("\n4. Update the size of the queue")
print("\n5. Exit")

while (1):
    ch = int(input("\nEnter your choice: "))

    if (ch == 1):
        enqueue()
        continue
    elif (ch == 2):
        dequeue()
        continue
    elif (ch == 3):
        display()
        continue
    elif (ch == 4):
        set_queue_size()
        continue
    elif (ch == 5):
        break
    else:
        print("\nWrong choice. Try again")
        continue
  • Related