Вы находитесь на странице: 1из 6

1.

Given a singly link list and a number 'K', swap the Kth node from the start with the Kth node from the last. Check all the edge cases. Sample Input: 1->2->3->4->5->6->7->8 and K = 3 Sample Output : 1->2->6->4->5->3->7->8 Sample Input: 1->2->3->4->5->6->7->8 and K = 10 Sample Output: print error "LIST IS OF LESSER SIZE". I. Please come up with basic test cases II. Write a program. Solution:

We need to handle a few test cases. 1. Both nodes are end nodes. 2. Both nodes are adjacent. 3. Both nodes are somewhere else. 4. Both node are same. 5. kth node doesn't exist. I prefer swapping the nodes rather than values. It is always advisable to swap the nodes as in general, nodes may contain several data. So, it will be overhead to swap the values.

*/ struct node { int data; struct node *link; }; int main() { struct node *start; start=NULL; int n,i=0,item,k; printf("Enter the no of node\n"); scanf("%d",&n); while(i++<n) { printf("Enter node value\n"); scanf("%d",&item); append(&start,item); } display(start); printf("Enter the number k for swaping kth from the fast and kth from last\n"); scanf("%d",&k); swap(start,k,n); printf("\n"); display(start); }

append(struct node **t,int b) { struct node *r,*temp; r=*t; if(r==NULL) { r=(struct node*)malloc(sizeof(struct node)); *t=r; } else { while(r!=NULL) { temp=r; r=r->link; } temp->link=(struct node *)malloc(sizeof(struct node)); r=temp->link; /* If you write like this r=(struct node *)malloc(sizeof(struct node)); then you are creating indepent node which are not connected to it's previous node.r=NULL then r(struct node *)malloc(sizeof(struct node)); it will not link to the previous node . */ } r->data=b; r->link=NULL; } display(struct node *q) { while(q!=NULL) { printf("%d-> ",q->data); q=q->link; } } swap(struct node *fast,int k,int n) { struct node *Kth_fast,*Kth_last; int temp; if(k>n) printf("Swaping is not possible you enter the number greater than number of element of linklist\n"); else { Kth_fast=recursion(fast,k); Kth_last=recursion(fast,n-k+1); } temp=Kth_last->data;

Kth_last->data=Kth_fast->data; Kth_fast->data=temp; } struct node *recursion(struct node *p,int K) { int i=1; while(i++<K) { p=p->link; } return p; }

Q2 - Write a program that takes two arguments at the command line, both strings. The program checks to see whether or not the second string is a substring of the first (without using the substr -- or any other library -- function). One caveat: any * in the second string can match zero or more characters in the first string, so if the input were abcd and the substring were a*c, then it would count as a substring. Also, include functionality to allow an asterisk to be taken literally if preceded by a \, and a \ is taken literally except when preceding an asterisk.

Solution:

The logic behind this solution is that search_within searches to find if at any place in the first string, the second string matches the next part of the first string. The find_at_front function is used to check if at any given starting spot in the first string, the second string matches. The trick to handle is asterisk matching any number of characters is that an asterisk matches any number of characters, so when an asterisk appears at the beginning of a string, that means that whatever remains of the string after the asterisk should match as a substring of the first string. And because of the way find_at_front recursively moves down each string, whenever an asterisk occurs in the second_string, it will be at the front, so we test to see if the remainder of the second string is a substring of the first string. Notice the use of pointer arithmetic to simplify the manipulation of the strings.
Source Code Snippet:
#include <iostream> using namespace std; int count_asterisks(char *str) { int asterisk_count = 0; for(int x=0; x<strlen(str); x++) { if(x=='*' && (x == 0 || str[x-1] != '\\')) { asterisk_count++; } } return asterisk_count; } bool match_within(char *, char *); bool match_at_front(char * search_in, char * search_for) { if(search_for[0] == '\0') //everything matches { return true; } else if(search_in[0] == '\0') //end of first string {

return false; } else if(search_for[0] == '*') { //any number of initial characters are chewed up by the * return match_within(search_in, (search_for+1)); } else if(search_for[0] == '\\' && search_for[1] == '*') { if(search_in[0] == '*') { return match_at_front(search_in + 1, search_for + 2); } else { return false; } } else if(search_for[0] == search_in[0]) { return match_at_front(search_in+1, search_for+1); } else { return false; } } bool match_within(char * search_in, char * search_for) { for(int x=0; x<=strlen(search_in)-strlen(search_for)count_asterisks(search_for); x++) { if(match_at_front(search_in+x, search_for)) { return true; } } return false; } int main(int argc, char* argv[]) { if(argc != 3) { cout<<"Input should be of the form substring str1 str2"; return 0; } if(match_within(argv[1], argv[2])) { cout<<"String "<<argv[2]; cout<<" is contained within "<<argv[1]<<"."; } else { cout<<"String "<<argv[2]<<" is not contained within "; cout<<argv[1]<<"."; }

Вам также может понравиться