Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions fib_series.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//Code to print Fibonacci Series issue#25
#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}
27 changes: 27 additions & 0 deletions fib_series2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<stdio.h>
int f(int);

int main()
{
int n, i = 0, c;

scanf("%d", &n);

printf("Fibonacci series terms are:\n");

for (c = 1; c <= n; c++)
{
printf("%d\n", f(i));
i++;
}

return 0;
}

int f(int n)
{
if (n == 0 || n == 1)
return n;
else
return (f(n-1) + f(n-2));
}
105 changes: 105 additions & 0 deletions linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
int data;
struct node* next;
} node;

void insert_node(node** head, int val, int position);

void insert_node(node** head, int val, int position)
{
struct node *curr = *head, *tmp_node = NULL;
int count = 1;

tmp_node = (node*)malloc(sizeof(node));

if (tmp_node == NULL) {
printf("Memory allocation is failed:");
return;
}
tmp_node->data = val;
tmp_node->next = NULL;

if (*head == NULL) {
// List is empty, assigning head pointer to tmp_node
*head = tmp_node;
return;
}
if (position == 1) {
// Inserting node at the beginning of the list
tmp_node->next = *head;
*head = tmp_node;
return;
}
while (curr && count < position - 1) {
curr = curr->next;
count++;
}
if (position > (count + 1)) {
printf("\n position doesn't exists in the list ");
return;
}
if (count + 1 == position && curr->next == NULL) {
// Inseting node at the end of the list
curr->next = tmp_node;
return;
}
// Inserting node in the list at given position
tmp_node->next = curr->next;
curr->next = tmp_node;
}

void print_list(node* head)
{
printf("\nList elements:\n");
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
return;
}

int main()
{
int num_nodes, value, index, position;
node* head = NULL;

printf("Enter the no. of nodes to create list: ");
scanf("%d", &num_nodes);

for (index = 1; index <= num_nodes; index++) {
printf("Enter node data for position %d in the list: ", index);
scanf("%d", &value);
insert_node(&head, value, index);
}
print_list(head);

printf("\nInsert the element at 1st position: ");
scanf("%d", &value);
insert_node(&head, value, 1);
// We have inserted one more element, hence num_nodes get increased by 1
num_nodes += 1;
print_list(head);

printf("\nInsert the element at last position: ");
scanf("%d", &value);
insert_node(&head, value, num_nodes + 1);
// We have inserted one more element, hence num_nodes will get increased by 1
num_nodes += 1;
print_list(head);

printf("\nInsert the element at any position in the list\n");
printf("Enter the position: ");
scanf("%d", &position);
printf("Enter the element value: ");
scanf("%d", &value);
insert_node(&head, value, position);
// We have inserted one more element, hence num_nodes will get increased by 1
num_nodes += 1;
print_list(head);

return 0;
}