Sei sulla pagina 1di 3

File: /home/debabrata/Desktop/C_progran_Practice/1ST_linked_list.

c
#include<stdio.h> #include<math.h> #include<malloc.h> struct node { int data; struct node *link; }; void append(struct node **, int); void display(struct node *); void addatbeg(struct node **, int); void addafter(struct node *, int , int); int count(struct node *); void del(struct node **, int); int main() { struct node *p; p = NULL; /*Empty linked list */ printf("MY FIRST LINKED LIST ARRAY\n"); append(&p,13); append(&p,23); append(&p,25); append(&p,33); display(p); addatbeg(&p,333); addatbeg(&p,777); display(p); addafter(p,7,0); addafter(p,3,19); display(p); printf("No. of elements in the Linked List = %d \n",count(p)); del(&p,99); del(&p,333); display(p); printf("No. of elements in the Linked List = %d \n",count(p)); return 0; } /* Adds a node at the end of a linked list */ void append(struct node **q, int num) { struct node *temp, *r; if(*q == NULL) { temp = (struct node *)malloc(sizeof (struct node)); temp ->data = num; temp->link = NULL; *q = temp; } else { temp = *q; /* go to last node */ while(temp->link != NULL) temp = temp->link; /* add node at the end */ r = (struct node *)malloc(sizeof(struct node)); r->data = num;

Page 1 of 3

File: /home/debabrata/Desktop/C_progran_Practice/1ST_linked_list.c
r->link = NULL; temp->link = r; } } /* Display the content of the linked list */ void display( struct node *q) { /* traverse the entire linked list */ while( q != NULL) { printf("%d",q->data); q = q->link; printf("\t"); } printf("\n"); } /* Adds a new node at the beginning of the linked list */ void addatbeg(struct node **q, int num) { struct node *temp; /* add a new node */ temp = (struct node *)malloc(sizeof (struct node)); temp->data = num; temp->link = *q; *q = temp; } /* Adds a new node after the shifted number of nodes */ void addafter(struct node *q, int loc, int num) { struct node *temp, *r; int i; temp = q; /* skip to desired portion */ for(i=0; i<loc; i++) { temp = temp->link; /* if end of linked list is encountered */ if(temp == NULL) { printf("There are less than %d elements in the list \n",loc); return; } } /* insert new node */ r = (struct node *)malloc(sizeof (struct node)); r->data = num; r->link = temp->link; temp->link = r; } /* display the contents of the linked list */ int count(struct node *q) { int c=0; while(q != NULL) { q = q->link; c++; } return c; } /* Delete a specified node form the linked list */ void del(struct node **q, int num) { struct node *old, *temp; temp = *q;

Page 2 of 3

File: /home/debabrata/Desktop/C_progran_Practice/1ST_linked_list.c
while( temp != NULL) { if(temp->data == num) { /* If node to be deleted is the first node in the linked list */ if(temp == *q) *q = temp->link; else old->link = temp->link; /* free the memory occupied by the node */ free(temp); return ; } else { old = temp; /* old point to be the previous node */ temp = temp->link; /* go to the nest node */ } } printf("Element %d not found \n",num); }

Page 3 of 3

Potrebbero piacerti anche