Προς το περιεχόμενο

Linked LIST in C


MauriDalia

Προτεινόμενες αναρτήσεις

Το παρακάτω πρόγραμμα δίνει λύση σχεδόν σε όλα όσα χρειάζεσαι για τις λίστες, δημιουργεί Ν λίστες αν θέλεις, εγώ απλά έβαλα μία (1), και προσθέτει κόμβους με βάση τις δομές σου στην-στις λιστα-ες που έχεις. Έπίσης τυπώνεται η λίστα για του λόγου το αληθές, κάνε copy - paste τον κώδικα στον compiler που χρησιμοποιείς και τρέξε το πρόγραμμα. Έχω αλλάξει λίγο τις δομές σου προτείνω να ακολουθήσεις το δικό μου styl γιατί τα έχεις ξεσκίσει τα typedef και οι δείκτες είναι καλό να φαίνονται για να βλέπεις τι κάνεις malloc. Tώρα το τι θα κάνεις στην main() ή το τι θέλεις να φτιάξεις δεν το ξέρω, αργότερα θα σου κάνω και τις συναρτήσεις που αφαιρούν ένα στοιχείο από την λίστα και την διαγράφουν, τώρα φεύγω πάω να δω τον Ολυμπιακό.

 

Κώδικας:

 

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

#define LOGIN_SIZE 15
#define BODY_SIZE 250

/* Structure to hold our List node. */
typedef struct _user{
char login[LOGIN_SIZE];
}User;

typedef struct mail{
User sender;
User reciever;
char body[bODY_SIZE];
struct mail *next;

}MAIL;

/* List. */

typedef struct _MailList{
MAIL *head;
MAIL *tail;
int size;
}MailList;

MailList *CreateMailList(int HowMany)
{
MailList *list = malloc(HowMany *sizeof(MailList));
if(!list)
{
	fprintf(stderr,"ErrorReporter:Error Memory Fault.\n");
	return NULL;
}
else
{
	/* Initialise List. */
	int i;
	for(i = 0; i < HowMany; i++)
	{
		list[i].head = NULL;
		list[i].tail = NULL;
		list[i].size = 0;
	/* Return List. */
	}
	if(i == HowMany)
		return list;
}
}
/* Insert a new mail after the element mail. */
int Insert_After_Element(MailList *list, MAIL *element)
{
if(!list)
{
	fprintf(stderr,"ErrorReporter:Error List NULL.\n");
	return -1;
}
else
{
	/* Create a new Node. */
	MAIL *newMail = malloc(sizeof(MAIL));
	if(!newMail)
	{
		fprintf(stderr,"ErrorReporter:Error Memory Fault.\n");
		return -2;

	}
	else
	{
		/* Init new Mail. */
		printf("Enter the New Mail's Data:\n");
		printf("Enter Sender Login:");
		fgets(newMail->sender.login, sizeof(newMail->sender.login), stdin);
		printf("Enter Reciever Login:");
		fgets(newMail->reciever.login, sizeof(newMail->reciever.login), stdin);
		printf("Enter Body Message:");
		fgets(newMail->body, sizeof(newMail->body), stdin);
		printf("\n");
		/* Point nowhere initially. */
		newMail->next = NULL;
		/* Should trim if you want from all the '\n' character of fgets. */
		if(!element)
		{
			if(list->size == 0)
			{
				list->tail = newMail;
				list->head = newMail;
				newMail->next = NULL;
			}
			else
			{
				newMail->next = list->head;
				list->head = newMail;
			}
		}
		else
		{
			if(element->next == NULL)
			{
				list->tail = newMail;
				newMail->next = NULL;
			}
			/* Otherwhise. */
			newMail->next = element->next;
			element->next = newMail;
		}
		/* Advance list size. */
		list->size++;
		/* Return with zero. */
		return 0;
	}
}
}

void DumpMailList(MailList *list)
{
if(list)
{
	MAIL *mailptr = NULL;
	printf("List Print:\n\n");
	for(mailptr = list->head; mailptr != NULL; mailptr = mailptr->next)
	{
		printf("SenderLogin:%sRecieverLogin:%sBodyText:%s\n", mailptr->sender.login, mailptr->reciever.login, mailptr->body);
	}
}
}

#pragma argsused
int main(int argc, char* argv[])
{
MailList *theList = NULL;
/* Create A single List. */
theList = CreateMailList(1);
/* Insert in head always. */
Insert_After_Element(theList, NULL);
Insert_After_Element(theList, NULL);
Insert_After_Element(theList, NULL);
DumpMailList(theList);
printf("Hit enter to continue....");
getchar();
return 0;
}

 

Εκτυπωμένα Αποτελέσματα:

 

Enter the New Mail's Data:

Enter Sender Login:Kostas

Enter Reciever Login:Nikos

Enter Body Message:Ela re ti kaneis pws paei to pr

 

Enter the New Mail's Data:

Enter Sender Login:Ilias

Enter Reciever Login:Antwnis

Enter Body Message:Kamia gomena paizei?

 

Enter the New Mail's Data:

Enter Sender Login:Katerina

Enter Reciever Login:Maria

Enter Body Message:Pote gia kafe kai ti wra?

 

List Print:

 

SenderLogin:Katerina

RecieverLogin:Maria

BodyText:Pote gia kafe kai ti wra?

 

SenderLogin:Ilias

RecieverLogin:Antwnis

BodyText:Kamia gomena paizei?

 

SenderLogin:Kostas

RecieverLogin:Nikos

BodyText:Ela re ti kaneis pws paei to programma?

 

Hit enter to continue....

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Τελική μορφή και με την συνάρτηση που αφαιρεί κόμβο από την λίστα σου, καλή συνέχεια.

 

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

#define LOGIN_SIZE 15
#define BODY_SIZE 250

/* Structure to hold our List node. */
typedef struct _user{
char login[LOGIN_SIZE];
}User;

typedef struct mail{
User sender;
User reciever;
char body[bODY_SIZE];
struct mail *next;

}MAIL;

/* List. */

typedef struct _MailList{
MAIL *head;
MAIL *tail;
int size;
}MailList;

MailList *CreateMailList(int HowMany)
{
MailList *list = malloc(HowMany *sizeof(MailList));
if(!list)
{
	fprintf(stderr,"ErrorReporter:Error Memory Fault.\n");
	return NULL;
}
else
{
	/* Initialise List. */
	int i;
	for(i = 0; i < HowMany; i++)
	{
		list[i].head = NULL;
		list[i].tail = NULL;
		list[i].size = 0;
	/* Return List. */
	}
	if(i == HowMany)
		return list;
}
}
/* Insert a new mail after the element mail. */
int Insert_After_Element(MailList *list, MAIL *element)
{
if(!list)
{
	fprintf(stderr,"ErrorReporter:Error List NULL.\n");
	return -1;
}
else
{
	/* Create a new Node. */
	MAIL *newMail = malloc(sizeof(MAIL));
	if(!newMail)
	{
		fprintf(stderr,"ErrorReporter:Error Memory Fault.\n");
		return -2;

	}
	else
	{
		/* Init new Mail. */
		printf("Enter the New Mail's Data:\n");
		printf("Enter Sender Login:");
		fgets(newMail->sender.login, sizeof(newMail->sender.login), stdin);
		printf("Enter Reciever Login:");
		fgets(newMail->reciever.login, sizeof(newMail->reciever.login), stdin);
		printf("Enter Body Message:");
		fgets(newMail->body, sizeof(newMail->body), stdin);
		printf("\n");
		/* Point nowhere initially. */
		newMail->next = NULL;
		/* Should trim if you want from all the '\n' character of fgets. */
		if(!element)
		{
			if(list->size == 0)
			{
				list->tail = newMail;
				list->head = newMail;
				newMail->next = NULL;
			}
			else
			{
				newMail->next = list->head;
				list->head = newMail;
			}
		}
		else
		{
			if(element->next == NULL)
			{
				list->tail = newMail;
				newMail->next = NULL;
			}
			/* Otherwhise. */
			newMail->next = element->next;
			element->next = newMail;
		}
		/* Advance list size. */
		list->size++;
		/* Return with zero. */
		return 0;
	}
}
}

void DumpMailList(MailList *list)
{
if(list)
{
	MAIL *mailptr = NULL;
	printf("List Print:\n\n");
	for(mailptr = list->head; mailptr != NULL; mailptr = mailptr->next)
	{
		printf("SenderLogin:%sRecieverLogin:%sBodyText:%s\n", mailptr->sender.login, mailptr->reciever.login, mailptr->body);
	}
}
}

/* Remove A mail from List. */

int Remove_Mail(MailList *list, MAIL *element, MAIL **data)
{
if(!list)
{
	fprintf(stderr,"ErrorReporter:Error List NULL.\n");
	return -1;
}
else
{
	/* Pointer to the item to be Deleted from the list. */
	MAIL *mailtobeDeleted = NULL;
	if(list->size == 0)
	{
		printf("ErrorReporter:Error Empty List.\n");
		return -2;
	}
	else
	{

           /* Create a ptr to the item to be removed so to restore it. */
		   MAIL *ptr = malloc(sizeof(MAIL));

		if(element == NULL)
		{
		   /* Copy Struct. */
		   memcpy(ptr, list->head, sizeof(MAIL));
		   /* Return it. */
		   *data = ptr;
		   mailtobeDeleted = list->head;
		   list->head = list->head->next;
		   if(list->size == 1)
				list->tail = NULL;
		}
		else
		{
			if(element->next == NULL)
				return -3;
			/* Case not null. */
			memcpy(ptr, element->next, sizeof(MAIL));
			*data = ptr;
			mailtobeDeleted = element->next;
			/* Go to the next one. */
			element->next = element->next->next;
			if(element->next == NULL)
				list->tail = element;
		}
		/* Free the mail. */
		free(mailtobeDeleted);
		/* Minus the size list. */
		list->size--;
		/*Return with zero. */
		return 0;
	}
}

}

int main(int argc, char* argv[])
{
MAIL *removed = NULL;
MailList *theList = NULL;
/* Create A single List. */
theList = CreateMailList(1);
/* Insert in head always. */
Insert_After_Element(theList, NULL);
Insert_After_Element(theList, NULL);
Insert_After_Element(theList, NULL);
DumpMailList(theList);
/* Remove a mail from the head, the first mail. */
Remove_Mail(theList, NULL, &removed);
DumpMailList(theList);
printf("\n");
printf("Mail that was removed from list was:\n");
printf("SenderLogin:%sRecieverLogin:%sBodyText:%s\n", removed->sender.login, removed->reciever.login, removed->body);
printf("Hit enter to continue....");
getchar();
return 0;
}
//---------------------------------------------------------------------------

 

Εκτυπωμένα αποτελέσματα:

 

Enter the New Mail's Data:

Enter Sender Login:Kostas

Enter Reciever Login:Maria

Enter Body Message:Geia soy maria ti kaneis?

 

Enter the New Mail's Data:

Enter Sender Login:Maria

Enter Reciever Login:Kostas

Enter Body Message:Kala kwsta moy esy?

 

Enter the New Mail's Data:

Enter Sender Login:Kostas

Enter Reciever Login:Maria

Enter Body Message:Edw katse na sbisw to prwto minima!!!

 

List Print:

 

SenderLogin:Kostas

RecieverLogin:Maria

BodyText:Edw katse na sbisw to prwto minima!!!

 

SenderLogin:Maria

RecieverLogin:Kostas

BodyText:Kala kwsta moy esy?

 

SenderLogin:Kostas

RecieverLogin:Maria

BodyText:Geia soy maria ti kaneis?

 

List Print:

 

SenderLogin:Maria

RecieverLogin:Kostas

BodyText:Kala kwsta moy esy?

 

SenderLogin:Kostas

RecieverLogin:Maria

BodyText:Geia soy maria ti kaneis?

 

 

Mail that was removed from list was:

SenderLogin:Kostas

RecieverLogin:Maria

BodyText:Edw katse na sbisw to prwto minima!!!

 

Hit enter to continue....

 

Περιμένω σχόλια σου....

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Δεν θα το έλεγα Παναθηναικός είμαι αλλά είμαι φίλαθλος και μου αρέσει το ποδόσφαιρο, όταν παίζει ο Ολυμπιακός έξω του συμπαραστέκομαι, μέσα τον μισώ.

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...