Asd c4

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

struct dl_node *search(struct dl_list l, int v) //l is used when not needed to

modiffy list, *l when you have to modify it

void insertBefore(struct dl_list *l,struct dl_node *X, int v){ /dl(double linked)
struct dl_node *n = create_node(v);
n->prev = X->prev
X->prev = n;
n->next = X;
if(X != l->head){ //for the first element, to not witch with previous NULL
struct dl_node *prev = n->prev;
prev->next = n}
//or this instead of the previous two lines
(n->prev)->next = n; }
else
__main__
struct dl_node *s=search(l, v:3)
insertBefore(X:s, v:17);
s=search(l, v:3);
delete(&l, s)
print(l)

void delete(struct dl_list 8l, struct node*X){ //general case


X->prev->next = X->next;
X->next->prev = X->prev;
free(X);
l->len--;
}
void delete(struct dl_list 8l, struct node*X){
if(X==l->head && X==l->tail){
l->head = l->tail = NULL; }
else if (X == l->head){
l->head->next->prev = NULL; //make the second.prev look to null
l->head = l->head->next;
free(X); }
else if (X == l->tail){
((l->tail)->prev)->next = NULL;
l->tail = l->tail->prev; }
else {

X->prev->next = X->next;
X->next->prev = X->prev;
free(X);
l->len--;
}

struct Stack{
int *array;
int top;
int limit;
}
struct Stack create_stack(innt limit){
struct Stack s;
s.array = malloc(limit*sizeof(int));
s.top = 0;
}
void push(struct Stack *s, int v){
if(s->top < s->limit){
s->array[s->top] = v;
s->top++; }
else
printf("Limit excedeed");
}
int pop(struct Stack *s){
s->top--;
return s->array[s->top];
}
__main__
struct Stack s = create_stack(limit:3);
push(&s, 2);
printf("%d", pop(&s));

You might also like