📜  数据结构-队列的链表实现

📅  最后修改于: 2020-10-15 00:57:08             🧑  作者: Mango

#### 插入操作

``````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;
``````

#### 算法

• 步骤1：为新节点PTR分配空间
• 步骤2： SET PTR-> DATA = VAL
• 第3步：如果FRONT = NULL SET FRONT = REAR = PTR SET FRONT-> NEXT = REAR-> NEXT = NULL ELSE SET REAR-> NEXT = PTR SET REAR = PTR SET REAR-> NEXT = NULL [IF结束]
• 步骤4：结束

#### C功能

``````void insert(struct node *ptr, int item; )
{

ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\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;
}
}
}
``````

#### 删除中

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

``````

#### 算法

• 步骤1：如果FRONT = NULL，则写“下溢”，转到步骤5 [IF结束]
• 步骤2： SET PTR = FRONT
• 步骤3： SET FRONT = FRONT-> NEXT
• 步骤4：免费PTR
• 步骤5：结束

#### C功能

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

#### 菜单驱动程序，在链接队列上执行所有操作

``````#include
#include
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=================================================================\n");
printf("\n1.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("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter 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("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{    printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%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