Developer community
0 votes
408 views
asked by | 408 views

1 Answer

0 votes
#include "stdio.h"
 
 
// Variables
 
typedef struct node {
 
int value;
 
struct node *next;
 
}mynode;
 
 
// Globals (not required, though).
 
mynode *head, *tail, *temp;
 
 
// Functions
 
void add(int value);void iterative_reverse();void print_list();
 
 
// The main() function
 
int main()
 
{
 
head=(mynode *)0; // Construct the linked list.
 
add(1);
 
add(2);
 
add(3);
 
//Print it
 
print_list();
 
 
// Reverse it.
 
iterative_reverse();
 
 
//Print it again
 
print_list();
 
 
return(0);
 
}
 
// The reverse function
 
void iterative_reverse()
 
{
 
mynode *p, *q, *r;
 
if(head == (mynode *)0)
 
{
 
return;
 
}
p = head; q = p->next;
 
p->next = (mynode *)0;
 
while (q != (mynode *)0)
 
{
 
r = q->next;
 
q->next = p;
 
p = q; q = r;
 
}
 
head = p;
 
}
 
 
// Function to add new nodes to the linked list
 
void add(int value)
 
{
 
temp = (mynode *) malloc(sizeof(struct node));
 
temp->next=(mynode *)0;
 
temp->value=value;
 
if(head==(mynode *)0)
 
{
 
head=temp;
 
tail=temp;
 
}
 
else
 
{
 
tail->next=temp;
 
tail=temp;
 
}
 
}
 
// Function to print the linked list.
 
void print_list()
 
{
 
printf("\n\n");
 
for(temp=head; temp!=(mynode *)0; temp=temp->next)
 
{
 
printf("[%d]->",(temp->value));
 
}
 
printf("[NULL]\n\n");
 
}
 
answered by

Related questions

0 votes
1 answer
0 votes
1 answer
asked by arnav Sr Member (1.3k points) 9 21 28 | 60 views
0 votes
1 answer
asked by arnav Sr Member (1.3k points) 9 21 28 | 525 views
0 votes
1 answer
0 votes
1 answer
asked by sara Guru (24.6k points) 30 54 84 | 3.3k views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users