Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Updated the coding structure for more readability. #1393

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 39 additions & 44 deletions data_structures/linked_list/ascending_priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,39 @@ delete() - Would delete the smallest element in the queue

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NULL ((void *)0)

struct node
typedef struct node
{
int data;
struct node *next;
};
}node;

struct node *front, *rear;
node *front, *rear;

/* This function initializes the queue to empty by making both front and rear as
* NULL */
void createqueue() { front = rear = NULL; }

int empty()
int isempty()
{
if (front == NULL)
return 1;
else
return 0;
return front == NULL;
}

void insert(int x)
void insert(int data)
{
struct node *pnode;

pnode = (struct node *)malloc(sizeof(struct node));
node *pnode = (node *)malloc(sizeof(node));
if (pnode == NULL)
{
printf("Memory overflow. Unable to insert.\n");
exit(1);
}

pnode->data = x;
pnode->data = data;
pnode->next = NULL; /* New node is always last node */

if (empty())
if (isempty())
front = rear = pnode;
else
{
Expand All @@ -71,72 +67,70 @@ void insert(int x)

int removes()
{
int min;
struct node *follow, *follow1, *p, *p1;

if (empty())
node *follow, *follow1, *temp, *target_node;

if (isempty())
{
printf("\nQueue Underflow. Unable to remove.");
exit(1);
}

/* finding the node with minimum value in the APQ.*/
p = p1 = front;
follow = follow1 = NULL;
min = front->data;
while (p != NULL)
{
if (p->data < min)
int min = front->data;
for(temp = target_node = front; temp!=NULL; temp=temp->next){
if (temp->data < min)
{
min = p->data;
min = temp->data;
follow1 = follow;
p1 = p;
target_node = temp;
}
follow = p;
p = p->next;
follow = temp;
}

/* Deleting the node with min value */

if (p1 == front) /* deleting first node.*/
if (target_node == front) /* deleting first node.*/
{
front = front->next;
if (front == NULL) /* Deleting the only one node */
rear = NULL;
}
else if (p1 == rear) /* Deleting last node */
else if (target_node == rear) /* Deleting last node */
{
rear = follow1;
rear->next = NULL;
}
else /* deleting any other node.*/
follow1->next = p1->next;
follow1->next = target_node->next;

free(p1);
free(target_node);
return min; /* DONT FORGET LAST 2 STATEMENTS.*/
}

void show()
{
struct node *p;

if (empty())
if (isempty())
printf("Queue empty. No data to display \n");
else
{
printf("Queue from front to rear is as shown: \n");

p = front;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
for(node *temp = front; temp!=NULL; temp=temp->next)
printf("%d ", temp->data);

printf("\n");
}
}

void menu()
{
printf("\n\n Menu: \n");
printf("1:Insert \n");
printf("2:Remove \n");
printf("3:exit \n");
}
void destroyqueue() { front = rear = NULL; }

int main()
Expand All @@ -147,12 +141,10 @@ int main()

do
{
printf("\n\n Menu: \n");
printf("1:Insert \n");
printf("2:Remove \n");
printf("3:exit \n");
menu();
printf("Enter your choice: ");
scanf("%d", &ch);


switch (ch)
{
Expand All @@ -171,6 +163,9 @@ int main()

case 3:
break;
default:
printf("Enter valid case\n");
break;
}
} while (ch != 3);

Expand Down
Loading