Usuwanie elementów listy dwukierunkowej

0

Przerabiam sobie właśnie listy dwukierunkowe. Język C. Mam takie pytanie - czy funkcje w jakikolwiek sposób modyfikujące listę zawsze muszą zwracać wskaźnik do elementu listy? Gdybyśmy mieli na przykład listę złożoną z jednego elementu i zadanie usunięcia ogona listy to trzeba zwrócić wskaźnik

NULL

i przypisać go do wskaźnika głowy listy? Wiem, że np. wyświetlanie elementów listy nie musi niczego zwracać, ale to żadna nowość. A jak ma się to z funkcjami modyfikującymi listę?

0

Jeżeli usuniesz wszystko z listy i nie ustawisz głowy na NULLa to znaczy, że wskaźnik na coś będzie pokazywał. Jak chcesz później dodawać do takiej listy? Np gdy dodasz na koniec to dodasz za element którego tak naprawdę nie chcesz tam mieć.

0
Node *modify1(Node *first);
void modify2(Node **first);
typedef struct _list { Node *first; } List,*ListPtr;
void modify3(ListPtr L);
0

Ćwiczę listy na takim kawałku kodu. Dodawanie elementów jest zapewne zrealizowane słabo, ale na razie sam to pisałem, bez patrzenia na cudzy kod. Działa usuwanie głowy, ale ogonu już nie. Domyślam się, że ma to związek ze zwracaniem zmiennej automatycznej mającej zasięg lokalny. Jak można inaczej napisać takie funkcje?

#include <stdio.h>
#include <stdlib.h>

struct ts
{
    int key;
    struct ts *prev, *next;
};

struct ts * push(struct ts * head, struct ts * x)
{
	struct ts * temp = head;
	if(head)
	{
		while(head->next != NULL)
		{
			head = head->next;
		}
		head->next = x;
		x->prev = head;
	}
	else
	{
		return x;
	}
	return temp;
}

void wyswietl(struct ts * head)
{
	while(head)
	{
		printf("%d\n", head->key);
		head = head->next;
	}
}

struct ts * usun_glowe(struct ts * head)
{
	struct ts * temp = head;
	if(head)
	{
		if(head->next != NULL)
		{
			head->next->prev = NULL;
			free(head);
			return temp->next;
		}
		else
		{
			free(head);
			return NULL;
		}
	}
	else
		return NULL;
}

struct ts * usun_ogon(struct ts * head)
{
	struct ts * temp = head;
	if(head)
	{
		if(head->prev == NULL)
		{
			free(head);
			return NULL;
		}
		else
		{
			head->prev->next = NULL;
			free(head);
			return temp;
		}
	}
	else
		return NULL;
}

int main(void)
{
    struct ts *head = NULL, *x = NULL;
    int i, n;
    scanf("%d", &n);
    for(i=1; i<=n; i++)
    {
    	int a;
        x = (struct ts *) malloc(sizeof(struct ts));
        printf("Podaj wartosc dla wartosci %d \n", i);
        scanf("%d", &a);
        x->key = a;
        x->prev = x->next = NULL;
        head = push(head, x);
    }
    printf("Wyswietlenie listy:\n");
    wyswietl(head);
    
    head = usun_glowe(head);
    printf("Wyswietlenie listy po usuniecu glowy:\n");
    wyswietl(head);
    head = usun_ogon(head);
    printf("Wyswietlenie listy po usuniecu ogona:\n");
    wyswietl(head);
    
    return 0;
}
0

To wywal to i ćwicz na normalnym kawałku kodu:

struct ts
  {
   int key;
   struct ts *prev, *next;
  };

struct list
  {
   struct ts *head,*tail;
  };

struct ts *allocts(int key,struct ts *prev,struct ts *next);
void push_back(struct list *lst,int key);
void push_front(struct list *lst,int key);
void show(const struct list *lst);
int pop_back(struct list *lst);
int pop_front(struct list *lst);

1 użytkowników online, w tym zalogowanych: 0, gości: 1