C语言数据结构与算法 | 第二集2025年3月17日 | 阅读11分钟 栈和队列本教程将介绍C语言中另外两种线性数据结构——栈和队列。在本教程的第一部分,我们回顾了数组、动态数组和链表的概念。栈和队列都是**用户定义**的,这意味着当我们说int arr[]时,编译器能识别它是一个数组,但当我们说Stack时,编译器在程序中没有定义它就无法识别。 栈栈是一种线性数据结构。它基于**“后进先出”(LIFO)**的原理实现。这意味着最后插入栈的元素将是第一个被删除的元素。栈**只有一个开口**,这意味着要插入或删除元素,我们需要使用同一个末端。当我们向栈中插入元素时,**我们将元素堆叠起来**——新元素放在现有元素之上。插入所有元素后,如果我们想从栈中删除元素,最后插入的元素将是第一个被取出的。 术语
栈的函数
Push和PopPush和pop是栈的两个主要操作。因此,让我们详细了解它们 假设我们有一个名为“Hundreds”的栈。 它的容量是 10*sizeof(integer) = 40 字节。 Top索引 = 3, 栈顶元素 = 400 ![]() 它有四个元素。现在让我们对其进行push和pop操作 push(Hundreds, 550)
![]() pop() 550不是100的倍数。我们错误地将其推入了栈。它在栈顶。现在,我们需要将其弹出。 1. 检查栈是否为空。如果为空,则无法执行pop操作。 如果 top_index = -1 栈为空 但这里,top_index = 4。因此栈不为空。 2. top = top - 1 Hundreds[top] ![]() 现实生活中的栈的例子
栈的实现在C语言中,我们可以用两种方式实现栈
1. 使用数组 输出 100 pushed into the Stack Elements in the Stack: 100 200 pushed into the Stack Elements in the Stack: 200 100 300 pushed into the Stack Elements in the Stack: 300 200 100 After the pop operation, elements in the Stack: Elements in the Stack: 200 100
2. 使用链表 输出 Push operation: Enter a value to push:100 100 pushed Resultant Stack: The Stack: 100 Enter a value to push:200 200 pushed Resultant Stack: The Stack: 200 100 Enter a value to push:300 300 pushed Resultant Stack: The Stack: 300 200 100 Pop operation: Resultant Stack: The Stack: 200 100 -------------------------------- Process exited after 5.783 seconds with return value 0 Press any key to continue . . . 我们编写了两个方法**push和pop**来实现栈。我们需要确保两点:
队列队列是一种线性数据结构,与栈类似,但队列的实现原理是**FIFO(先进先出)**。这意味着插入队列的元素将是第一个从队列中出来的元素,而在栈中,最后压入的元素将是第一个被弹出的。 关于队列的重要注意事项
![]() 队列在现实生活中的例子
术语
我们可以在C语言中实现队列
1. 使用数组 输出 Enqueue operation: Enter an element to insert into the queue: 100 100 inserted The resultant queue:100 Enter an element to insert into the queue: 200 200 inserted The resultant queue:100 200 Enter an element to insert into the queue: 300 300 inserted The resultant queue:100 200 300 Dequeue operation: The resultant queue: 200 300 -------------------------------- Process exited after 5.774 seconds with return value 2 Press any key to continue . . .
**rear:(插入顺序):** H - E - L - L - O(0 -> 1 -> 2 -> 3 -> 4) **front:(删除顺序):** O - L - L - E - H(4 -> 3 -> 2 -> 1 -> 0) ![]() 2. 使用链表 输出 Enqueue operation: Enter the value to insert: 100 Resultant queue: 100 Enter the value to insert: 200 Resultant queue: 100 200 Enter the value to insert: 300 Resultant queue: 100 200 300 Dequeue operation: Resultant queue: 200 300 -------------------------------- Process exited after 4.771 seconds with return value 0 Press any key to continue . . .
3. 使用栈
让我们举个例子 有一个栈,我们将元素压入其中 ![]() 现在,如果我们执行pop操作,将弹出300。但是,根据队列的原则,应该删除100。 我们需要再创建一个栈,比如stack2,然后将我们的栈中的元素弹出到stack2中。 ![]() 现在,我们可以从stack2中弹出元素。这将等同于队列中的enqueue操作。 我们可以用两种方式使用栈来实现队列 1. 使dequeue操作变得昂贵 在上面的例子中,当我们向第一个栈插入元素时,它不会改变——O(1)。但是,当我们执行dequeue时,我们首先需要将元素弹出到另一个栈,然后从那个栈中——dequeue操作变得昂贵——O(n)。 2. 使enqueue操作变得昂贵 我们来看一个例子 我们有一个栈“stack1”,其中有一个元素——100
![]() 这是一个使用第一种方式实现的程序——**使dequeue操作变得昂贵:** 输出 Enqueue operation-after inserting 10, 20 and 30:10 20 30 Dequeue operation: The deleted element is 10 Resultant queue:20 30 -------------------------------- Process exited after 0.8232 seconds with return value 1 Press any key to continue . . . 程序中的函数
下一主题C语言中的员工记录系统 |
我们请求您订阅我们的新闻通讯以获取最新更新。