![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
hey i'm trying to write this gay little linked list that basically takes a name into the structure, that's it. this is one of those "it compiles but doesn't run" things again. pointers suck nuts, but you gotta love 'em. this is stripped down to bare bones and what-not. oh and obviously i';m just learning linked lists. awesome yet difficult. i'm waiting for eggbert to swoop in and do some magic. also, until they hopefully split this forum, i think everyone should put which langugae they're talking about in their thread title like i did.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct data {
char name[81];
struct data * next;
};
int main()
{
struct data * head = NULL;
struct data * new1;
struct data * current;
char nname[81];
char choice;
while(1)
{
current = head;
while (current->next != NULL)
{
current = current->next;
}
new1 = (struct data *)malloc(sizeof(struct data));
new1->next = head;
head = new1;
puts("enter a name");
gets(nname);
strcpy(new1->name, nname);
puts("another?");
scanf("%c", &choice);
if (choice == 'y')
continue;
else
break;
}
current = head;
while (current != NULL)
{
printf("\n%s", current->name);
current = current->next;
}
return 0;
}
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
ok i got it to work a little with a rather unglamorous solution. since it was testing for the null condition that it already met the loop sent a pointer off into space. so i just said fuck it and instantiated a node withe the name "ben" so head wouldn't point to NULL when the loop condition was being tested cuz i'm lazy and i'm drinking. it works, but only loops once, when it asks "another?" and you say 'y', it just keeps saying another. this may make more sense in the morning but look at it anyway.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct data {
char name[81];
struct data * next;
};
int main()
{
struct data * head = NULL;
struct data * new1;
struct data * current;
char nname[81];
char choice;
new1 = (struct data *)malloc(sizeof(struct data));
new1->next = head;
head = new1;
strcpy(new1->name, "ben");
while(1)
{
current = head;
while (current->next != NULL)
{
current = current->next;
}
new1 = (struct data *)malloc(sizeof(struct data));
new1->next = head;
head = new1;
puts("enter a name");
gets(nname);
strcpy(new1->name, nname);
puts("another?");
scanf("%c", &choice);
if (choice == 'y')
continue;
else
break;
}
current = head;
while (current != NULL)
{
printf("\n%s", current->name);
current = current->next;
}
return 0;
}
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
add a few functions for adding to a node reading a node, i generally found that it made it easier to create a linked list that way.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
Here, I made modified some of your code, but please note, that this is in C++:
#include <iostream>
using namespace std;
struct data {
char name[81];
data* next;
};
data* head;
void printName();
int main()
{
data* current;
char nname[81];
char choice='y';
while(choice=='y'){
cout << "enter a name" << endl;
cin >> nname;
data* new1;
new1=new data;
strcpy(new1->name, nname);
new1->next = head;
head = new1;
cout << "another?" << endl;
cin >> choice;
}
printName();
system("pause");
return 0;
}
void printName()
{
data* current;
current = head;
while (current != NULL)
{
cout << current->name << endl;
current = current->next;
}
} |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
Actually you don't need data* current in main, so just delete that part.
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
If you're going to do it in C++ why not just use a class? Or <vector> for that matter...
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
I can make the class implementation of this program, but what I'm trying to do here is to help bloodninja's program which does not use class. And same reason for why I don't use <vector>.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
yeah, i'm trying to play aroung with linked lists, not implement simpler ways of doing things. thanks for the help baron.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. Last edited by bl00dninja; Apr 20th, 2005 at 4:16 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|