Sito Erastotenesa

0

Witam serdecznie. Mam delikatny problem z programem wyszukującym liczby pierwsze z zadanego przedziału [2,n], mianowicie program po wypisaniu wszystkich liczb pierwszych(działa poprawnie) "wykrzacza się". Z góry dziękuję za każdą pomoc, a także proszę o porady dotyczące poprawę jakości mojego kodu. Jestem jeszcze początkującym programistą, także za każdą radę, która pomoże podnieść mi moje umiejętności oraz poszerzyć wiedzę serdecznie dziękuję. Pozdrawiam

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 struct lista
{
  int x; 
  struct lista* next; /* Wskaznik na nastepny element */
  struct lista* prev; /* Wskaznik na poprzedni element */
};

typedef struct lista lista;
lista *head= NULL , *tail = NULL;

void dodaj (int x)
{
	lista *nowy = NULL;
	if (head == NULL)
	{
		head = tail = malloc(sizeof(lista));
		head -> x = x;
		head -> next = NULL;
		head -> prev = NULL;
				printf("%d\n", head->x);
		return;
	}
	nowy = malloc(sizeof(lista));
	nowy -> x= x;
	nowy -> next = NULL;
	nowy -> prev = tail;
	tail -> next = nowy;
	tail = nowy;
			printf("%d\n", nowy->x);
}
void usun(lista *trash)
{
	lista *tmp;
	if(head==NULL)
	{
		printf("Lista jest pusta");
		return;
	}
    if(trash == head)
	{
		head ->next->prev = NULL;
		head = head -> next;
		free(trash); /* Zwalniam pamiec zaalokowana przez malloc'a */
		return;
	}
    if(trash == tail)
	{
		tail ->prev->next = NULL;
		tail = tail->prev;
		free(trash); /* Zwalniam pamiec zaalokowana przez malloc'a */
		return;
	}
	trash -> prev -> next = trash -> next;
	trash -> next -> prev = trash -> prev;
	free(trash);	
}
void wyswietl()
{
		lista *tmp = head;
	if(head == NULL)
	{
		printf("Lista jest pusta");
		return;
	}
	else 
	{
	
	while (tmp != NULL);
	{
		printf("%d\n", tmp->x);
		tmp = tmp -> next;
	}
}
}
void erato(int n)
{
	lista *tmp = head, *tmp2 = head, *trash = NULL;
	while(tmp ->x <n)
	{
		printf("%d\n", tmp->x);
		tmp2 = tmp -> next;
		while(tmp2 !=NULL)
		{
			if((tmp2 -> x % tmp -> x) == 0)
			{
				trash = tmp2;
				tmp2 = tmp2 -> next;
				usun(trash);
				continue;
			}
			tmp2 = tmp2 -> next;
		}
		tmp = tmp -> next;
		system("Pause");
	}
}
int main()
{
	
	int i;
	int n;
	wyswietl();
	printf("\n\nPodaj Liczbe : ");
	scanf("%d", &n);
	for(i=2; i<n; i++)
	{
		dodaj(i);
	}
	system("Pause");
	printf("\n Liczby pierwsze z zadanego przedzialu to:\n");
	erato(n);
	system("Pause");
	return 0;
}

0
while(tmp ->x <n)

Przemyśl tą linijkę, jest prostszy sposób na wypisanie całej listy. I popraw formatowanie :P

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