• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Lista jednokierunkowa - jak zmienić funkcję

Object Storage Arubacloud
0 głosów
460 wizyt
pytanie zadane 11 kwietnia 2018 w C i C++ przez kikosiak Obywatel (1,010 p.)
edycja 11 kwietnia 2018 przez kikosiak

Cześć 

Mam pytanie jak zmodyfikować ten program, tak aby do funkcji insert_node() i delete_node() początek listy był przekazywany przez parametr będący podwójnym wskaźnikiem, oraz żeby zwracały one wartość typu bool, sygnalizującą poprawność wykonanej operacji. 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct sll_node
{
    int data ;
    struct sll_node * next ;
};

struct sll_node * create_list ( int data )
{
    struct sll_node * front = ( struct sll_node *)
                              malloc ( sizeof ( struct sll_node ) ) ;
    if ( NULL != front )
    {
        front -> data = data ;
        front -> next = NULL ;
    }
    return front ;
}

struct sll_node * insert_front ( struct sll_node *front,
                                 struct sll_node * new_node )
{
    new_node -> next = front ;
    return new_node ;
}

struct sll_node * find_spot ( struct sll_node *front, int data )
{
    struct sll_node * prev = NULL ;
    while (( NULL != front ) && (front -> data > data ) )
    {
        prev = front ;
        front = front -> next ;
    }
    return prev ;

}

void insert_after ( struct sll_node *node, struct sll_node * new_node )
{
    new_node -> next = node -> next ;
    node -> next = new_node ;
}

void insert_back ( struct sll_node *back, struct sll_node * new_node )
{
    back -> next = new_node ;
}

struct sll_node *insert_node ( struct sll_node *front, int data )
{
    if ( NULL == front )
        return NULL ;

    struct sll_node * new_node = ( struct sll_node *)
                                 malloc ( sizeof ( struct sll_node ) ) ;
    if ( NULL != new_node )
    {
        new_node -> data = data ;
        new_node -> next = NULL ;
        if (front -> data <= data )
            return insert_front (front, new_node ) ;
        else
        {
            struct sll_node * node = find_spot (front, data ) ;
            if ( NULL != node -> next )
                insert_after (node, new_node ) ;
            else
                insert_back (node, new_node ) ;
        }
    }
    return front ;
}

struct sll_node * delete_front ( struct sll_node * front )
{


    struct sll_node * next = front -> next ;
    free ( front ) ;
    return next ;
}

struct sll_node * find_prev_node ( struct sll_node *front, int data )
{
    struct sll_node * prev = NULL ;
    while (( NULL != front ) && (front -> data != data ) )
    {
        prev = front ;
        front = front -> next ;
    }
    return prev ;
}

void delete_after ( struct sll_node * node )
{

    struct sll_node * next = node -> next ;
    if ( NULL != next )
    {
        node -> next = next -> next ;
        free ( next ) ;
    }

}

struct sll_node * delete_node ( struct sll_node *front, int data )
{
    if ( NULL == front )
        return NULL ;

    if (front -> data == data )
        return delete_front ( front ) ;

    struct sll_node * prev = find_prev_node (front, data ) ;
    delete_after ( prev ) ;
    return front ;
}

void print_list ( struct sll_node * front )
{
    for (; NULL != front ; front = front -> next )
        printf ("%d ", front -> data ) ;
    printf ("\n") ;
}

void remove_list ( struct sll_node ** front )
{
    struct sll_node * next = NULL ;
    while ( NULL != * front )
    {
        next = (* front ) ->next ;
        free (* front ) ;
        * front = next ;
    }
}

int main ()
{
    struct sll_node * front = create_list (1) ;
    int i;

    for (i=2; i <5; i++)
        front = insert_node (front, i) ;
    for (i=6; i <10; i++)
        front = insert_node (front, i) ;
    printf (" List elements :\n") ;
    print_list ( front ) ;

    front = insert_node (front, 0) ;
    printf (" List elements after insertion of 0:\n") ;
    print_list ( front ) ;
    front = insert_node (front, 5) ;
    printf (" List elements after insertion of 5:\n") ;
    print_list ( front ) ;

    front = insert_node (front, 7) ;
    printf (" List elements after insertion of 7:\n") ;
    print_list ( front ) ;
    front = insert_node (front, 10) ;
    printf (" List elements after insertion of 10:\n") ;
    print_list ( front ) ;

    front = delete_node (front, 0) ;
    printf (" List elements after deletion of 0:\n") ;
    print_list ( front ) ;
    front = delete_node (front, 1) ;
    printf (" List elements after deletion of 1:\n") ;
    print_list ( front ) ;
    front = delete_node (front, 1) ;
    printf (" List elements after deletion of 1:\n") ;
    print_list ( front ) ;
    front = delete_node (front, 5) ;
    printf (" List elements after deletion of 5:\n") ;
    print_list ( front ) ;
    front = delete_node (front, 10) ;
    printf (" List elements after deletion of 10: \n") ;
    print_list ( front ) ;

    remove_list (& front ) ;
    return 0;
}


 

