A) Write a ‘C’ program to accept a string from user and reverse it using Static
implementation of Stack.
Answer :
#include<stdio.h>
#include<conio.h>
#define MAX 50
char a[MAX];
int top=-1;
void push(char c){
top++;
a[top]=c;
}
char pop(){
char c;
c=a[top];
top--;
return c;
}
int main(){
int i;
printf("\n\nENTER THE STRING");
gets(a);
for(i=0;a[i]!=NULL; i++){
if(a[i]==' '){
while(top!=-1){
printf("%c",pop());
}
printf(" ");
}else{
if(top==MAX-1){
printf("\n\nSTACK IS FULL");
}else{
push(a[i]);
}
}
}while(top!=-1){
printf("%c",pop());
}
getch();
}
B) Write a ‘C’ program to create a singly linked list, reverse it and display both the list.
Answer :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list{
int data;
struct list *link;
}*start;
void createlist(int);
void disp();
int main(){
struct list *p1,*p2,*p3;
int i,n,m;
printf("\nEnter the total number of nodes");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("\n\n Enter %dst node",i);
scanf("%d",&m);
createlist(m);
}
printf("\n\n SINGLY LINKED LIST CREATED SUCCESSFULLY \n\n");
disp();
printf("\n\n SUCCESSFULLY REVERSED LIST\n\n");
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL){
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
disp();
getch();
}
void createlist(int m){
struct list *tmp,*q;
tmp=(struct list *) malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else{
q=start;
while(q->link!=NULL){
q=q->link;
}
q->link=tmp;
}
}
void disp(){
struct list *q;
q=start;
while(q!=NULL){
printf("%d->",q->data);
q=q->link;
}
printf("NULL");
}
C) Write a menu driven program using ‘C’ for singly linked list
a. To create linked list.
b. To display linked list
c. To search node in linked list.
d. Insert at last position.
Answer :
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create_list(struct node *start);
void display(struct node *start);
void search(struct node *start,int data);
struct node *addatbeg(struct node *start,int data);
struct node *addatend(struct node *start,int data);
int main()
{
struct node *start=NULL;
int choice,data,item,pos;
while(1)
{
printf("1.Create List\n");
printf("2.Display\n");
printf("3.Search\n");
printf("4.Add at end\n");
printf("5.Quit\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
start=create_list(start);
break;
case 2:
display(start);
break;
case 3:
printf("Enter the element to be searched : ");
scanf("%d",&data);
search(start,data);
break;
case 4:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
break;
case 5:
exit(1);
default:
printf("Wrong choice\n");
}
}
return 0;
}
struct node *create_list(struct node *start)
{
int i,n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
if(n==0)
return start;
printf("Enter the element to be inserted: ");
scanf("%d",&data);
start=addatbeg(start,data);
for(i=2;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
}
return start;
}
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
p=start;
printf("List is :\n");
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n\n");
}
void search(struct node *start,int item)
{
struct node *p=start;
int pos=1;
while(p!=NULL)
{
if(p->info==item)
{
printf("Item %d found at position %d\n",item,pos);
return;
}
p=p->link;
pos++;
}
printf("Item %d not found in list\n",item);
}
struct node *addatbeg(struct node *start,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}
struct node *addatend(struct node *start,int data)
{
struct node *p,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}
D) Write a ‘C’ program to accept and sort n elements in ascending order using Selection sort method.
Answer :
#include<stdio.h>
int main(){
int array[100],n,i,j,pos,t;
printf("Enter Number of Elements \n ");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<(n-1); i++){
pos=i;
for(j=i+1; j<n; j++){
if(array[pos]>array[j])
pos=j;
}if(pos!=i){
t=array[i];
array[i]=array[pos];
array[pos]=t;
}
}
printf("Sorted list in ascending order : \n");
for(i=0;i<n;i++)
printf("%d",array[i]);
return 0;
}
No comments:
Post a Comment