├── README.md └── Array Implementation of Stack ADT /README.md: -------------------------------------------------------------------------------- 1 | # DSA 2 | .. 3 | -------------------------------------------------------------------------------- /Array Implementation of Stack ADT: -------------------------------------------------------------------------------- 1 | #include #include #include struct node 2 | { 3 | int element; struct node *next; 4 | }*list=NULL,*p; struct node *find(int); 5 | struct node *findprevious(int); void insert(int X); 6 | void deletion(int X); void display(); 7 | void main() 8 | { 9 | int data,ch; clrscr(); 10 | cout<<"1.Insert\n2.Deletion\n3.Display\n4.Exit"; do 11 | { 12 | cout<<"\nenter your choice"; cin>>ch; 13 | switch(ch) 14 | { 15 | case 1: 16 | cout<<"enter the element to insert"; cin>>data; 17 | insert(data); 18 | 19 | 20 | break; case 2: 21 | cout<<"enter the element to delete"; cin>>data; 22 | deletion(data); break; 23 | case 3: 24 | display(); break; case 4: 25 | exit(0); 26 | } 27 | }while(ch<4); 28 | getch(); 29 | } 30 | void insert(int X) 31 | { 32 | struct node *newnode; int pos; 33 | newnode=(struct node*)malloc(sizeof(struct node)); newnode->element=X; 34 | if(list->next==NULL) 35 | { 36 | 37 | list->next=newnode; newnode->next=NULL; 38 | } 39 | else 40 | { 41 | cout<<"\n enter the value after which the element to be inserted"; cin>>pos; 42 | p=find(pos); 43 | newnode->next=p->next; p->next=newnode; 44 | } 45 | } 46 | struct node *find(int s) 47 | { 48 | p=list->next; 49 | while(p!=NULL&&p->element!=s) p=p->next; 50 | return p; 51 | } 52 | 53 | 54 | void deletion(int X) 55 | { 56 | struct node *temp; 57 | temp=(struct node*)malloc(sizeof(struct node)); p=findprevious(X); 58 | if(p->next!=NULL) 59 | { 60 | temp=p->next; 61 | p->next=temp->next; 62 | cout<<"\n the deleted element is"<element; free(temp); 63 | } 64 | else 65 | cout<<"\n element not found in the list"; 66 | } 67 | struct node *findprevious(int s) 68 | { 69 | p=list; 70 | while(p->next!=NULL&&p->next->element!=s) p=p->next; 71 | return p; 72 | } 73 | void display() 74 | { 75 | if(list->next==NULL) cout<<"list is empty"; else 76 | { 77 | p=list->next; 78 | cout<<"\n the contents of the list are:\n"; while(p!=NULL) 79 | { 80 | cout<element; p=p->next; 81 | } 82 | cout<<"null"; 83 | } 84 | } 85 | --------------------------------------------------------------------------------