Algorithm
INPUT: HEADER is a pointer to the header node of the linkedlist
OUTPUT: Prints all the elements of the linkedlist
DATA STRUCTURE: Singly Linkedlist
STEP 1: PTR=HEADER->LINK
STEP 2: WHILE(PTR!=NULL)DO
STEP 3: PRINT(PTR->DATA)
STEP 4: PTR=PTR->LINK
STEP 5: ENDWHILE
STEP 6: STOP
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);
void display();
struct node{
int data;
struct node *link;
};
int main(){
int n;
printf("ENTER THE LIMIT");
scanf("%d",&n);
getnode(n);
return 0;
}
void display(struct node *head){
struct node *ptr;
ptr=head;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->link;
}
}
int getnode(int n){
struct node *prev,*head,*p;
head=NULL;
for(int i=0;i<n;i++)
{
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->link=NULL;
if(head==NULL)
head=p;
else
prev->link=p;
prev=p;
}
display(head);
return 0;
}
Output
user@computer$ : ENTER THE LIMIT5
1
2
3
4
5
1 2 3 4 5