# 佇列的連結串列實現

## 連結佇列上的操作

#### 1. 插入操作

``````ptr = (struct node *) malloc (sizeof(struct node));
``````

``````ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
``````

``````rear -> next = ptr;
rear = ptr;
rear->next = NULL;
``````

``````第2步：為新節點PTR分配空間

SET FRONT = REAR = PTR
SET FRONT - > NEXT = REAR - > NEXT = NULL
其他
SET REAR - > NEXT = PTR
SET REAR = PTR
SET REAR - > NEXT = NULL
[結束]

``````

C語言的實現

``````void insert(struct node *ptr, int item; )
{
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("OVERFLOW\n");
return;
}else
{
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
``````

#### 2. 刪除

``````ptr = front;
front = front -> next;
free(ptr);
``````

``````第1步：IF FRONT = NULL
提示「下溢」
轉到第5步
[結束]

``````

C語言實現程式碼 -

``````void delete (struct node *ptr)
{
if(front == NULL)
{
printf("UNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
``````

``````#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while (choice != 4)
{
printf("=================================================================\n");
printf("1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Enter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("OVERFLOW\n");
return;
}
else
{
printf("Enter value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{
front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if (front == NULL)
{
printf("UNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if (front == NULL)
{
printf("Empty queue\n");
}
else
{
printf("printing values .....\n");
while (ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}
``````

``````***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter value?
123

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter value?
90

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

printing values .....

123

90

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

printing values .....

90