Algorithm
INPUT: HEADER IS A POINTER TO THE HEADER NODE OF THE LINKEDLIST
OUTPUT: LINKEDLIST WITHOUT FIRST ELEMENT
DATA STRUCTURE: SINGLY LINKEDLIST
STEP 1: START
STEP 2: PTR=HEADER->LINK
STEP 3: WHILE(PTR->LINK!=HEADER)
STEP 4: PTR1=PTR
STEP 5: PTR=PTR->LINK
STEP 6: ENDWHILE
STEP 7: PTR1->LINK=NULL
STEP 8: FREE(PTR)
STEP 9: END
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);
void display();
void deleteatend();
int x, pos, n, item;
struct node
{
int data;
// struct node *llink;
struct node *link;
};
struct node *prev, *head, *news, *p, *ptr;
struct node *q1;
struct node *header, *header1;
int main()
{
int a;
char ch;
printf("ENTER THE LIMIT ");
scanf("%d", &n);
getnode(n);
deleteatend();
printf("List after deletion");
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 deleteatend(){
struct node *ptr1;
ptr1=malloc(sizeof(struct node));
ptr=header->link;
while(ptr->link!=header)
{
ptr1=ptr;
ptr=ptr->link;
}
ptr1->link=header;
free(ptr);
}
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 List after deletion1 2 3 4