ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

数据结构教程9:循环国彩网数据结构介绍和使用C语言中的数组的实现。

前开发者教程 2019年5月19日

在上一章中,我们已经看到了使用数组和链接列表[LL]的简单国彩网的实现。但是我们也发现使用简单国彩网时的一个缺点是我们将无法充分利用空白空间。因此,为了有效利用空间,我们应该使用循环国彩网。

定义:

循环链表,可以将国彩网中的元素插入国彩网前面的空白区域,从而有效地使用空白区域。

在循环国彩网中,前端将指向0,后端指针将指向国彩网的最大索引大小。我们将使用模数“%”运算符来知道剩余的空白空间。

当元素插入到后端时,我们使用以下公式进行计算’s index.

rear_end = (rear_end + 1) % MAX_QUEUE_SIZE;

同样,当从front_ end删除元素时,我们使用以下公式计算其索引:

front_ end = (front_ end + 1) % MAX_QUEUE_SIZE;

例如:

假设MAX_QUEUE_SIZE = 4;

Initially:
	front_ end will be at index 0
	rear_ end will be at index 3

Enter 10:
	front_ end will be at index 0
	rear_ end = [ 3 + 1] % 4 = 0

	Hence element will be inserted at index 0

 [10, , , ]
 
Enter 20:
	front_ end will be at index 0
	rear_ end = [ 0 + 1] % 4 = 1

	Hence element will be inserted at index 1

 [10, 20, , ]
 
Enter 30:
	front_ end will be at index 0
	rear_ end = [ 1+ 1] % 4 = 2

	Hence element will be inserted at index 2

 [10, 20, 30, ]
 
Enter 40:
	front_ end will be at index 0
	rear_ end = [ 2 + 1] % 4 = 3

	Hence element will be inserted at index 3

 [10, 20, 30, 40]

Delete 10

front_ end will be at index 0 = [0 + 1] % 4 = 0
Element at index 0 will de deleted.

	rear_ end will be at index 3

	Hence element will be inserted at index 3

 [ , 20, 30, 40]

Enter 50:
	front_ end will be at index 1
	rear_ end = [ 3 + 1] % 4 = 0
	Check if there is any element at index '0'. If no element enters 50.
	Hence element will be inserted at index 0
 
 [50, 20, 30, 40]

depending upon the result, the rear pointer will update accordingly.

以下是我们将要使用的操作

1.插入():

此函数用于将元素插入国彩网。

 数据结构教程9:循环国彩网数据结构介绍和使用C语言中的数组的实现。

2.删除():

此功能用于从国彩网中删除元素。

3.显示():

在此功能中,我们将显示国彩网元素。

因此,通过使用循环国彩网,我们​​将有效利用空间。

使用数组实现循环链​​接列表:

#include<stdio.h>
#include<stdlib.h>

#define QUEUE_SIZE	5

int rear_end; /* a variable to insert element at rear end */
int front_end; /* a variable to delete element at front end */
int 国彩网_elements [10]; /* An array to hold 国彩网 elements */
int 国彩网_item; /* a variable to hold the item to be inserted */
int element_count; /* it holds the number of elements presently in the 国彩网*/

//function to insert into the 国彩网
void InsertQueue()
{
	if(element_count == QUEUE_SIZE)
	{
		printf("Queue is full\n");
		return;
	}

	// we use this formula to get the exact location to insert element
	rear_end = (rear_end + 1) % QUEUE_SIZE;
	queue_elements[rear_end] = 国彩网_item;
	element_count ++;
}

//this function is used to delete element from the 国彩网.
int DeleteQueue()
{
	if (element_count == 0)
		return -1;

	queue_item = 国彩网_elements[front_end];

	//we use this formula to get element from front end 
	front_end = (front_end + 1) % QUEUE_SIZE;
	element_count -= 1;

	return 国彩网_item;
}


//this function is used to display the content of the 国彩网
void DisplayQueue()
{
	int front = front_end;
	if (element_count == 0)
	{
		printf("Queue is empty\n");
		return;
	}

	printf("The Queue Elements\n");


	for (int i = 1; i <= element_count; i++)
	{
		printf("%d\n",queue_elements[front] );
		front = (front + 1) % QUEUE_SIZE;
	}
}

int main()
{
	int choice = 0;

	front_end = 0;
	rear_end = -1;
	element_count = 0;


	for( ; ; )
	{
		printf("1. Insert \n2. Delete \n3. Display \n4. Exit\n");

		scanf("%d", &choice);

		switch(choice)
		{
			case 1:
				printf("\nEnter the item to be inserted\n");
				scanf("%d", &queue_item);

				InsertQueue();
			break;

			case 2:
				queue_item = DeleteQueue();

				if(queue_item == -1)
					{
						printf("Queue is empty\n");
					}
					else
					{
						printf("Deleted Item = %d\n",queue_item );
					}
			break;

			case 3:
				DisplayQueue();
				break;

			default:
				exit(0);
		}
	}

	return 0;
}

进一步阅读:

AJ关于DS和算法的权威指南。单击此处以学习算法和数据结构教程的完整列表。 85多个章节可供学习。

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

    1. <ins id="hoUKv9Y" class="hYMhbrs"><b id="wSWYCzv"><label class="GyDiPHG"><strike id="vE82sMn"></strike></label></b></ins>

    2. <tfoot class="EcBAo95"></tfoot>

      <var id="KYFU9DA" class="KA1G8fl"></var>