1.
Linked List Question
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
● // function for printing the linked list
void print(struct Node* tempHead) {
if(tempHead == NULL) {
print("empty list");
return;
}
while(tempHead != NULL) {
printf("%d ", tempHead -> data);
tempHead = tempHead -> next;
}
}
● // function for pushing the elements in the list
struct Node* push(struct Node* tempHead, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* lastNode = tempHead;
newNode -> data = data;
newNode -> next = NULL;
if(tempHead == NULL) {
tempHead = newNode;
return tempHead;
}
while(lastNode -> next != NULL) {
lastNode = lastNode -> next;
}
lastNode -> next = newNode;
return tempHead;
}
// insert not working, will check later
/*
struct Node* insert(struct Node* tempHead, int n, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = data;
newNode -> next = NULL;
struct Node* temp1 = tempHead;
if(n == 0) {
newNode -> next = tempHead;
tempHead = newNode;
return tempHead;
}
struct Node* temp = temp1;
for(int i = 1; i < n; i++) {
temp = temp -> next;
}
newNode -> next = temp -> next;
temp -> next = newNode;
return temp1;
}
*/
● // function for inserting an element after an element
void insertAfter(struct Node* prev, int data) {
if(prev == NULL)
return;
struct Node* newNode =(struct Node*) malloc(sizeof(struct Node));
newNode -> data = data;
newNode -> next = prev -> next;
prev -> next = newNode;
}
● // function for popping an element
struct Node* pop(struct Node* tempHead) {
if(tempHead == NULL) {
printf("empty list");
return tempHead;
}
if(tempHead -> next == NULL) {
free(tempHead);
return NULL;
}
struct Node* secondLast = tempHead;
while(secondLast -> next -> next != NULL) {
secondLast = secondLast -> next;
}
free(secondLast -> next);
secondLast -> next = NULL;
return tempHead;
}
● // function for reversing the linked list
struct Node* reverse(struct Node* tempHead) {
struct Node *current, *prev, *next;
current = tempHead;
prev = NULL;
while(current != NULL) {
next = current -> next;
current -> next = prev;
prev = current;
current = next;
}
tempHead = prev;
return tempHead;
}
● // main function and static initialization of linked list
int main() {
struct Node* head = NULL;
head = push(head, 2);
head = push(head, 5);
head = push(head, 9);
head = push(head, 12);
head = push(head, 22);
printf("After all pushes: ");
print(head);
/* insert(head, data, after which position want a new node)
0 for position means beginning of the list, 1 meaning after 1st node, 3 meaning after 3rd
node
numbering of node starts from 1
And of course we don't insert after the last element as that is same as push */
// head = insert(head, 31, 3); This is not working, will check again later in the day or maybe
discuss
insertAfter(head -> next -> next, 43);
printf("\nAfter inserting 43 in middle: ");
print(head);
head = pop(head);
printf("\nAfter popping a node: ");
print(head);
head = reverse(head);
printf("\nAfter reversing the linked list: ");
print(head);
printf("\n");
return 0;
}
2. Fizzbuzz question
#include<stdio.h>
#include<stdlib.h>
int main() {
for(int i = 1; i <= 100; i++) {
if(i % 3 == 0 && i % 15 != 0)
printf("Fizz ");
else if(i % 5 == 0 && i % 15 != 0)
printf("buzz ");
else if(i % 15 == 0)
printf("fizzbuzz ");
else
printf("%d ", i);
}
return 0;
}