Skip to main content

C program to delete node from single linked list when just address of the previous node to be deleted is given

shiv@ubuntu:~/ds/list$ cat deln.c
/*
 * C Program to delete node in the single linked list, when address of the
 *previous  node to be deleted is given
 */

#include <stdio.h>
#include <stdlib.h>

struct list {
    int d;
    struct list *next;
};

/*
 * function to delete node when just address of the node to be
 * deleted is given in the single linked list
 */
void deletenode(void **ptr)
{
  struct list **temp, *del;

  if(*ptr == NULL) {
     printf("Invalid pointer\n");
     return;
  }
  temp= (struct list **)ptr;
  printf("The address of previous node of the node to be deleted is %p data %d\n", *temp, (*temp)->d);

  if((*temp)->next && (*temp)->next->next){
     /* any node apart from node before last or last node in the list */
     (*temp)->d = (*temp)->next->d;
     del = (*temp)->next;
     (*temp)->next = (*temp)->next->next;
     free(del);
  } else if((*temp)->next) {
           /* This is  node before last node */
            (*temp)->d = (*temp)->next->d;
            free((*temp)->next);
            (*temp)->next = NULL;
  } else {
       /* this is last node we can't delete from it's previous node next pointer
        * but we can free it and make it NULL, really we can't delete last node
        * it is not possible to remove
        */
       free(*temp);
       (*temp)=NULL;
  }
   
}

void display(struct list *temp)
{
 printf("Elements in list are\n");
 while(temp != NULL){
        printf("ADDR %p data %d next %p\n", temp, temp->d, temp->next);
        temp=temp->next;
 }

}
int main()
{
  struct list *head=NULL, *temp;
  void *ptr;

  /* testing program by creating list from below array */
  int a[]={1 ,3 ,4,5,6,7,8,9};
  int i;

  for(i=0;i<8; i++)
  {
     temp=malloc(sizeof(struct list));
     if(!temp){
    return;
     }
     temp->d = a[i];
     temp->next = NULL;
     if(head == NULL){
    head = temp;
     } else {
        temp->next = head;
    head = temp;
    }
 }

 display(head); /* display list before deleting */
 printf("Enter address of previous node to be deleted\n");
 scanf("%p", &ptr);
 deletenode(&ptr);
 display(head);  /* display list after delete */
}
/* end of the program */



Comments

Popular posts from this blog

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * str...

simple program to create Orphan process

shiv@ubuntu:~/ds/unix$ cat  orp.c /*  * Program to create orphan process @ Linux  * getpid() gives process PID and   * getppid() gives process's parent ID   * here main() process ID is parent id is current shells PID  * once process becomes orphan it is adopted by init process(it's PID is 1)  */   #include<stdio.h> #include<unistd.h> int main() {  pid_t p; /* create child process */  p=fork();  if(p==0) {     /* fork() returns Zero to child */     sleep(10);  }  printf("The child process pid is %d parent pid %d\n", getpid(), getppid()); /*parent/child waits for 20 secs and exits*/  sleep(20);  printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());  return 0; } O/p shiv@ubuntu:~/ds/unix$ ./a.out The child process pid is 2575 parent pid 1922 The child process pid is 2576 parent pid 2575 Process 2575 is done it...

C99 and C11 standards

as part of my job  sometimes I get chance to interview C programmers, till today, number of  C programmers I have interviewed none knows about new(C11 & C99) features of C programming language, some even not heard of these standards that made me to add  this page, I strongly believe if we are working on product which is born out of C programming, one needs to know about C99 and C11 features as well as Safe-C libraries which improves software quality and security C99 standard has become absolute by now, considering C11 standard which is part of our products which are written in C programming language.  if we name few missing elements in C89 programming features 1) No array bounds checking 2) No variable length arrays 3) No inline functions 4) No support for Boolean data type 5) No complex number type support 6) No single line comment (//) 7) No variable number of arguments in MACRO 8) No support for declaring variables in blocks . and more .. all...