LinkedList Deletion at the End

Algorithm

INPUT: HEADER IS A POINTER TO THE HEADER NODE OF THE LINKEDLIST
OUTPUT: LINKEDLIST WITHOUT LAST ELEMENT
DATA STRUCTURE: SINGLY LINKEDLIST

STEP 1: START
STEP 2: PTR=HEADER->LINK
STEP 3: WHILE(PTR->LINK!=NULL)
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 newnode();int x;struct node{int data;struct node *link;};struct node *prev,*head,*news,*p,*ptr,*ptr1;
 struct node *header;
 int main(){
 int n;
 printf("ENTER THE LIMIT");
 scanf("%d",&n);
 getnode(n);
 return 0;}
 void display(struct node *head){
 struct node *ptr;
 ptr=header->link;
 while(ptr!=NULL){
 printf("%d ",ptr->data);
 ptr=ptr->link;} }
 void newnode(){
 ptr=head;
 ptr1=head;
 while(ptr->link!=NULL){
    ptr1=ptr; 
 ptr=ptr->link;}
 ptr1->link=NULL;
 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;
 }header->link=head;
 display(header);
 printf("deletion from last");
 newnode();
 display(header);
 return 0;}

Output

user@computer$ : ENTER THE LIMIT3
1
2
3
1 2 3 deletion from last1 2

Leave a Comment

Your email address will not be published. Required fields are marked *