Stack Overflow Program in C++ With Source Code
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*head;
void main()
{
int n;
void push();
void pop();
void peep();
void update();
void display();
head=NULL;
int flag=0;
do
{
if(flag==1)
getch();
clrscr();
flag=1;
printf("\nGive 1 for Push");
printf("\nGive 2 for Pop");
printf("\nGive 3 for Peep");
printf("\nGive 4 for Update");
printf("\nGive 5 for Display");
printf("\nGive 6 for Exit:");
scanf("%d",&n);
switch(n)
{
case 1:push();
flag=0;
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:update();
break;
case 5:display();
break;
default: printf("Invalid Input");
break;
}
}while(n!=6);
}
void push()
{
int item;
struct node *q,*temp;
printf("Insert value:");
scanf("%d",&item);
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
head->data=item;
head->next=NULL;
}
else
{
q=head;
while(q->next!=NULL)
{
q=q->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->next=NULL;
q->next=temp;
}
}
void pop()
{
struct node *q,*temp;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
temp=(struct node*)malloc(sizeof(struct node));
if(q->next==NULL)
{
temp=q;
head=NULL;
}
else
{
while(q->next->next!=NULL)
{
q=q->next;
}
temp=q;
temp=temp->next;
q->next=NULL;
}
printf("Deleted Element:%d",temp->data);
free(temp);
}
void peep()
{
int s;
int count=1,flag=0;
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
printf("Enter element for search:");
scanf("%d",&s);
while(q!=NULL)
{
if(s==q->data)
{
printf("\nElement is at position:%d",count);
flag=1;
break;
}
count++;
q=q->next;
}
if(flag==0)
{
printf("\nElement Not Found...");
}
}
void update()
{
int item1,item2,flag=0;
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
printf("Which element you want to update?");
scanf("%d",&item1);
printf("Enter Element:");
scanf("%d",&item2);
while(q!=NULL)
{
if(q->data==item1)
{
q->data=item2;
printf("Element Updated successfully...");
flag=1;
break;
}
q=q->next;
}
if(flag==0)
{
printf("Element Not Found...");
}
}
void display()
{
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
while(q!=NULL)
{
printf("%d,",q->data);
q=q->next;
}
}
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*head;
void main()
{
int n;
void push();
void pop();
void peep();
void update();
void display();
head=NULL;
int flag=0;
do
{
if(flag==1)
getch();
clrscr();
flag=1;
printf("\nGive 1 for Push");
printf("\nGive 2 for Pop");
printf("\nGive 3 for Peep");
printf("\nGive 4 for Update");
printf("\nGive 5 for Display");
printf("\nGive 6 for Exit:");
scanf("%d",&n);
switch(n)
{
case 1:push();
flag=0;
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:update();
break;
case 5:display();
break;
default: printf("Invalid Input");
break;
}
}while(n!=6);
}
void push()
{
int item;
struct node *q,*temp;
printf("Insert value:");
scanf("%d",&item);
if(head==NULL)
{
head=(struct node*)malloc(sizeof(struct node));
head->data=item;
head->next=NULL;
}
else
{
q=head;
while(q->next!=NULL)
{
q=q->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=item;
temp->next=NULL;
q->next=temp;
}
}
void pop()
{
struct node *q,*temp;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
temp=(struct node*)malloc(sizeof(struct node));
if(q->next==NULL)
{
temp=q;
head=NULL;
}
else
{
while(q->next->next!=NULL)
{
q=q->next;
}
temp=q;
temp=temp->next;
q->next=NULL;
}
printf("Deleted Element:%d",temp->data);
free(temp);
}
void peep()
{
int s;
int count=1,flag=0;
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
printf("Enter element for search:");
scanf("%d",&s);
while(q!=NULL)
{
if(s==q->data)
{
printf("\nElement is at position:%d",count);
flag=1;
break;
}
count++;
q=q->next;
}
if(flag==0)
{
printf("\nElement Not Found...");
}
}
void update()
{
int item1,item2,flag=0;
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
printf("Which element you want to update?");
scanf("%d",&item1);
printf("Enter Element:");
scanf("%d",&item2);
while(q!=NULL)
{
if(q->data==item1)
{
q->data=item2;
printf("Element Updated successfully...");
flag=1;
break;
}
q=q->next;
}
if(flag==0)
{
printf("Element Not Found...");
}
}
void display()
{
struct node *q;
q=head;
if(q==NULL)
{
printf("There is no Element...");
return;
}
while(q!=NULL)
{
printf("%d,",q->data);
q=q->next;
}
}
Stack Overflow Program in C++ With Source Code
Reviewed by Unknown
on
5:50:00 PM
Rating:
No comments: