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
55
DS/C/Lab/Shortcodes/dllconcs.c
Normal file
55
DS/C/Lab/Shortcodes/dllconcs.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct node {
|
||||
int d;
|
||||
struct node *l, *r;
|
||||
} *N;
|
||||
|
||||
void concat(N *X1, N *X2) {
|
||||
if (*X1 == NULL) *X1 = *X2;
|
||||
else if (*X2) {
|
||||
N t = *X1;
|
||||
while (t->r) t = t->r;
|
||||
t->r = *X2;
|
||||
(*X2)->l = t;
|
||||
}
|
||||
}
|
||||
|
||||
N new_node(int d) {
|
||||
N n = (N)malloc(sizeof(struct node));
|
||||
n->d = d;
|
||||
n->l = n->r = NULL;
|
||||
return n;
|
||||
}
|
||||
|
||||
void insert(N *h, int d) {
|
||||
N n = new_node(d);
|
||||
if (!*h) *h = n;
|
||||
else {
|
||||
N t = *h;
|
||||
while (t->r) t = t->r;
|
||||
t->r = n;
|
||||
n->l = t;
|
||||
}
|
||||
}
|
||||
|
||||
void print(N h) {
|
||||
while (h) {
|
||||
printf("%d ", h->d);
|
||||
h = h->r;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
N X1 = NULL, X2 = NULL;
|
||||
insert(&X1, 1);
|
||||
insert(&X1, 2);
|
||||
insert(&X1, 3);
|
||||
insert(&X2, 4);
|
||||
insert(&X2, 5);
|
||||
concat(&X1, &X2);
|
||||
print(X1);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue