#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* Function to reverse the linked list */
void tailToHead(struct Node* head)
{
// Base case
if (!head)
return;
// print the list after head node
tailToHead(head->next);
// After everything else is printed, print head
printf("%d ", head->data);
}
void headToTail(struct Node* head)
{
// Base case
if (!head)
return;
// After everything else is printed, print head
printf("%d ", head->data);
// print the list after head node
headToTail(head->next);
}
void iheadToTail(struct Node* head)
{
while (head!= NULL)
{
// print current data
printf("%d ", head->data);
// increase pointer
head=head->next;
}
}
/*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;
}
/* Drier program to test above function*/
int main()
{
// Let us create linked list 10->20->30->40
struct Node* head = NULL;
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
printf("\nrecursive Head to tail \n");
headToTail(head);
printf("\n iterative Head to tail \n");
iheadToTail(head);
printf(" \n recursive Tail to Head \n");
tailToHead(head);
return 0;
}
Output:-
recursive Head to tail 10 20 30 40 iterative Head to tail 10 20 30 40 recursive Tail to Head 40 30 20 10
C programming Write an iterative and recursive version of a function to print out a linked...
Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...
1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...
C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...
Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...
Please write program in C language. 2.Write a recursive int function to return the number of even integers in a linked list. Each node in the list has an integer number (declared as int number) and a pointer to the next node (declared as NODE *next). The function is defined as int count (NODE *); and is called as follows: x = count(head);
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...
C LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object) typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial term associated with the object int coef; // power of the polynomial term associated with the object int power; // pointer to the next object (to facilitate a doubly linked list) struct Term *next; // pointer to the previous object (to facilitate a doubly linked list) struct Term...