Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-01-2008, 11: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-03-2008 at 12:35 AM. Reason: Added [code][/code] tags to make it more readable
dave120 is offline   Reply With Quote
Old 02-03-2008, 02:36 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
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.
Code:
// Insert at the front of the list
	if(lname != (*head)->lname)
	{
		newnode->next = *head;
		*head = newnode;
    
		return;
	}
Isn't this a false way of doing it ??
you will never reach the while loop for inserting your node.
Unless your added node is the exact same as the very first node in your list.

For some further thoughts on this subject, you might want to read teh Pointer error thread.

For some fun with how to handle pointers, you might want to read my example in the File-I/O arrays thread.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-04-2008, 12:50 PM   #3 (permalink)
dave120
Recruit
 
Join Date: Sep 2006
Posts: 10
dave120 is on a distinguished road
Thanks for the help.

I still haven't been able to fix this issue however. It seems to add nodes correctly but it always just outputs however many when I print with just the most recent values.

The code you referenced I've taken out just to simplify things and make it so it only adds names at the end of the list since the way I'm going to search it won't care what order they're in.

Apparently it inserts nodes as it always ends up with the right number of names in the list. I just can't figure out if I'm referencing something wrong when I print them out or if it's actually replacing every name with the most recent in the list.
dave120 is offline   Reply With Quote
Old 08-23-2008, 12:57 PM   #4 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
I am working on your program!

What did you have here:
Code:
crnt =
landonmkelsey is offline   Reply With Quote
Old 08-23-2008, 05:57 PM   #5 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
fun challenge

make printList recursive..i.e. it calls itself until it hits that last null

I'll do it when I get all the code
landonmkelsey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List Constructor help... fp_unit Standard C, C++ 6 01-31-2005 04:23 PM
linked list problem if13121 Standard C, C++ 3 11-12-2004 09:58 AM


All times are GMT -8. The time now is 05:16 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting