Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct node
- {
- int dane;
- struct node *next;
- };
- void wypisywanie(struct node *n)
- {
- if(n == NULL){
- puts("Lista jest pusta");
- return;
- }
- while(n != NULL){
- printf("%d ", n->dane);
- n = n->next;
- }
- }
- void dodawanie_poczatek(struct node **n, int liczba)
- {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- temp->dane = liczba;
- temp->next = *n;
- *n = temp;
- }
- void dodawanie_koniec(struct node **n, int liczba)
- {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- struct node *last = *n;
- temp->dane = liczba;
- temp->next = NULL;
- if(*n == NULL){
- *n = temp;
- return;
- }
- while(last->next != NULL){
- last = last->next;
- }
- last->next = temp;
- }
- void usuwanie_poczatek(struct node **n)
- {
- struct node *temp = *n;
- if(*n == NULL){
- puts("Lista jest pusta");
- return;
- }
- *n = (*n)->next;
- free(temp);
- }
- void usuwanie_koniec(struct node **n)
- {
- struct node *ostatni, *przedostatni;
- if(*n == NULL){
- puts("Lista jest pusta");
- return;
- }
- ostatni = *n;
- przedostatni = *n;
- while(ostatni->next != NULL){
- przedostatni = ostatni;
- ostatni = ostatni->next;
- }
- if(ostatni == *n){
- *n = NULL;
- return;
- }
- przedostatni->next = NULL;
- free(ostatni);
- }
- int main()
- {
- struct node* pierwszy = NULL;
- dodawanie_poczatek(&pierwszy, 4);
- dodawanie_koniec(&pierwszy, 00);
- dodawanie_koniec(&pierwszy, 11);
- dodawanie_poczatek(&pierwszy, 6);
- dodawanie_poczatek(&pierwszy, 1);
- usuwanie_poczatek(&pierwszy);
- usuwanie_koniec(&pierwszy);
- usuwanie_koniec(&pierwszy);
- wypisywanie(pierwszy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement