linked list implementation
#include<iostream>
#include<stdlib.h>
using namespace std;
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
void printReverse(struct node* head)
{
// Base case
if(head == NULL)
return;
// print the list after head node
printReverse(head->next);
// After everything else is printed, print head
cout<<"\nReverse Printing is \t"<<head->data;
}
/* Function to forward the linked list */
void printForward(struct node* head)
{
// Base case
if(head == NULL)
return;
cout<<"\nForward Printing is \t"<<head->data;
// print the list after head node
printForward(head->next);
}
void search(struct node *head, int key, int index)
{
if (head->data == key)
{
cout<<"Key found at Position:\n"<< index<<endl;
}
if (head->next == NULL)
{
return;
}
search(head->next, key, index - 1);
}
/*UTILITY FUNCTIONS*/
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct 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 pochar to the new node */
(*head_ref) = new_node;
}
/* Driver program to test above function*/
int main()
{
struct node* head = NULL;
int key=0,num=5; //num is number of nodes we have put in linked list here in this case it is 5
push(&head, 5);
push(&head, 2);
push(&head, 3);
push(&head, 4);
push(&head, 5);
printForward(head);
cout<<endl;
printReverse(head);
cout<<endl;
cout<<"please enter the number you want to find \n";
cin>>key;
search(head, key, num);
getchar();
system("pause");
return 0;
}
#include<stdlib.h>
using namespace std;
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
void printReverse(struct node* head)
{
// Base case
if(head == NULL)
return;
// print the list after head node
printReverse(head->next);
// After everything else is printed, print head
cout<<"\nReverse Printing is \t"<<head->data;
}
/* Function to forward the linked list */
void printForward(struct node* head)
{
// Base case
if(head == NULL)
return;
cout<<"\nForward Printing is \t"<<head->data;
// print the list after head node
printForward(head->next);
}
void search(struct node *head, int key, int index)
{
if (head->data == key)
{
cout<<"Key found at Position:\n"<< index<<endl;
}
if (head->next == NULL)
{
return;
}
search(head->next, key, index - 1);
}
/*UTILITY FUNCTIONS*/
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct 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 pochar to the new node */
(*head_ref) = new_node;
}
/* Driver program to test above function*/
int main()
{
struct node* head = NULL;
int key=0,num=5; //num is number of nodes we have put in linked list here in this case it is 5
push(&head, 5);
push(&head, 2);
push(&head, 3);
push(&head, 4);
push(&head, 5);
printForward(head);
cout<<endl;
printReverse(head);
cout<<endl;
cout<<"please enter the number you want to find \n";
cin>>key;
search(head, key, num);
getchar();
system("pause");
return 0;
}
Comments
Post a Comment