Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct element
- {int DATA;
- struct element *nastepny;
- struct element *poprzedni;
- };
- typedef struct element Node;
- void usun(Node** st,Node**kon,int n)
- {
- if(*st==NULL||n<0)
- {
- printf("Pusta lista");
- return;
- }
- if(*st==*kon)
- {
- if(n==0)
- {
- *st=NULL;
- free(*kon);
- *kon=NULL;
- }
- return;
- }
- int i;
- Node* tmp= *st;
- for(i=0;i < n &&tmp!=NULL;i++)
- {
- tmp=tmp->nastepny;
- }
- if(tmp==NULL)
- return;
- if(tmp==*st)
- {
- //Node *tmp=*st;
- *st=(*st)->nastepny;
- (*st)->poprzedni=NULL;
- //free(tmp);
- }
- else if(tmp==*kon)
- {
- //Node *tmp=*kon;
- *kon=(*kon)->poprzedni;
- (*kon)->nastepny=NULL;
- //free(tmp);
- }
- else
- {
- tmp->poprzedni->nastepny=tmp->nastepny;
- tmp->nastepny->poprzedni=tmp->poprzedni;
- //free(tmp);
- }
- free(tmp);
- }
- void dodaj(Node** head,Node **tail,int data)
- {
- Node* nowy= (Node*)malloc(sizeof(Node));
- nowy->nastepny=NULL;
- nowy->poprzedni=NULL;
- nowy->DATA=data;
- if(*head==NULL)
- {
- *head=nowy;
- *tail=nowy;
- return;
- }
- nowy->nastepny=*head;
- (*head)->poprzedni=nowy;
- *head=(*head)->poprzedni;
- }
- void wypisz(Node *tmp)
- {
- while(tmp!=NULL)
- {
- printf("%d " ,tmp->DATA);
- tmp=tmp->nastepny;
- }
- putchar('\n');
- }
- void kasuj(Node** head)
- {
- Node* tmp=*head;
- while(*head!=NULL)
- {
- tmp=*head;
- *head=(*head)->nastepny;
- free(tmp);
- }
- }
- int main()
- {
- Node *head=NULL;
- Node *tail=NULL;
- int i;
- for(i=0;i<10;i++)
- {
- dodaj(&head,&tail,2*i);
- }
- wypisz(head);
- usun(&head,&tail,5);
- wypisz(head);
- kasuj(&head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement