In: Computer Science
#include<stdio.h>
#include<stdlib.h>
struct listNode{
int data;
struct listNode *nextptr;
};
typedef struct listNode node;
void insert(node*);
void showList(node*);
void printListBackwards(node *);
int main(void)
{
node *list1;
printf("\n Create a sorted list..");
printf("\n Enter values for the first list (-999 to end):");
list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list
node
insert(list1); //insert values by calling the function insert
showList(list1); //display values entered by user
printf("\n After recursively reversing the list is :\n");
printListBackwards(list1); //print the values in reverse order
using the function
return (0);
}
void insert(node *list)
{
printf("\n Enter data :");
scanf("%d",&list->data);
if(list->data ==-999) //end data entry if -999
{
(list->nextptr);
}
else
{
list->nextptr = (node *)malloc(sizeof(node));
insert(list->nextptr);
}
}
void showList(node *list)
{
node *p;
for (p=list;p->nextptr !=NULL;p=p->nextptr) //loop until the
end of list and print values
printf("%d",p->data);
if(list->nextptr ==NULL) //if the list does not have nodes
{
printf("\n The list is empty!");
}
}
void printListBackwards(node *list)
{ if(list->nextptr ==NULL) //if the end of list is
encountered
return;
else //if there are nodes in the list,use the function recursively
to print the list
{
printListBackwards(list->nextptr);
printf("%d",list->data);
}
}
can you provide a pseudocode for this program
This program can get input and store in singular linked list and Display all values in given order and in reverse order
Struct Listnode
Contains
int data;
Listnode *nextptr;
end
Function Insert --
Inserting Elements to nodes
Pass in :
Node pointer
Input
Display a
message to get values
Get the value
from keyboard
Store in data
part of node
IF data
is -999
Then point the
next node
End getting
values
ELSE
Create a new
node of Listnode size
Point the
current listpointer towards the newly created Node
Call : Insert Function
Pass Out:
nothing
Function showList --
Displaying the values in lists of node created
Pass in :
Node pointer pointing initial node
Initialize a new node pointer
FOR (Node
pointer = initial node ; pointer is not null ; point the pointer
towards next node)
Display the data
part of current node pointed by pointer
IF
pointer is NULL
Then Display
List is empty
Pass Out
: nothing
Function printListBackwards
-- Displaying the values in list of nodes in reverse order
Pass in :
Node pointer pointing initial node
IF
pointer is NULL
Then end
Function
ELSE
Call : printListBackwards
Display data
part of the current node pointed by pointer
Pass Out
: nothing
Function main -- Program
starts here
Pass in :
nothing
Create a new node pointer
Display Create a sorted list
Display a message to get First
value and 'Enter -999 to end'
Allocate memory to node
Call :
Insert ,pass node pointer as argument
Call :
showList ,pass node pointer as argument
Call
:printListBackwards ,pass node pointer as argument
pass Out:
nothing
End of the Program