MIT-Curricular/DS/C/Lab/Shortcodes/dcll.c

55 lines
1.1 KiB
C
Raw Permalink Normal View History

2024-10-22 08:48:45 +05:30
#include <stdio.h>
#include <stdlib.h>
typedef struct c { int d; struct c *n, *p; } *C;
C l = NULL;
C new_c(int v) {
C t = (C)malloc(sizeof(struct c));
t->d = v; t->n = t->p = NULL; return t;
}
void insert_beg(int v) {
C t = new_c(v);
if (!l) { l = t; l->n = l->p = l; }
else {
t->n = l->n;
l->n->p = t;
t->p = l;
l->n = t;
}
}
void del_end() {
if (!l) printf("List is empty");
else {
C p = l->p;
p->n = l->n;
l->n->p = p;
free(l);
l = p;
}
}
void traverse() {
if (!l) return;
C c = l->n;
do { printf("%d ", c->d); c = c->n; } while (c != l->n);
printf("\n");
}
int main() {
char cont = 'y'; int ch;
while (cont == 'y') {
printf("1. Insert at beginning\n2. Delete at end\n3. Traverse\n");
scanf("%d", &ch);
if (ch == 1) { int v; scanf("%d", &v); insert_beg(v); }
else if (ch == 2) { del_end(); printf("Element deleted\n"); }
else if (ch == 3) { traverse(); }
printf("Continue? (y/n)\n");
fflush(stdin);
scanf(" %c", &cont);
}
return 0;
}