Rohit Khandelwal

Student at Chandigarh University

Delete A Value Linked List Program Using C

#include<stdio.h> #include<stdlib.h> int np; struct Node{     int data;     struct Node* next; }; void value_key(struct Node** head_ref,int value) {     int i;     struct Node* temp=*head_ref;     for(i=0;i<=value;i++)     {         if(i==value)         {             //printf("%d",i);             printf("\nvalue at provided posn is %d\n",temp->data);             np=temp->data;         }         else         temp=temp->next;         //printf("%d",main_key);     } } void delete1(struct Node **head_ref,int key) {     struct Node* temp=*head_ref,*prev;     if(temp!=NULL && temp->data==key)     {         *head_ref=temp->next;         free(temp);         return;     }     while(temp!=NULL && temp->data!=key)     {         prev=temp;         temp=temp->next;     }     if(temp==NULL)   //not in list     {         return;     }     prev->next=temp->next;     free(temp); } void push(struct Node **head_ref,int y) {     struct Node *new1=(struct Node*)malloc(sizeof(struct Node));     new1->data=y;     new1->next=*head_ref;     *head_ref=new1; } int main() {     struct Node *head=NULL;     push(&head,7);     push(&head,1);     push(&head,9);     push(&head,12);     value_key(&head,0);    // delete1(&head,1);    //struct Node *temp =head;     delete1(&head,np);     while(head!=NULL)     {         printf("%d\n",head->data);         head=head->next;     }     return 0; }

Linked List Length Using C Program

#include<stdio.h> struct node {     int data;     struct node* next; }; void getlength(struct node** head_ref) {     struct node *temp=*head_ref;     int count=0;     while(temp!=NULL)     {         temp=temp->next;         count++;     }     printf("total number of nodes are %d",count); } void push(struct node **head_ref,int x) {     struct node* new1;     new1=(struct node*)malloc(sizeof(struct node));     new1->data=x;     new1->next=*head_ref;     *head_ref=new1; } void delete1(struct node** head_ref,int key) {     struct node* temp=*head_ref;     struct node *prev;     while(temp!=NULL)     {         prev=temp;         temp=temp->next;         if(temp->data==key)         {             break;         }     }     prev->next=temp->next;     free(temp); } int main() {     struct node* head=NULL;     push(&head,89);     push(&head,65);     push(&head,23);     delete1(&head,89);     getlength(&head); }