Yogesh M Joshi
BAN USER1. Sum all elements in array
10 + 6 + 3 + 4 + 7 + 5 + 2 + 4 + 9 + 1 = 59
2. Sum all numbers from 1 to N here N is 10 = 55
3. Subtract 59-55 = 4
So 4 is duplicate number in array.
#include <stdio.h>
#include <conio.h>
typedef struct NODE
{
int data;
struct NODE *next;
}*PNODE,NODE;
void printList(PNODE head)
{
printf("\nLinked List ");
while(head)
{
printf(" -> %d", head->data);
head = head -> next;
}
}
PNODE getNode(int data)
{
PNODE node = (PNODE)malloc(sizeof(NODE));
node -> data = data;
node -> next = NULL;
return node;
}
void delNode(PNODE node)
{
free(node);
node = NULL;
}
void createList(PNODE head)
{
int i = 0;
for(i = 1; i< 10; i++)
{
head -> next = getNode(i);
head = head->next;
}
}
PNODE revList(PNODE head, int len)
{
PNODE current = head;
PNODE next;
PNODE prev = NULL;
PNODE newHead = NULL;
PNODE newTail = NULL;
int i = 0;
while(current)
{
i = 0;
prev = NULL;
while(current && i < len)
{
next = current -> next;
current -> next = prev;
prev = current;
current = next;
i++;
}
if(newHead == NULL)
{
newHead = prev;
}
else
{
newTail = newHead;
while(newTail -> next)
newTail = newTail -> next;
newTail -> next = prev;
}
}
return newHead;
}
int main()
{
PNODE head = getNode(0);
PNODE newHead = NULL;
clrscr();
createList(head);
printList(head);
newHead = revList(head, 4);
printList(newHead);
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
void main()
{
char str[] = "This is a simple string ...";
char *p1 = str;
char *p2 = str;
char *p3 = NULL;
char c;
clrscr();
while(*p2 != '\0')
p2++;
p2--;
/* Reverse the string */
while(p1 < p2)
{
c= *p1;
*p1 = *p2;
*p2 = c;
p1 ++;
p2 --;
}
p1 = str;
p2 = str;
/* Reverse the words */
while(*p1 != '\0')
{
while(*p1 == ' ' && *p1 != '\0')
p1++;
if(*p1 == '\0')
break;
p2 = p1;
while(*p2 != ' ' && *p2 != '\0')
p2++;
p3 = p2;
if(*p2 == ' ' || *p2 == '\0')
p2 --;
while(p1 < p2)
{
c = *p1;
*p1 = *p2;
*p2 = c;
p1++;
p2--;
}
p1 = p3;
p2 = p3;
}
printf("\n%s", str);
getch();
}
I am not sure how to do it in single iteration.
@Ankur : Have a look at program below its working ...
- Yogesh M Joshi January 20, 2013