#include typedef struct _node { char element; struct _node *next; } node; typedef node *olist; olist init () { olist L; /* Create the dummy node */ L = (node *)malloc(sizeof(node)); L -> element = '\0'; L -> next = NULL; return L; } olist insert ( olist L , char ch , int pos ) { int i; node *p, *n; if (pos < 0) { fprintf(stderr, "insert: Invalid index %d\n", pos); return L; } p = L; i = 0; while (i < pos) { p = p -> next; if (p == NULL) { fprintf(stderr, "insert: Invalid index %d\n", pos); return L; } ++i; } n = (node *)malloc(sizeof(node)); n -> element = ch; n -> next = p -> next; p -> next = n; return L; } olist delete ( olist L , int pos ) { int i; node *p; if (pos < 0) { fprintf(stderr, "delete: Invalid index %d\n", pos); return L; } p = L; i = 0; while ((i < pos) && (p -> next != NULL)) { p = p -> next; ++i; } if (p -> next == NULL) { fprintf(stderr, "delete: Invalid index %d\n", pos); return L; } p -> next = p -> next -> next; return L; } int isPresent ( olist L , char ch ) { int i; node *p; i = 0; p = L -> next; while (p != NULL) { if (p -> element == ch) return i; p = p -> next; ++i; } return -1; } char getElement ( olist L , int pos ) { int i; node *p; i = 0; p = L -> next; while ((i < pos) && (p != NULL)) { p = p -> next; ++i; } if (p == NULL) { fprintf(stderr, "getElement: Invalid index %d\n", pos); return '\0'; } return p -> element; } void print ( olist L ) { node *p; p = L -> next; while (p != NULL) { printf("%c", p -> element); p = p -> next; } } int main () { olist L; L = init(); L = insert(L,'a',0); printf("Current list is : "); print(L); printf("\n"); L = insert(L,'b',0); printf("Current list is : "); print(L); printf("\n"); L = delete(L,5); printf("Current list is : "); print(L); printf("\n"); L = insert(L,'c',1); printf("Current list is : "); print(L); printf("\n"); L = insert(L,'b',3); printf("Current list is : "); print(L); printf("\n"); L = delete(L,2); printf("Current list is : "); print(L); printf("\n"); L = insert(L,'z',8); printf("Current list is : "); print(L); printf("\n"); L = delete(L,2); printf("Current list is : "); print(L); printf("\n"); printf("Element at position 1 is %c\n", getElement(L,1)); }