Skip to content

Instantly share code, notes, and snippets.

@jonathan-nwosu
Created June 15, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-nwosu/c8168d76514c6c1cf275ccdfd77b6e5e to your computer and use it in GitHub Desktop.
Save jonathan-nwosu/c8168d76514c6c1cf275ccdfd77b6e5e to your computer and use it in GitHub Desktop.
C++ linkedlist recursion
//
// main.cpp
//
// Created by Jonathan Nwosu on 05/04/2017.
// Copyright © 2017 Jonathan Nwosu. All rights reserved.
//
//searching through a linkedlist using recursion... (simply traversing)
#include <iostream>
using namespace std;
struct node{ //creating an object called node, with 2 parts to it
int data; //data variable holding an interger
node* next; // Putting in a node pointer called next
};
void SearchRecursive(node* Ptr, int searchValue) {
if(Ptr == NULL){ //want to passs in the head pointer, we want to see if the head pointer is passing to anything..
cout << searchValue << " was not found in the list \n";
} // if not case were still somewhere in the list, check to see if whatever were pointing to contains the data
//we are looking for ...
else if(Ptr->data == searchValue){
cout << searchValue << " is in the list \n";
}
else { //now we are calling a function within a function, so its recursion...
SearchRecursive(Ptr->next, searchValue); //were making pointer point to the next node
}
}
node* Insert(Node* head, int data){
node* n = new node;
node* head = n;
node* t = n;
n->data = 1;
for(int i = 2; i < 10; i++){
//first thing we want to do is create a new node and have n point to that new node...
n = new node; //but we need to fill this new node in with data
n->data = i; // we access the data part of that node and we say its equal to i...
t->next = n; //works as t is going to be 1 step behind n every time..
t = n;
}
n->next = NULL; //very end of the list will basically point to nothing...
}
void Print(Node* head){
t = head;
while(t != NULL){
cout << t->data << " ";
t = t->next;
}
cout << "\n\n";
}
int main() {
node* n = new node; //which means node pointer, n points to a new node
node* head = n; //head points to whatever n points to
node* t = n; //t points to whatever n points to as well, t is the temporary node that points to the data field in each node.
n->data = 1; //placed 1 inside the new node we have created... node creates value 1
for(int i = 2; i < 10; i++){
//first thing we want to do is create a new node and have n point to that new node...
n = new node; //but we need to fill this new node in with data
n->data = i; // we access the data part of that node and we say its equal to i...
t->next = n; //works as t is going to be 1 step behind n every time..
t = n;
}
n->next = NULL; //very end of the list will basically point to nothing...
t = head;
while(t != NULL){
cout << t->data << " ";
t = t->next;
}
cout << "\n\n";
SearchRecursive(head, 4);
SearchRecursive(head, 11);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment