Untitled
unknown
plain_text
4 years ago
4.0 kB
4
Indexable
stack #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *push(struct node *head) { printf("Enter the data\n"); int n; scanf("%d",&n); if(head==NULL) { head=(struct node *)malloc(sizeof(struct node)); head->data=n; head->next=NULL; return head; } struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=n; temp->next=head; head=temp; return head; } struct node *pop(struct node *head) { struct node *temp=head; if(head==NULL) { printf("Stack is empty\n"); return NULL; } head=head->next; temp->next=NULL; printf("Deleted element=%d\n",temp->data); free(temp); return head; } void top(struct node *head) { if(head==NULL) { printf("Stack is empty\n"); return; } printf("Top element = %d\n",head->data); } void display(struct node *head) { struct node *temp=head; while(temp !=NULL) { printf("%d ",temp->data); temp=temp->next; } } int main() { struct node *head=NULL; while(1) { printf(" 1) TO PUSH \n 2) TO POP \n 3) TO GET THE TOP ELEMENT \n 4) TO DISPLAY\n 5) TO EXIT\n"); int n; scanf("%d",&n); switch(n) { case 1: { struct node *t; t=push(head); head=t; break; } case 2: { struct node *t; t=pop(head); head=t; break; } case 3: { top(head); break; } case 4: { display(head); printf("\n"); break; } case 5: { return 0; } } } } queue #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *push(struct node *head) { printf("Enter the data\n"); int n; scanf("%d",&n); if(head==NULL) { head=(struct node *)malloc(sizeof(struct node)); head->data=n; head->next=NULL; return head; } struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=n; temp->next=NULL; struct node *tail=head; while(tail->next != NULL) { tail=tail->next; } tail->next=temp; return head; } struct node *pop(struct node *head) { struct node *temp=head; if(head==NULL) { printf("Queue is empty\n"); return NULL; } head=head->next; temp->next=NULL; printf("Deleted element=%d\n",temp->data); free(temp); return head; } void top(struct node *head) { if(head==NULL) { printf("Queue is empty\n"); return; } printf("Top element = %d\n",head->data); } void display(struct node *head) { struct node *temp=head; while(temp !=NULL) { printf("%d ",temp->data); temp=temp->next; } } int main() { struct node *head=NULL; while(1) { printf(" 1) TO PUSH \n 2) TO POP \n 3) TO GET THE TOP ELEMENT \n 4) TO DISPLAY\n 5) TO EXIT\n"); int n; scanf("%d",&n); switch(n) { case 1: { struct node *t; t=push(head); head=t; break; } case 2: { struct node *t; t=pop(head); head=t; break; } case 3: { top(head); break; } case 4: { display(head); printf("\n"); break; } case 5: { return 0; } } } }
Editor is loading...