komentarz 11 kwietnia 2018 przez Hiskiel Pasjonat (22,830 p.)
Proszę, na sam początek sformatuj jakiś ładnie kod. Chodzi o wcięcia. To nie jest ksiażka która się tak o czyta.
komentarz 11 kwietnia 2018 przez kikosiak Obywatel (1,010 p.)
Proszę
komentarz 12 kwietnia 2018 przez RafalS VIP (122,820 p.)
Kod jest dosyć dobrze napisany. Ciężko uwierzyć, że ktoś kto napisał ten kod nie potrafi zrobić w nim takiej wręcz kosmetycznej zmiany :P
komentarz 12 kwietnia 2018 przez kikosiak Obywatel (1,010 p.)
Nie do końca jest tak, że ja napisałem ten kod :P. Po prostu jestem ciekawy jak to zrobić

1 odpowiedź

0 głosów
odpowiedź 12 kwietnia 2018 przez RafalS VIP (122,820 p.)
wybrane 12 kwietnia 2018 przez kikosiak
 
Najlepsza

Pisane na szybko, bo to kosmetyczna podmiana, wynik się zgadza, nie zagłębiałem się ;P
 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct sll_node
{
	int data;
	struct sll_node * next;
};

struct sll_node * create_list(int data)
{
	struct sll_node * front = (struct sll_node *)
		malloc(sizeof(struct sll_node));
	if (NULL != front)
	{
		front->data = data;
		front->next = NULL;
	}
	return front;
}

struct sll_node * insert_front(struct sll_node *front,
	struct sll_node * new_node)
{
	new_node->next = front;
	return new_node;
}

struct sll_node * find_spot(struct sll_node *front, int data)
{
	struct sll_node * prev = NULL;
	while ((NULL != front) && (front->data > data))
	{
		prev = front;
		front = front->next;
	}
	return prev;

}

void insert_after(struct sll_node *node, struct sll_node * new_node)
{
	new_node->next = node->next;
	node->next = new_node;
}

void insert_back(struct sll_node *back, struct sll_node * new_node)
{
	back->next = new_node;
}

bool insert_node(struct sll_node **front, int data)
{
	if (NULL == front)
		return false;

	struct sll_node * new_node = (struct sll_node *)
		malloc(sizeof(struct sll_node));
	if (NULL != new_node)
	{
		new_node->data = data;
		new_node->next = NULL;
		if ((*front)->data <= data)
			*front = insert_front(*front, new_node);
		else
		{
			struct sll_node * node = find_spot(*front, data);
			if (NULL != node->next)
				insert_after(node, new_node);
			else
				insert_back(node, new_node);
		}
		return true;
	}
	else 
		return false;
}

struct sll_node * delete_front(struct sll_node * front)
{
	struct sll_node * next = front->next;
	free(front);
	return next;
}

struct sll_node * find_prev_node(struct sll_node *front, int data)
{
	struct sll_node * prev = NULL;
	while ((NULL != front) && (front->data != data))
	{
		prev = front;
		front = front->next;
	}
	return prev;
}

void delete_after(struct sll_node * node)
{

	struct sll_node * next = node->next;
	if (NULL != next)
	{
		node->next = next->next;
		free(next);
	}

}

bool delete_node(struct sll_node **front, int data)
{
	if (NULL == front)
		return false;

	if ((*front)->data == data) 
	{
		*front = delete_front(*front);
		return true;
	}
	struct sll_node * prev = find_prev_node(*front, data);
	delete_after(prev);
	return true;
}

void print_list(struct sll_node * front)
{
	for (; NULL != front; front = front->next)
		printf("%d ", front->data);
	printf("\n");
}

void remove_list(struct sll_node ** front)
{
	struct sll_node * next = NULL;
	while (NULL != *front)
	{
		next = (*front)->next;
		free(*front);
		*front = next;
	}
}

int main()
{
	struct sll_node * front = create_list(1);
	int i;

	for (i = 2; i <5; i++)
		insert_node(&front, i);
	for (i = 6; i <10; i++)
		insert_node(&front, i);
	printf(" List elements :\n");
	print_list(front);

	insert_node(&front, 0);
	printf(" List elements after insertion of 0:\n");
	print_list(front);
	insert_node(&front, 5);
	printf(" List elements after insertion of 5:\n");
	print_list(front);

	insert_node(&front, 7);
	printf(" List elements after insertion of 7:\n");
	print_list(front);
	insert_node(&front, 10);
	printf(" List elements after insertion of 10:\n");
	print_list(front);

	delete_node(&front, 0);
	printf(" List elements after deletion of 0:\n");
	print_list(front);
	delete_node(&front, 1);
	printf(" List elements after deletion of 1:\n");
	print_list(front);
	delete_node(&front, 1);
	printf(" List elements after deletion of 1:\n");
	print_list(front);
	delete_node(&front, 5);
	printf(" List elements after deletion of 5:\n");
	print_list(front);
	delete_node(&front, 10);
	printf(" List elements after deletion of 10: \n");
	print_list(front);

	remove_list(&front);
	return 0;
}

 

komentarz 12 kwietnia 2018 przez kikosiak Obywatel (1,010 p.)
Dzięki :)

Podobne pytania

0 głosów
0 odpowiedzi 267 wizyt
pytanie zadane 23 grudnia 2018 w C i C++ przez Prospector Nowicjusz (120 p.)
0 głosów
1 odpowiedź 195 wizyt
0 głosów
1 odpowiedź 1,793 wizyt
pytanie zadane 15 kwietnia 2017 w C i C++ przez Lemon Nowicjusz (210 p.)

92,570 zapytań

141,422 odpowiedzi

319,643 komentarzy

61,958 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...