Assignment
University
California State UniversityCourse
COMP 182 | Data Structures and Program Design and LabPages
6
Academic year
2023
127_Dhruvil Lathiya
Views
0
Subject Code: 3130702 Subject Name: Data Structures Date: Enrollment No: 200420107127 Name: Lathiya Dhruvil M. 1. Problem Statement- Write a program to implement stack using linked list. Program- #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *next; }; typedef struct node node; node * insert_first(node* R,int n) { node *nnode,*temp; temp=R; nnode=(node*)malloc(sizeof(node)); nnode->data=n; nnode->next=R; temp=nnode; return temp; } node * delete_first(node *R) { node *temp; temp=R; R=R->next; free(temp); printf("Deletion complete!!!"); return R; } void display(node *R) { while(R!=NULL) { printf("%d ",R->data); R=R->next; } } void main() { node * start=NULL; int ch,n; clrscr(); do{ printf("\n-------------------Stack using Linked List ---------------- "); SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 1
printf("\n1. Insertion"); printf("\n2. Deletion"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter element:"); scanf("%d",&n); start=insert_first(start,n); break; case 2: start=delete_first(start); break; case 3: display(start); break; case 4: exit(0); } } while(ch!=0); getch(); } Output- SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 2
2. Problem Statement- Write a program to implement queue using linked list. Program- #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *next; }; typedef struct node node; node * insert_end(node* R,int n) { node *nnode,*temp; SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 3
temp=R; nnode=(node*)malloc(sizeof(node)); if(temp==NULL) { nnode->data=n; nnode->next=R; temp=nnode; } else { while(R->next!=NULL) R=R->next; nnode=(node*)malloc(sizeof(node)); nnode->data=n; nnode->next=NULL; R->next=nnode; } return temp; } node * delete_first(node *R) { node *temp; temp=R; R=R->next; free(temp); printf("Deletion complete!!!"); return R; } void display(node *R) { while(R!=NULL) { printf("%d ",R->data); R=R->next; } } void main() { node * start=NULL; int ch,n; clrscr(); do{ printf("\n------------------- Queue using Linked List ----------------- "); printf("\n1. Insertion"); printf("\n2. Deletion"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 4
{ case 1: printf("Enter element:"); scanf("%d",&n); start=insert_end(start,n); break; case 2: start=delete_first(start); break; case 3: display(start); break; case 4: exit(0); } } while(ch!=0); getch(); } Output- SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 5
SCET/CO/2021-22/Odd/BE Div-I/Sem-III Page No- 6
Linked List-Based Stack and Queue Implementations in C
Please or to post comments