Untitled
unknown
plain_text
a month ago
1.6 kB
4
Indexable
#include <stdio.h> #include <stdlib.h> struct element { int data; struct element *link; }; struct element *head = NULL; void addl(int x) { struct element *p = (struct element *) malloc(sizeof(struct element)); if (p == NULL) { printf("No memory allocated \n"); return; } p->data = x; p->link = NULL; if (head == NULL) { head = p; return; } struct element *current = head; while (current->link != NULL) { current = current->link; } current->link = p; } void deletel(int x){ struct element *current = head; struct element *prev = NULL; while(current != NULL){ if (current->data == x){ if(prev != NULL){ prev->link = current->link; } else{ head = current->link; } free(current); return; } prev = current; current = current->link; } printf("Element not found \n"); } void printl(){ if (head==NULL){ printf("Linked list is empty \n"); return; } struct element *current = head; while(current!=NULL){ printf("%d ",current->data); current=current->link; } printf("\n"); } int main() { addl(5); addl(6); addl(7); addl(8); printf("Linked List: "); printl(); deletel(7); printf("Linked List after deletion: "); printl(); return 0; }
Editor is loading...
Leave a Comment