1 A linked list is a linear data structure consisting of a s
(1) A linked list is a linear data structure consisting of a set of nodes, where each one except the last one points to the next node in the list. (Appendix A provides more information about linked lists.) Suppose we have the set of 5 nodes shown in the illustration below. These nodes have been scrambled up and placed in a MARIE program as shown below. Write a MARIE program to traverse the list and print the data in order as stored in each node.
MARIE program fragment:
Address (Hex)
00D
Addr,
Hex
????
/Top of list pointer (you fill address of Node1)
00E
Node2,
Hex
0032
/Node’s data is the character ‘2’
00F
Hex
????
/Address of Node3
010
Node4,
Hex
0034
/Character ‘4’
011
Hex
????
012
Node1,
Hex
0031
/Character ‘1’
013
Hex
????
014
Node3,
Hex
0033
/Character ‘3’
015
Hex
????
016
Node5,
Hex
0035
/Character ‘5’
017
Hex
0000
/Indicates terminal node
(2)
Write a MARIE program to calculate N! = 1 x 2 x 3 x 4 x … x N, where the user enters N. Your program should include a recursive subroutine to calculate the N!. Think carefully about how you can set up your stack to accomplish the recursion.
| Address (Hex) | ||||
| 00D | Addr, | Hex | ???? | /Top of list pointer (you fill address of Node1) |
| 00E | Node2, | Hex | 0032 | /Node’s data is the character ‘2’ |
| 00F | Hex | ???? | /Address of Node3 | |
| 010 | Node4, | Hex | 0034 | /Character ‘4’ |
| 011 | Hex | ???? | /Address of Node5 | |
| 012 | Node1, | Hex | 0031 | /Character ‘1’ |
| 013 | Hex | ???? | /Address of Node2 | |
| 014 | Node3, | Hex | 0033 | /Character ‘3’ |
| 015 | Hex | ???? | /Address of Node4 | |
| 016 | Node5, | Hex | 0035 | /Character ‘5’ |
| 017 | Hex | 0000 | /Indicates terminal node |
Solution
I put both programs in this box
program 1:
#include<conio.h>
#include<stdio.h>
#define NULL 0 //defining null=0
struct node
{
int data;
struct node *next;
}*first=NULL,*last=NULL,*nodeptr=NULL,*temp=NULL;
void main()
{
char ch;
clrscr();
//to create nodes in the linked list
do{
temp=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the Data in the Node:\");
scanf(\"%d\",&temp->data);
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
printf(\"do you want to create another node in the list[Y/N]?\ \");
scanf(\"%c\",&ch);
if(ch!=\'Y\'||ch!=\'y\')
mariefun(first);
exit(0);
}while(1);
//traverse the list as given in the MARIE program fragment
//to do this put a \"nodeptr\" to point the present node and print the next node data and delete that node
}
void mariefun(struct node *first)
{
nodeptr=first
while(first!=NULL)
{
if(nodeptr==first)
{
temp=first;
nodeptr=first->next;
printf(\"%d\",&nodeptr->data);
temp->next=nodeptr->next;
nodeptr=nodeptr->next;
}
else
{
temp=nodeptr;
if(temp->next==NULL)
{
printf(\"%d\",&first->data);
first=first->next;
}
else
{
printf(\"%d\",&temp->next->data);
nodeptr=temp->next;
}
}
}
}
program 2:
#include <stdio.h>
long int Factorial(long int );
int main()
{
long int n,r ;
printf(\"Enter a number>0\");
scanf(\"%ld\", &n);
r=Factorial(n);
printf(\"Factorial of %d is %d\", n,r);
return 0;
}
long int Factorial(long int n)
{
if (n >= 1)
return n*Factorial(n-1);
else
return 1;
}



