Wednesday, 11 January 2017

Write a c program to find pairs with given sum in doubly linked list.

Write a c program to find pairs with given sum in doubly linked list.
Program:
#include <stdio.h>
struct node
{
int data;
struct node *next,*back;
};

struct node *head,*tail,*c,*p;
create();
main()
{
int sum,i,value,flag=0;
create();
printf("enter the sum");
scanf("%d",&sum);
for(c=head;c->next!=NULL;c=c->next)
for(p=c->next;p!=NULL;p=p->next)
if(c->data+p->data == sum)
printf("\nsum found \t%d + %d = %d ",c->data,p->data,sum);

}
create()
{
int value,opt;
while(1)
{
printf("Press 1 to continue and Press 0 to stop");
scanf("%d",&opt);
if(opt == 1)
{
printf("\nenter a value");
scanf("%d",&value);
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
if(head == NULL)
{
newnode->next=NULL;
newnode->back=NULL;
head=tail=newnode;

}
else
{
tail->next=newnode;
newnode->back=tail;
newnode->next=NULL;
tail=newnode;
}
}
else
return;
}
}

Output:

String Weight C Program

String Weight
Prof. M went to unknown world where words are considered to be powerful according to their weights. But Prof. M doesn't know how to calculate weight of words. He will tell you a word and you have to tell its weight.
You know that weight of a word is equal to sum weight of individual characters. But as Prof. M is too old he can't pronounce the word properly.
He skips some of its letters. He wants your help to calculate weight of the Word.
Word is made up of characters from [a-z] only and every characters has specific weight.
Initially, weight of a is 1 , b is 2 ....... y is 25 , z is 26 and those letters which are skipped by Prof.M, take its weight as 0 (zero).
Input:
First Line will contain T number of test cases.
Every test case will contain first line as word W and in second line there will be a integer N denoting number on letters Prof. M skipped and finally third Line will contain N letters which Prof. M skips.
Output:
For each test case you have to print the resulting weight of that word on a separate line.
Constraints:
1 ≤ T ≤ 10000
1 ≤ |W| ≤ 10000 where |W| is length of word W
1 ≤ N ≤ 26
SAMPLE INPUT
3
abcd
2
a c
aaaa
1
a
ababab
2
a b
SAMPLE OUTPUT
6
0
0
Explanation

Actual weight =1+2+3+4=10 But since he skipped a,c therefore Loss=1+3=4 Resultant weight =10-4=6

Program:


#include <stdio.h>
main()
{
char word[20],skip[20];
int i,j,w1=0,flag,n;
printf("\n enter a word\n");
gets(word);
printf("enter the number of letters skipped\n");
scanf("%d",&n);
printf("enter the skipped letters\n");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&skip[i]);
}
skip[i]='\0';
for(i=0;word[i]!='\0';i++)
{
flag=0;
for(j=0;skip[j]!='\0';j++)
{
if(word[i]==skip[j])
{
flag=1; 
break;
}
}
if(flag==0)
w1=w1+(int)word[i]-96;
}
printf("final weight = %d",w1);
}

Output:


(OR)
#include <stdio.h>
#include <string.h>
main()
{
char word[50],skip[50];
int i,n,w1=0;
puts("enter first string");
gets(word);
printf("\nenter the number of skipped characters\n");
scanf("%d",&n);
printf("enter skipped characters\n");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&skip[i]);
}
       
        skip[i]='\0';
for(i=0;word[i] != '\0';i++)
{
if(strchr(skip,word[i]))
continue;
else
w1=w1+(word[i]-'a')+1;
}
printf("\ntotal weight = %d",w1);
}

Special array reversal,reverse the string without affecting the positions of the special characters: C program

Special array reversal
Given a string S, containing special characters and all the alphabets, reverse the string without

affecting the positions of the special characters. 

#include <stdio.h>
#include <string.h>
main()
{
char s[20],temp;
int i=0,j;
gets(s);
j=strlen(s)-1;
while(i<j)
{
if(isalpha(s[i]) && isalpha(s[j]))
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;j--;
}
else if(isalpha(s[i]))
j--;
else
i++;
}
puts(s);
}

Output:

Monday, 9 January 2017

Converting binary to decimal using linked lists: C program

#include <stdio.h>
#include <stdlib.h>
void create();
display();
struct node
{
int data;
struct node *next;
};
struct node *head=NULL,*c,*p,*r;
main()
{
int n,bit,i;
printf("enter the number of bits in the binary number");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter a bit\n");
scanf("%d",&bit);
create(bit);
}
display();
}
void create(int value)
{
struct node * new = (struct node *)malloc(sizeof(struct node));
new->data = value;
new->next=NULL;
if(head == NULL)
head = new;
else
{
c=head;
while(c->next != NULL)
{
c=c->next;
}
c->next=new;
}
}
display()
{
int dec=0;
if(head == NULL)
printf("list is empty");
else
{
for(c=head;c!=NULL;c=c->next)
{
printf("%d\t",c->data);
dec=dec*2+c->data;
}
printf("\ndecimal equivalent = %d",dec);
}
}

Output:

Write a c program to implement single linked list to print like 5 4 4 3 3 2 2 1 1 0 by taking input as 1 2 3 4 5

#include<stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node * create(struct node *,int);
display(struct node *);
reverse(struct node *);
main()
{
int n;
struct node *head=NULL;
printf("\nenter a number\n");
scanf("%d",&n);
head=create(head,n);
//display(head);
reverse(head);
}
struct node * create(struct node *q,int n)
{
int i;
struct node *newnode,*c;
for(i=1;i<=n;i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=i;
newnode->next=NULL;
if(q==NULL)
q = newnode;
else
{
c=q;
while(c->next != NULL)
c=c->next;
c->next=newnode;
}
}
return q;
}

display(struct node *p)
{
while(p!=NULL)
{
printf("\t%d\t%d",p->data,p->data-1);
p=p->next;
}
}
reverse(struct node *p)
{
struct node *q,*r;
q=NULL;
while(p != NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
p=q;
display(p);
}

Doubly circular linked list C program

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next,*back;
};

struct node *head,*c;
deleteend();
deletebeg();
deleteend();
insertmiddle(int);
insertend(int);
insertbeg(int);
display();
create();
main()
{
int n,i,value;
create();
display();
printf("\nenter a value to insert at beginning");
scanf("%d",&value);
insertbeg(value);
display();
printf("\nenter a value to insert");
scanf("%d",&value);
insertmiddle(value);
display();
deletebeg();
printf("\nafter deleting first node\n");
display();
deletemiddle();
printf("\nafter deleting\n");
display();
printf("\nafter deleting last node\n");
deleteend();
display();
}
create()
{
int value,opt;
while(1)
{
printf("Press 1 to continue and Press 0 to stop");
scanf("%d",&opt);
if(opt == 1)
{
printf("\nenter a value");
scanf("%d",&value);
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
if(head == NULL)
{
head=newnode;
newnode->next=head;
newnode->back=head;
}
else
{
newnode->next=head;
newnode->back=head->back;
newnode->back->next=newnode;
head->back=newnode;
}
}
else
return;
}
}
insertmiddle(int value)
{
int pos,i=1;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
printf("\nenter the position at which u want to insert\n");
scanf("%d",&pos);
c=head;
while(i<pos)
{

c=c->next;
i++;
}

newnode->next=c;
newnode->back=c->back;
c->back->next=newnode;
c->back=newnode;

}
insertbeg(int value)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
if(head == NULL)
{
head=newnode;
newnode->next=head;
newnode->back=head;

}
else
{
newnode->back=head->back;
head->back->next=newnode;
newnode->next=head;
head->back=newnode;
head=newnode;
}
}

deletebeg()
{
c=head;
head->next->back=head->back;
head->back->next=head->next;
head=head->next;
free(c);
}
deletemiddle()
{
int pos,i=1;
printf("\nenter the position at which u want to delete\n");
scanf("%d",&pos);
c=head;
while(i<pos)
{
c=c->next;
i++;
}
c->next->back=c->back;
c->back->next=c->next;
free(c);
}
deleteend()
{
c=head->back;
head->back=head->back->back;
head->back->next=head;
free(c);
}
display()
{
c=head;
if(head==NULL)
{
printf("list is empty");
}
else
{
do
{
printf("%d<->",c->data);
c=c->next;
}while(c!=head);
}
}

Output: