Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct osoba
- {
- int wiek;
- struct osoba *next;
- };
- struct osoba *head;
- struct osoba *last;
- struct osoba *pom;
- void create();
- void insertFirst(x);
- void insertLast(x);
- void insertAfter(key,x);
- void display();
- int main()
- {
- int choice,key,x;
- while(choice!=0)
- {
- printf("\n------------------------------------------\n");
- printf("1.Tworzenie listy:\n");
- printf("2.Dodawanie elementu na poczatek listy:\n");
- printf("3.Dodawanie elementu na koniec listy: \n");
- printf("4.Dodawanie elementu po zadanym listy:\n");
- printf("5.Wyswietlanie elementow listy:\n");
- printf("6.Usuwanie elementu wybranego listy:\n");
- printf("7.Wyjscie z programu: 0\n");
- printf("------------------------------------------\n");
- printf("Dokonaj wyboru: ");
- scanf("%d",&choice);
- if(choice==0)
- {
- break;
- }
- else if(choice==1)
- {
- create();
- system("cls");
- }
- else if(choice==2)
- {
- system("cls");
- printf("dodajesz na poczatek listy\n");
- printf("\n---------------------------\n");
- printf("Podaj wiek: \n");
- scanf(" %d",&x);
- insertFirst(x);
- }
- else if(choice==3)
- {
- system("cls");
- printf("Dodajesz na koniec listy\n");
- printf("---------------------------\n");
- printf("Podaj wiek: \n");
- scanf(" %d",&x);
- insertLast(x);
- }
- else if(choice==4)
- {
- system("cls");
- printf("Dodajesz element po zadanym\n");
- printf("---------------------------\n");
- printf("\nPo ktorym chcesz dodac:\n");
- scanf("%d",&key);
- printf("Podaj wiek: \n");
- scanf("%d",&x);
- insertFirst(key,x);
- }
- else if(choice==5)
- {
- system("cls");
- printf("Wyswietlasz elementy listy\n");
- printf("---------------------------\n");
- display();
- }
- }
- return 0;
- }
- void display()
- {
- int nr_elementu=0;
- struct osoba* pom2;
- pom=head;
- while(pom2->next!=NULL)
- {
- printf("el.listy.nr:%d posiada wartosc wieku: %d\n",++nr_elementu,pom->wiek);
- pom2=pom->next;
- pom=pom2;
- }
- printf("el.listy.nr:%d posiada wartosc wieku: %d\n",++nr_elementu,pom->wiek);
- }
- void create()
- {
- head=NULL;
- last=NULL;
- }
- void insertFirst(int x)
- {
- pom=(struct osoba*)malloc(sizeof(struct osoba));
- pom->wiek=x;
- if(head==NULL)
- {
- head=pom;
- last=pom;
- head->next=NULL;
- last->next=NULL;
- }
- else
- {
- pom->next=head;
- head=pom;
- }
- }
- void insertLast(int x)
- {
- pom=(struct osoba*)malloc(sizeof(struct osoba));
- if(head==NULL)
- {
- pom->next=NULL;
- last=pom;
- head=pom;
- head->wiek=x;
- }
- else
- {
- last->next=pom;
- pom->next=NULL;
- last=pom;
- last->wiek=x;
- }
- }
- void insertAfter(int key, int x)
- {
- struct osoba *wstaw = (struct osoba*)malloc(sizeof(struct osoba));
- struct osoba *pom2;
- pom=head;
- pom2=head;
- if(head==NULL)
- {printf("Nie mozna wykonac operacji");
- exit(1);}
- for(int i=0; i<key; i++)
- {
- pom2=pom->next;
- pom=pom2;
- }
- pom->next=wstaw;
- wstaw->next=pom;
- pom=wstaw;
- pom->wiek=x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement