From ee19e7e5470bacce0886cd89a1cc4a8a06bfa8e6 Mon Sep 17 00:00:00 2001 From: vishvajeet-y Date: Fri, 25 Oct 2019 19:16:31 +0530 Subject: [PATCH 1/2] my first commit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c3e050b..0235edf 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Data Structures and Algorithms in C -## Won't count towards prize +# Won't count towards prize +# May use of C and C++ language + From 4b1cd6ba94c5251a3ba3e7d81f366bece23d24fa Mon Sep 17 00:00:00 2001 From: himanshu342 <43965914+himanshu342@users.noreply.github.com> Date: Wed, 7 Oct 2020 23:56:37 +0530 Subject: [PATCH 2/2] link list hackoctober --- link list | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 link list diff --git a/link list b/link list new file mode 100644 index 0000000..97df659 --- /dev/null +++ b/link list @@ -0,0 +1,68 @@ +// Simple C++ program to find n'th node from end +#include +using namespace std; + +/* Link list node */ +struct Node { + int data; + struct Node* next; +}; + +/* Function to get the nth node from the last of a linked list*/ +void printNthFromLast(struct Node* head, int n) +{ + int len = 0, i; + struct Node* temp = head; + + // count the number of nodes in Linked List + while (temp != NULL) { + temp = temp->next; + len++; + } + + // check if value of n is not + // more than length of the linked list + if (len < n) + return; + + temp = head; + + // get the (len-n+1)th node from the beginning + for (i = 1; i < len - n + 1; i++) + temp = temp->next; + + cout << temp->data; + + return; +} + +void push(struct Node** head_ref, int new_data) +{ + /* allocate node */ + struct Node* new_node = new Node(); + + /* put in the data */ + new_node->data = new_data; + + /* link the old list off the new node */ + new_node->next = (*head_ref); + + /* move the head to point to the new node */ + (*head_ref) = new_node; +} + +// Driver Code +int main() +{ + /* Start with the empty list */ + struct Node* head = NULL; + + // create linked 35->15->4->20 + push(&head, 20); + push(&head, 4); + push(&head, 15); + push(&head, 35); + + printNthFromLast(head, 4); + return 0; +}