View Single Post
Old 02-01-2008, 10:05 PM   #1 (permalink)
dave120
Recruit
 
Join Date: Sep 2006
Posts: 10
dave120 is on a distinguished road
Linked List in C help

Hello,

I'm trying to make a program that uses a linked list to store a first and last name of people. I'm eventually going to make it read from a file to get this info but for now it just gets input manually from the menu.

The program compiles and runs fine without errors, but I'm having an issue when I try and display it of whatever the most recent name I added being shown for each person in the list. I think my delete function needs work too, but I'm trying to work out one thing at a time. My code that I have so far is below:
Code:
#include <stdio.h>
#include <stdlib.h>

struct node
{
	char* fname;
	char* lname;
	struct node* next;
};


void insert(struct node** head, char* fname, char* lname)
{
	struct node* newnode;
	struct node* crnt;

	// Create the node that will be inserted
	newnode = (struct node*)malloc(sizeof(struct node));
	newnode->fname = fname;
	newnode->lname = lname;

	// Insert into empty list
	if(*head == NULL)
	{
		newnode->next = NULL;
		*head = newnode;

		return;
	}

	// Insert at the front of the list
	if(lname != (*head)->lname)
	{
		newnode->next = *head;
		*head = newnode;
    
		return;
	}


	// Find the correct place to insert the node
	crnt = *head;
	while(crnt->next != NULL && crnt->next->lname != lname)
	// 
	{
		crnt = crnt->next;
	}

	// Insert the node into the middle of the list

	newnode->next = crnt->next;
	crnt->next = newnode;
}

int deleteNode(struct node** head, char* fname, char* lname)
{
	struct node* temp;
	struct node* crnt;	

	if((*head) == NULL)
		return 0;

	// Delete the head if it's the node we're looking for
	// (including if it's the only node)
	if(((*head)->lname == lname) && ((*head)->fname == fname))
	{
		temp = *head;
		(*head) = (*head)->next;
		free(temp);
		return 1;
	}

	// Track down the node to delete
	crnt = *head;
	while(crnt->next != NULL && crnt->next->lname != lname)
		crnt = crnt->next;

	// the value we're looking for isn't present
	if(crnt->next == NULL || crnt->next->lname != lname)
		return 0;

	// Delete the target node
	temp = crnt->next;
	crnt->next = crnt->next->next;
	free(temp);
	return 1;
}


void printList(struct node* head)
{
	struct node* crnt = head;
	
	// Loop through each node in the list printing its value
	while(crnt != NULL)
	{
		printf("%s %s\n", crnt->fname, crnt->lname);
		crnt =
	}
}

int main(void)
{
	struct node* head = NULL;
	int choice;
	int number;
    char fname[1024];
	char lname[1024];

	printf("Welcome to linked list program!!\n");

	do
	{
		printf("Make a choice:\n");
		printf("1) Add a Name\n");
		printf("2) Delete a Name\n");
		printf("3) Print the list\n");	
		printf("0) Quit\n");

		scanf("%d",&choice);

		switch(choice)
		{
		case 1:
			//Insert case
			printf("Enter the First Name to insert\n");
			scanf("%s",&fname);
			
			printf("Enter Last Name:");
			scanf("%s",&lname);
			insert(&head, fname, lname);
			break;
		case 2:
			// Delete case
			printf("Enter the Name to delete\n");
			scanf("%s %s",&fname,&lname);
			if(deleteNode(&head,fname,lname))
				printf("Deleted\n");
			else
				printf("That node didn't exist\n");
			break;

		case 3:
			// Print case
			printList(head);
			break;
		case 0:
			// Quit
			printf("Thanks for using the program!\n");
			break;
		default:
			printf("That's not a valid option\n");
		}
	}while(choice != 0);

	system("PAUSE");
	return 0;
}

Last edited by redhead; 02-02-2008 at 11:35 PM. Reason: Added [code][/code] tags to make it more readable
dave120 is offline   Reply With Quote