You are on page 1of 3

CITY UNIVERSITY

Faculty of Science and Engineering


Department of Computer Science and Engineering
Fall 2021

CSE 214 Data Structure Laboratory

Assignment

Submitted To
Md. Al-Mamun Riyadh
Lecturer, Dept. of CSE
City University

Submitted By
Rabbany Rahman
2015302501

Date of Submission: 08/03/2022


Question: Design a stack data structure with following functions
Empty, Size, Top, Push, Pop, Display.

Program:
// Design a stack data structure with following functions Empty, Size, Top,
Push, Pop, Display.

#include <stdio.h>
#include<stdlib.h>
typedef struct Node node;

struct Node{
int value;
node *next;
};

int empty(node* head);


int size(node* head);
int top(node* head);
node* push(node* head, int value);
node* pop(node* head);
void display(node* head);

int main(){
node* head=NULL;
head=push(head,1);
head=push(head,2);
head=push(head,3);
head=push(head,4);
display(head);
head=pop(head);
display(head);
printf("empty: %d\n",empty(head));
printf("Size: %d\n",size(head));
printf("Top: %d\n",top(head));

return 0;
}

int empty(node* head){


if(!head){
return 1;
}
return 0;
}

int size(node* head){


int i=0;
while(head){
head=head->next;
i++;
}
return i;
}

int top(node* head){


return head->value;
}

node* push(node* head, int value){


node* temp=(node*)malloc(sizeof(node));
temp->value=value;
temp->next=head;
return temp;
}

node* pop(node* head){


node *temp=head->next;
free(head);
return temp;
}

void display(node* head){


while(head){
printf("%d ",head->value);
head=head->next;
}
printf("\n");
}

Output:

You might also like