Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- struct element{
- struct element* previous;
- struct element* next;
- int number;
- } *first = NULL;
- void print(struct element* input){
- if(input != NULL){
- printf("%d ", input->number);
- if(input->next != NULL){
- print(input->next);
- }
- else {
- printf("\n");
- }
- }
- else {
- printf("LISTA JEST PUSTA! \n");
- }
- }
- void insert(int inputValue, struct element* inputElement, struct element* previous){
- struct element* newfirst = (struct element *)malloc(sizeof(struct element));
- if(inputElement == NULL){
- // pusta lista lub koniec
- struct element* tmp = (struct element *)malloc(sizeof(struct element));
- tmp->previous = previous;
- tmp->next = NULL;
- tmp->number = inputValue;
- if(inputElement == first){
- first = tmp;
- }
- else {
- tmp->previous->next = tmp;
- }
- }
- else {
- if(inputElement->number >= inputValue){
- /* Znaleziony wiekszy - wstawiamy przed nim */
- struct element* tmp = (struct element *)malloc(sizeof(struct element));
- tmp->previous = previous;
- tmp->next = inputElement;
- tmp->number = inputValue;
- if(tmp->previous != NULL){
- tmp->previous->next = tmp;
- }
- else {
- first = tmp;
- }
- tmp->next->previous = tmp;
- }
- else {
- /* Pudlo - szukamy dalej */
- insert(inputValue, inputElement->next, inputElement);
- }
- }
- }
- void usun(int inputValue){
- if(first == NULL){
- return;
- }
- struct element* tmp = first;
- while(tmp->number != inputValue){
- if(tmp->next == NULL){
- return;
- }
- tmp = tmp->next;
- }
- if(tmp->number == inputValue){
- /* trafienie */
- if(tmp == first){
- if(tmp->next == NULL){
- first = NULL;
- }
- else {
- first = tmp->next;
- }
- }
- else {
- if(tmp->next != NULL){
- tmp->next->previous = tmp->previous;
- }
- if(tmp->previous != NULL){
- tmp->previous->next = tmp->next;
- }
- }
- free(tmp);
- }
- }
- int main(){
- int option;
- int value;
- int run = 1;
- while(run){
- printf("Wybierz opcje: 1 - insert, 2 - delete, 3 - wyswietl");
- scanf("%d", &option);
- if(option == 1){
- printf("Podaj wartosc do wstawienia: ");
- scanf("%d", &value);
- insert(value, first, NULL);
- }
- else if(option == 2){
- printf("Podaj wartosc do usuniecia: ");
- scanf("%d", &value);
- usun(value);
- }
- else if(option == 3){
- printf("Zawartosc listy ");
- print(first);
- }
- else {
- run = 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment