Algorithm
INPUT: HEADER is a pointer to the header node of the linkedlist
OUTPUT: LINKEDLIST WITH THE ADDED ELEMENT
DATA STRUCTURE: Circularly Linkedlist
STEP 1: NEW=GETNODE(NODE)
STEP 2: IF(NEW=NULL)THEN
STEP 3: PRINT("MEMORY INSUFFICIENT")
STEP 4: EXIT
STEP 5: ELSE
STEP 6: NEW->DATA=X
STEP 7: NEW->LINK=HEADER->LINK
STEP 8: HEADER->LINK=NEW
STEP 9: ENDIF
STEP 10:STOP
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);void display();int x, pos, n;void insertatbeg();
struct node
{ int data;
struct node *link;};
struct node *prev, *head, *news, *p, *ptr;
struct node *q1;
struct node *header, *header1;
struct node *q, *r, *s;
int main(){
printf("ENTER THE LIMIT ");
scanf("%d", &n);
getnode(n);
printf("ENTER THE ELEMENT WANT TO INSERT:");
scanf("%d",&x);
insertatbeg();
display(header);
return 0;
}
void display(struct node *head1)
{
struct node *ptr;
ptr = head1->link;
// ptr=header->link;
while (ptr != header)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
}
void insertatbeg(){
news=malloc(sizeof(struct node));
news->data=x;
news->link=header->link;
header->link=news;
}
int getnode(int n)
{
header = malloc(sizeof(struct node));
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;
}
p->link=header;
header->link = head;
display(header);
return 0;
}
Output
user@computer$ : ENTER THE LIMIT 5
1
2
3
4
5
1 2 3 4 5 ENTER THE ELEMENT WANT TO INSERT:6
6 1 2 3 4 5