Slip1_1 :Write a ‘C’ program to reverse a string using Static implementation of Stack.
Program Code
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define SIZE 40
char stk[SIZE];
int top;
void init()
{ top=-1; }
int isfull()
{ if(top==SIZE-1)
return 1;
else
return 0;
}
int isEmpty()
{ if(top==-1)
return 1;
else
return 0;
}
void push(char c)
{ if(isfull())
{ printf("Stack is overflow"); }
else
{ top++;
stk[top]=c;
}
}
char pop()
{ if(isEmpty())
{ printf("Stack is empty"); }
else
{ char c;
c=stk[top];
top--;
return c;
}
}
void main() { char s[30];
int i;
printf("\nEnter String : ");
gets(s);
init();
for(i=0;s[i]!='\0';i++)
{ push(s[i]);
}
printf("\n Reverse String is : ");
while(!isEmpty()) { printf("%c",pop()); }
getch();
}
Output:
Slip1_2 : carete singly link list i) create ii) display iii)insert first position iv) Delete node at any position
Program Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);
}
t->next=NULL;
}
void display()
{
struct node *t;
for(t=head;t!=NULL;t=t
->next
)
{
printf("\t %d->",t->data);
}
}
void insert_last() {
struct node *nw,*t;
nw=(NODE*)malloc(sizeof(NODE));
printf("\n Enter node for inserting: ");
scanf("%d",&nw
->data);
for(t=head;t
->next!=NULL;t=t
->next);
t
->next=nw;
nw
->next=NULL;
}
void delete() {
struct node *t,*temp;
int cnt=0,i,p;
printf("\n Enter position for delete Data : ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t
->next)
cnt++;
if(p>cnt) {
printf("\n Invalid position ");
}
else {
if(p==1) {
temp=head;
head=head
->next;
free(temp);
}
else if(cnt==p)
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next=NULL;
free(temp);
}
else
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next = temp->next;
free(temp);
}
}
}
main()
{
int ch;
do
{
printf("\n MENU \n 1.Create \n 2.Display \n 3.Add at last position \n4.Delete node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_last();
break;
case 4: delete();
break;
case 5:break;
}
}
while(ch!=5);
getch();
}
Output:
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 1
How Many Nodes you Want to Create : 2
Enter node 1: 12
Enter node 1: 11
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 2
12-> 11->
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 3
Enter node for inserting: 4
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 3
Enter node for inserting: 2
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 2
12-> 11-> 4-> 2->
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 4
Enter position for delete Data : 1
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice : 2
11-> 4-> 2->
MENU
1.Create
2.Display
3.Add at last position
4.Delete node
5.Exit
enter your choice :
DS slip 1
Reviewed by Dinesh Varal
on
August 01, 2018
Rating:
No comments: