Upload files to "DS/C/Lab/Shortcodes"
This commit is contained in:
parent
325008fe63
commit
4d3da88635
5 changed files with 465 additions and 0 deletions
54
DS/C/Lab/Shortcodes/dcll.c
Normal file
54
DS/C/Lab/Shortcodes/dcll.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue