首頁  >  文章  >  後端開發  >  解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

王林
王林轉載
2023-09-15 16:01:01637瀏覽

資料結構是以結構化​​方式組織的資料集合。它分為兩種類型,即線性資料結構和非線性資料結構。

解釋C語言中的堆疊概念

線性資料結構 - 在這裡,資料以線性方式組織。

例如 - 陣列、結構體、堆疊、佇列、鍊錶。

非線性資料結構 - 在這裡,資料以層次結構方式組織。

例如 - 樹、圖、集合、表格。

C語言中的堆疊

它是一種線性資料結構,資料只能在一端插入和刪除。

操作

  • Push - 將元素插入堆疊中。
  • Pop - 從堆疊中刪除元素。

解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

解釋C語言中的堆疊概念

  • Deleted element = 50
    Item = a [top]
    top --
pop() ,pop(),pop(), pop()
  • ##
    Deleted element = 40
    Deleted element=30
    Deleted element=20
    Deleted element =10
  • #Pop ( )

堆疊溢位

    條件
  • 堆疊溢位- 嘗試向滿棧插入元素。
  • 堆疊下溢 - 嘗試從空堆疊中刪除元素。

Push ( ),Pop ( ),Display ( )的演算法

對應的演算法如下:

    Push ( )
#檢查堆疊是否溢位。
  • if (top = = n-1)
    printf("stack over flow”);
否則,將一個元素插入堆疊中。

top ++
a[top] = item
    Pop ( )
檢查堆疊下溢。
  • if ( top = = -1)
    printf( "stack under flow”);
否則,從堆疊中刪除該元素。

item = a[top]
top --
    Display ( )
檢查堆疊流程。
  • if (top == -1)
    printf ("stack is empty”);
否則,按照下面提到的演算法進行操作−

for (i=0; i<top; i++)
printf ("%d&rdquo;, a[i]);

範例

以下是使用陣列實作堆疊的C程序:

#include<stdio.h>
#include <conio.h>
int top = -1, n,a[100];
main ( ){
   int ch;
   void pop ( );
   void display ( );
   clrscr ( );
   printf ("enter the size of the stack&rdquo;);
   scanf ("%d&rdquo;, &n);
   printf("stack implementation</p><p>&rdquo;);
   printf ("1. push </p><p>&rdquo;);
   printf ("2. Pop </p><p>&rdquo;);
   printf ("3. exit </p><p>&rdquo;);
   do{
      printf ( "enter ur choice&rdquo;);
      scanf ("%d&rdquo;, &ch);
      switch (ch){
         case 1 : push ( );
         display ( );
         break;
      case 2 : push ( );
         display ( );
         break;
      case 3 : exit
   }
   }while (ch>=1 | | ch<= 3);
   getch ( );
}
void push ( ){
   int item;
   if (top = = n-1)
      printf ( "stack over flow&rdquo;)
   else{
      printf("enter an element for insertion&rdquo;)
      scanf ("%d&rdquo;, &item);
      top ++;
      a[top] = item;
   }
}
void pop ( ){
   int item;
   if (top = = -1);
      printf ( "stack under flow&rdquo;);
   else{
      item = a[top];
      top --;
      printf("deleted element = %d&rdquo;, item);
   }
}
void display ( ){
   int i;
   if (top = = -1)
      printf ( "stack is empty&rdquo;);
   else{
      printf("contents of the stack are&rdquo;);
      for (i=0; i<top; i++)
         printf ("%d \t&rdquo;, a[i]);
   }
}

輸出

當執行上述程式時,它會產生以下結果−###
enter the size of the stack = 5 [given by user]
Stack implementation
1. Push 2. Pop 3. exit
Enter ur choice : 1 [given by user]
Enter an element for insertion : 10
Contents of the stack : 10
Enter ur choice : 1
Enter an element for insertion : 2
Contents of the stack : 10 20
Enter ur choice : 2
Deleted element = 20
Contents of the stack are : 10
Enter ur choice : 2
Deleted element : 10
Contents of the stack are : stack is empty
Enter ur choice : 2
Stack underflow.
Enter ur choice : 1
Enter an element for insertion : 30
Contents of the stack are : 30
###

以上是解釋C語言中的堆疊概念的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除