Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,552 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,581 people online right now. Registration is fast and FREE... Join Now!




simple linked lists

 
Reply to this topicStart new topic

simple linked lists

Theteamdrunk
29 Oct, 2007 - 01:26 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 18


My Contributions
This code I am trying to write asks for five values and puts them in a linked list.,diplays the list and then asks for one more input and displays the new list. Heres what I have so far, but its not finished yet, and I am kind of at a standstill. Any thoughts?

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 5


typedef struct NUMBERS
{
       int value;
       struct NUMBERS *next;
       } node_t;

int main()
{
    int i, value;
  
    char name[MAXCHARS];
    
    for (i=0; i < MAXCHARS; i++)
      printf("Enter five numbers, one per line.\n");
      scanf("%d", &value[i]);
    
    printf("Your five numbers are:\n");
    
    for (i = 0; i < MAXCHARS; i++)
      printf(" %d\n", value[i];
    
    free(value);
    scanf("%d", &i);
    return 0;
    }

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Simple Linked Lists
29 Oct, 2007 - 01:31 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well are you having some trouble with the code or you need some ideas about the implementation of a single link list?
User is offlineProfile CardPM
+Quote Post

Theteamdrunk
RE: Simple Linked Lists
29 Oct, 2007 - 02:21 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 18


My Contributions
QUOTE(PennyBoki @ 29 Oct, 2007 - 02:31 PM) *

Well are you having some trouble with the code or you need some ideas about the implementation of a single link list?


I need help with implementing the linked list some I can complete my code.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Simple Linked Lists
30 Oct, 2007 - 03:24 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
you need to have an object(variable) of the structure so that you can access the variable value. Also you need to know how big the list should be so that you can allocate memory using the malloc() function, then do the inputs.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Simple Linked Lists
30 Oct, 2007 - 05:33 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,277



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(PennyBoki @ 30 Oct, 2007 - 07:24 AM) *
Also you need to know how big the list should be so that you can allocate memory using the malloc() function, then do the inputs.


NOOOOOOOO! wink2.gif Sorry, two things.

First, the entire point of a linked list is that it can be arbitrary in length. You allocate space to the nodes, not the list. You add to the list by creating a node making that node one of the links in the list. In it's simplest form, you point the new node at the top and then make it the top.

Second, malloc is a bad and evil thing, even in C++, and should be avoided at all costs. Even when using a struct in C++, you should be able to allocate and deallocate using new and delete.

Seriously, the example code doesn't really show an understanding of the concept. If you want the Internet to do your homework, ask Google. I got about 1.5 million hits, some must have the answer.



User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Simple Linked Lists
30 Oct, 2007 - 06:05 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(baavgai @ 30 Oct, 2007 - 06:33 AM) *

First, the entire point of a linked list is that it can be arbitrary in length. You allocate space to the nodes, not the list. You add to the list by creating a node making that node one of the links in the list. In it's simplest form, you point the new node at the top and then make it the top.

ok my mistake
QUOTE(baavgai @ 30 Oct, 2007 - 06:33 AM) *

Second, malloc is a bad and evil thing, even in C++, and should be avoided at all costs. Even when using a struct in C++, you should be able to allocate and deallocate using new and delete.

I think he/she is using C not C++ hence the malloc/free functions, if it was a C++ then yes I new/delete is the way to go.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month