Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 언어에서 연결 리스트를 사용하여 스택 설명

<시간/>

메모리를 동적으로 할당하여 스택 오버 플로우 및 스택 언더 플로우를 방지할 수 있습니다.

C 프로그래밍 언어에서 스택에서 수행되는 작업은 다음과 같습니다. -

  • 푸시

푸시

다음은 연결 목록의 기본 구현입니다 -

&item = 10
newnode = (node*) malloc (sizeof (node));
newnode ->data = item;
newnode ->link = NULL;
newnode ->link = start;
start = newnode;

C 언어에서 연결 리스트를 사용하여 스택 설명

구문은 다음과 같습니다 -

구문

if (start = = NULL)
printf("Deletion is not possible.List is empty")
else{
   temp = start;
   start = start link;
   free (temp);
}

프로그램

다음은 연결 목록을 사용하여 스택을 위한 C 프로그램입니다 -

#include <stdio.h>
#include <stdlib.h>
struct node{
   int info;
   struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main(){
   int no, ch, e;
   printf("\n 1 - Push");
   printf("\n 2 - Pop");
   printf("\n 3 - Top");
   printf("\n 4 - Empty");
   printf("\n 5 - Exit");
   printf("\n 6 - Display");
   printf("\n 7 - Stack Count");
   printf("\n 8 - Destroy stack");
   create();
   while (1){
      printf("\n Enter choice : ");
      scanf("%d", &ch);
      switch (ch){
         case 1:
            printf("Enter element : ");
         scanf("%d", &no);
         push(no);
         break;
         case 2:
            pop();
         break;
         case 3:
            if (top == NULL)
               printf("stack is empty");
         else{
            e = topelement();
            printf("\n Top element : %d", e);
         }
         break;
         case 4:
            empty();
         break;
         case 5:
            exit(0);
         case 6:
            display();
         break;
         case 7:
            stack_count();
         break;
         case 8:
            destroy();
         break;
            default :
         printf(" wrong choice:Try again ");
         break;
      }
   }
}
//empty stack
void create(){
   top = NULL;
}
void stack_count(){
   printf("\n no: of elements in stack : %d", count);
}
//push data
void push(int data){
   if (top == NULL){
      top =(struct node *)malloc(1*sizeof(struct node));
      top->ptr = NULL;
      top->info = data;
   }
   else{
      temp =(struct node *)malloc(1*sizeof(struct node));
      temp->ptr = top;
      temp->info = data;
      top = temp;
   }
   count++;
}
void display(){
   top1 = top;
   if (top1 == NULL){
      printf("empty stack");
      return;
   }
   while (top1 != NULL){
      printf("%d ", top1->info);
      top1 = top1->ptr;
   }
}
void pop(){
   top1 = top;
   if (top1 == NULL){
      printf("\n error");
      return;
   }
   else
      top1 = top1->ptr;
      printf("\n Popped value : %d", top->info);
      free(top);
      top = top1;
      count--;
   }
   int topelement(){
      return(top->info);
   }
   //check stack empty or not
   void empty(){
      if (top == NULL)
         printf("\n empty stack");
      else
         printf("\n stack not empty with %d values", count);
      }
      void destroy(){
         top1 = top;
      while (top1 != NULL){
         top1 = top->ptr;
         free(top);
      top = top1;
      top1 = top1->ptr;
   }
   free(top1);
   top = NULL;
   printf("\n all are destroyed");
   count = 0;
}

출력

위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -

1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Display
7 - Stack Count
8 - Destroy stack
Enter choice: 1
Enter element: 23
Enter choice: 1
Enter element: 45
Enter choice: 1
Enter element: 56
Enter choice: 2
Popped value: 56
Enter choice: 6
45 23
Enter choice: 8
all are destroyed
Enter choice: 6
empty stack
Enter choice: 5