List Adt
List Adt
#define LISTHEADER_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
struct Node {
int element;
struct Node *next;
};
List create_header() {
List L = (List)malloc(sizeof(struct Node));
if (L != NULL) {
L->element = -1;
L->next = NULL;
}
return L;
}
void display(List L) {
if (L->next == NULL) {
printf("Singly linked list is empty\n");
} else {
printf("The elements in the singly linked list are:\n");
for (L = L->next; L != NULL; L = L->next) {
printf("%d --> ", L->element);
}
printf("NULL\n"); // Indicate the end of the list
}
}
#endif // LISTHEADER_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include "listheader.h"
int main() {
int choice, prev, x, y;
Position prev_pos;
List l = create_header();
do {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the element after which to insert: ");
scanf("%d", &prev);
prev_pos = find_pos(l, prev);
if (prev_pos == NULL)
printf("Element not found\n");
else {
printf("Enter the element to insert: ");
scanf("%d", &x);
insert(l, x, prev_pos);
}
break;
case 2:
printf("Enter the element to be deleted: ");
scanf("%d", &y);
delete(l, y);
break;
case 3:
display(l);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}