Page 1 of 1

Complete Program In C Language and Make It As User Input Program! Instruction is given in program comment NOTE : 1. Plea

Posted: Wed Apr 27, 2022 3:37 pm
by answerhappygod
Complete Program In C Language and Make It As User Input
Program!
Instruction is given in program comment
NOTE :
1. Please Make Sure Make It As User Input
Program!
2. Please add comment in source code
3. I will give you thumbs up.. if you can add comment in
detail! thanks :)
source Code :
/*
- initially Heap memory is empty (Heap memory = a set of addresses
in RAM specifically allocated to store non-static variables of a
process/application)
- our list consists of several nodes
- each node will have a random address
- previous node must have address to next node
- The first node address must be known by the calling program
BASIC STAGE of using a linked list:
1. create a data structure for a node (consisting of a field to
store data and an address/pointer to the next node)
2. create a variable which is a pointer to the address of the first
node
3. when the list is empty, the address of the first node is
NULL
4. allocate memory for a node, and save the pointer
5. at the node referred to by the pointer, enter data, and a
pointer to the next node (NULL)
6. save this node pointer to the first address pointer.
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
/*
node.next is a pointer
* node.next is the object stored at the address pointed to by the
pointer
&node is a pointer
*/
int main(){
struct node * headAddress = NULL; // step 3
struct node * tmpAddress;
struct node * iteratorPtr;
struct node * prevPtr;

//creating our first node by asking the memory
allocation first.
tmpAddress = malloc(sizeof(struct node)); //step
4
(*tmpAddress).data = 39;
(*tmpAddress).next = NULL; // step 5

headAddress = tmpAddress;

//add 1 node in front
tmpAddress = malloc(sizeof(struct node)); //ask for a
new plot address
(*tmpAddress).data = 38;
(*tmpAddress).next = headAddress;

headAddress = tmpAddress;

//add 1 node behind
tmpAddress = malloc(sizeof(struct node)); //new plot
again
(*tmpAddress).data = 40;
(*tmpAddress).next = NULL;

iteratorPtr = headAddress; //iteratorPtr is the
address to check
while(1){
if((*iteratorPtr).next == NULL){
//iteratorPtr refers to the last node
(*iteratorPtr).next =
tmpAddress;
break;
}else{
//the address for the
next check is the address stored in the next column at home at the
current address
iteratorPtr =
(*iteratorPtr).next;
}
}

//add some nodes in the middle ?

//delete start node
tmpAddress = headAddress;
headAddress = (*tmpAddress).next;
free(tmpAddress);
//delete end node
iteratorPtr = headAddress; // what we are
checking now
while(1){
if((*iteratorPtr).next == NULL){
//iteratorPtr refers to the last node
free(iteratorPtr);
(*prevPtr).next =
NULL;
break;
}else{
prevPtr = iteratorPtr; //
prev address
iteratorPtr =
(*iteratorPtr).next; //Next check address
}
}

//delete the node in the middle ?
if(headAddress != NULL){
printf("List contains:\n");
iteratorPtr = headAddress;
while(iteratorPtr != NULL){
printf(" [%d] :
%d\n",iteratorPtr, (*iteratorPtr).data);
iteratorPtr =
(*iteratorPtr).next;
}
}else{
printf("List empty\n");
}
return 0;
}