Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int value;
- struct node *prev;
- struct node *next;
- }node;
- node *head=NULL;
- node *last=NULL;
- node *current=NULL;
- int empty(){
- if(head==NULL)return 1;
- return 0;
- }
- int size(){
- int licznik=0;
- current=head;
- while(current!=NULL){
- licznik++;
- current=current->next;
- }
- return licznik;
- }
- void clear(){ //POPRAWIC NA GLOBALNA!!!!!!!!
- while(head!=NULL){
- node *tmp=head;
- head=head->next;
- free(tmp);
- }
- last=head;
- }
- void push_front(int t)
- {
- node *tmp=NULL;
- tmp=malloc(sizeof(node));
- if(head==NULL && last == NULL)
- {
- head=tmp;
- last=tmp;
- tmp->value=t;
- tmp->next=NULL;
- tmp->prev=NULL;
- }
- else {
- tmp->value = t;
- tmp->next = head;
- tmp->prev = NULL;
- head->prev = tmp;
- head = tmp;
- }
- }
- void push_back(int t)// nie dziala
- {
- node *tmp=NULL;
- tmp=malloc(sizeof(node));
- tmp->value=t;
- if(head==NULL && last == NULL)
- {
- head=tmp;
- last=tmp;
- tmp->next=NULL;
- tmp->prev=NULL;
- }
- else {
- tmp->next = NULL;
- tmp->prev = last;
- last->next = tmp;
- last = tmp;
- }
- }
- void remove1(int t)
- {
- node *guardian=malloc(sizeof(node));
- guardian->next=head;
- node *p=guardian;
- node *q=head;
- while(q!=NULL)
- {
- if(q->value==t)
- {
- p->next=q->next;
- q->next->prev=p;
- free(q);
- q=p->next;
- }
- else{
- p=q;
- q=q->next;
- }
- }
- head=guardian->next;
- p=guardian;
- while(p->next!=NULL)p=p->next;
- if(p==guardian)last=NULL;
- else last=p;
- }
- int front(){
- if(head==NULL)return -1;
- return head->value;
- }
- int back()
- {
- if(last==NULL)return -1;
- return last->value;
- }
- void pop_front()
- {
- node *tmp=head;
- if(head==NULL) return;
- head=head->next;
- free(tmp);
- }
- void pop_back()
- {
- node *tmp=last;
- if(last==NULL)return;
- last=last->prev;
- free(tmp);
- }
- void print()
- {
- current=head;
- while(current!=NULL)
- {
- printf("%d ",current->value);
- current->next;
- }
- }
- int main() {
- printf("Hello, World!\n");
- for(int i=1;i<5;i++)
- {
- push_front(i);
- }
- push_back(6);
- remove1(4);
- while(head!=NULL)
- {
- printf("%d ",head->value);
- head=head->next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement