Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1 way list.cpp: Określa punkt wejścia dla aplikacji konsoli.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct lista
- {
- int id;
- struct lista *nast;
- }list;
- // w mainie head=NULL; !!
- struct lista *head;
- struct lista *temp;
- struct lista *temp1;
- struct lista *temp2;
- struct lista *temp3;
- struct lista *temp4;
- int ilosc = 0;
- void stworz(int x)
- {
- temp = (list*)malloc(sizeof(list));
- temp ->nast = NULL;
- temp->id = x;
- ilosc++;
- }
- void wczytwaniePrzod(int x)
- {
- if (head == NULL)
- {
- stworz(x);
- head = temp;
- temp1 = head;
- }
- else
- {
- stworz(x);
- temp->nast = head;
- head = temp;
- }
- }
- void wczytanieTyl(int x)
- {
- if (head == NULL)
- {
- stworz(x);
- head = temp;
- temp1 = head;
- }
- else
- {
- stworz(x);
- {
- temp1->nast = temp;
- temp1 = temp;
- }
- }
- }
- void wypistyl()
- {
- list *temp = head;
- while (temp != NULL)
- {
- printf("%d ->", temp->id);
- temp = temp->nast;
- }
- printf("null\n");
- }
- dodaj(int x,int z)
- {
- int i = 1;
- temp2 = head;
- if (z<1 || z>ilosc + 1)
- {
- printf("wykraczamy po za zakres ");
- return;
- }
- if (head == NULL && z == 1)
- {
- stworz(x);
- head = temp;
- temp1 = head;
- }
- else
- {
- while (i < z)
- {
- temp3 = temp2;
- temp2 = temp2->nast;
- i++;
- }
- stworz(x);
- temp3->nast = temp;
- temp3 = temp;
- temp3->nast = temp2;
- }
- }
- void sort()
- {
- int i, j, x;
- temp2 = head;
- temp4 = head;
- if (temp2 == NULL)
- {
- printf("\n List empty to sort");
- return;
- }
- for (temp2 = head; temp2 != NULL; temp2 = temp2->nast)
- {
- for (temp4 = temp2->nast; temp4 != NULL; temp4 = temp4->nast)
- {
- if (temp2->id > temp4->id) // malejąco znak w drugą strone
- {
- x = temp2->id;
- temp2->id = temp4->id;
- temp4->id = x;
- }
- }
- }
- wypistyl();
- }
- void wymiana()
- {
- int data, data1;
- printf("\n Wpisz liczbe ktora chcesz wymienic : ");
- scanf("%d", &data);
- printf("\n Wpisz nowa liczbe : ");
- scanf("%d", &data1);
- temp2 = head;
- if (temp2 == NULL)
- {
- printf("\n Error : Lista jest pusta");
- return;
- }
- while (temp2 != NULL)
- {
- if (temp2->id == data)
- {
- temp2->id = data1;
- wypistyl();
- return;
- }
- else
- temp2 = temp2->nast;
- }
- printf("\n Error : %d nie znaleziono liczby do wymiany", data);
- }
- void szukaj()
- {
- int data, ilosc = 0;
- temp2 = head;
- if (temp2 == NULL)
- {
- printf("\n Error : List empty to search for data");
- return;
- }
- printf("\n Wpisz liczbe do wyszukania : ");
- scanf("%d", &data);
- while (temp2 != NULL)
- {
- if (temp2->id == data)
- {
- printf("\n liczba znajduje sie na pozycji %d \n", ilosc + 1);
- }
- temp2 = temp2->nast;
- ilosc++;
- }
- if(temp2==NULL)
- printf("\n Error : %d nie znaleziono liczby na liscie", data);
- }
- int main()
- {
- head =NULL;
- wczytwaniePrzod(10);
- wczytwaniePrzod(20);
- wczytwaniePrzod(30);
- wczytanieTyl(40);
- dodaj(50,3);
- wypistyl();
- sort();
- wymiana();
- szukaj();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement