Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2005, 1:19 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Wink linked list:in C

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.
bl00dninja is offline   Reply With Quote
Old Apr 20th, 2005, 2:01 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Red face

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.
bl00dninja is offline   Reply With Quote
Old Apr 20th, 2005, 2:25 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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.
Berto is offline   Reply With Quote
Old Apr 20th, 2005, 7:10 AM   #4
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
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;
    }
}
BaroN NighT is offline   Reply With Quote
Old Apr 20th, 2005, 9:02 AM   #5
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Actually you don't need data* current in main, so just delete that part.
BaroN NighT is offline   Reply With Quote
Old Apr 20th, 2005, 9:27 AM   #6
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
If you're going to do it in C++ why not just use a class? Or <vector> for that matter...
uman is offline   Reply With Quote
Old Apr 20th, 2005, 11:05 AM   #7
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
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>.
BaroN NighT is offline   Reply With Quote
Old Apr 20th, 2005, 4:08 PM   #8
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC