473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C create linked list

36
i need to create a linked lists in size =N
each struct pointing to the next sruct the user will enter and the first struct pointed by constant pointer head.

TNX .............

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define size=5
  3. void main()
  4.  
  5. {
  6.     typedef struct
  7.     {
  8.         int x,y;
  9.         struct Strct *next;
  10.     }Coordinate;
  11.  
  12.     for(i=0; i<size; i++)
  13.     {
  14.  
  15.         Coordinate (?????);
  16.         printf("Enter name & value: <name,value>\n");
  17.         scanf("%d,%d",(????));
  18.     }
  19.  
  20.  
  21.  
  22. }
Sep 10 '09 #1
19 3516
JosAH
11,448 Expert 8TB
And what is your question?

kind regards,

Jos
Sep 10 '09 #2
sedaw
36
how to do it , what i should add to the code ?

tnx....
Sep 10 '09 #3
donbock
2,426 Expert 2GB
Each node in a linked list consists of two parts: there are the links used to connect the nodes together; and there is the body or payload of the node. What goes in the body of your list?

Please tell me a little of what you've already learned about linked lists?
Sep 11 '09 #4
manjuks
72
If you go through books you will get functions to add some data to list. Just go through any book, try to understand, post again if you still come across any doubts.

Thanks,
Manjunath
Sep 11 '09 #5
do you want help on how to learn how to create a linked list or are you trying to get it built for you?
Sep 11 '09 #6
sedaw
36
@loonman
yes , i want to learn all bout linked lists , i do read books .
__________________________________________________ ________________

ok.... , what about that ?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.     typedef struct 
  4.     {
  5.         int x,y;
  6.         char str[20];
  7.         struct Node* next;
  8.     }Node ;
  9.  
  10. // i will add option to user initialize the field latter (thats not what important)
  11. void main()
  12. {
  13.     Node *head=NULL, *temp ;
  14.     int i, size;
  15.     printf("Enter the size of linked list\n");
  16.         scanf("%d",&size);
  17.  
  18.         for(i=0; i<size; i++)
  19.         {
  20.             temp=(Node*)malloc(sizeof(Node));
  21.             temp->next=head;
  22.             head=temp;
  23.         }
  24.  
  25. }
  26.  
the warning mesages:


line 21 : warning C4133: '=' : incompatible types - from 'Node *' to 'Node *
line 20 : warning C4013: 'malloc' undefined; assuming extern returning int


tnx..
Sep 11 '09 #7
manjuks
72
Hi,

For first warning -- you have to define structure like below,

struct node
{
int x,y;
char str[20];
struct node *next;
};

typedef struct node Node;

For second warning -- malloc is defined in stdlib.h header file, so you have to include that header file.

Thanks,
Manjunath
Sep 11 '09 #8
donbock
2,426 Expert 2GB
@sedaw
@manjuks
In your original code the first place the type name "Node" appears is in the definition of the "next" field. The compiler does not yet recognize that name as a type. Now look at Manjunath's alternative -- the compiler is aware there is a struct named "node" by the time it gets to the definition of the "next" field. That's why Manjunath's code makes the compiler warning go away.
Sep 11 '09 #9
donbock
2,426 Expert 2GB
Divide and conquer.
You need a function that allocates a node and initializes its payload. Then you need another function that inserts a node into a linked list.

Does the assignment say whether you're to create a singly- or doubly-linked list?
Sep 11 '09 #10
sedaw
36
thank you all !


@donbock
what`s the difference of singly- or doubly-linked ?

that`s not assignment i just tryin to learn linked lists .
TNX .
Sep 11 '09 #11
donbock
2,426 Expert 2GB
@sedaw
Linked lists are described in any number of software engineering books.
The internet is your friend: there's Wikipedia; a link provided in an earlier forum reply; and there's Google.

This forum is pretty good at answering specific questions. An open-ended question like "tell me about linked lists" is best answered by an essay or book chapter, not a forum reply that I throw together in a couple of minutes.
Sep 11 '09 #12
JosAH
11,448 Expert 8TB
@sedaw
A singly linked list contains one pointer per node that points to another node; e.g.

A ---> B ---> C ---> D ---> E

this list contains five elements where node A points to node B and at the end node D points to node E which is the last node in the list (A is the first node). The pointer of node E doesn't point to another node and is null.

A doubly linked list has two pointers per node; one pointer points to a next node (same as a singly linked list) and the other pointer points to the previous nodes in the list: e.g.

A <==> B <==> C <==> D <==> E

The last node (E) doesn't pont to a next node and the first node (A) doesn't point to a previous node. There are variations on the scenario.

kind regards,

Jos
Sep 11 '09 #13
sedaw
36
tnx...

one more litlle thing.

what`s wrong in line 24:

scanf("%d,%d",head->x,head->y);

theres no debugging prob .

but the prog collapse in line 24 .


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.     struct node
  5.     {
  6.         int x,y;
  7.         char str[20];
  8.         struct Node* next;
  9.     } ;
  10.     typedef struct node Node;
  11.  
  12. void main()
  13. {
  14.     Node *head=NULL, *temp, *currItem;
  15.     int i,size;
  16.     printf("Enter the size of linked list\n");
  17.         scanf("%d",&size);
  18.         for(i=0; i<size; i++)
  19.         {
  20.             temp=(Node*)malloc(sizeof(Node));
  21.             temp->next=head;
  22.             head=temp;
  23.             printf("Enter coordinate, <x,y>\n");
  24.             scanf("%d,%d",head->x,head->y);
  25.         }
  26.         currItem=head;
  27.         for(i=0; i<size; i++)
  28.         {
  29.             printf("value of cuurent item on linked list: %d\n",currItem->x);
  30.             currItem=currItem->next;
  31.         }
  32.  
  33. }
  34.  
Sep 11 '09 #14
JosAH
11,448 Expert 8TB
@sedaw
The scanf function needs to know where it has to store its parsed result; you have to pass it the address of something to tell that to the scanf function. You are passsing it two ints instead which are not addresses. Do this instead:

Expand|Select|Wrap|Line Numbers
  1. scanf("%d,%d",&(head->x),&(head->y));
  2.  
Here you are passing the addresses of those two int slots so scanf knows where to put its results.

kind regards,

Jos

ps. don't forget to type a comma between those two ints; scanf wants to see it according to your format string "%d,%d"
Sep 11 '09 #15
donbock
2,426 Expert 2GB
@sedaw
malloc will return NULL if it is unable to allocate the memory you request. You should always check the return value. If malloc returns NULL then you could [for example] print a message to stderr and invoke exit(1).
Sep 11 '09 #16
JosAH
11,448 Expert 8TB
... and it's "int main( ... )", not "void main( ... )"; when will you people learn? It even saves one entire keystroke, whick makes millions of keystrokes per year so your keyboard doesn't wear out that much so you save money, reduce your debts and buy your dog a proper meal and more important: it's the Standard! When will you learn instead of act monkey-see-monkey-do with that stupid Microsoft, the source of this silly error!

kind regards,

Jos ;-)
Sep 12 '09 #17
amen brother!!! heathens! int your main! avoid the void!!
Sep 12 '09 #18
JosAH
11,448 Expert 8TB
@loonman
I know that the OP has sinned father; may I suggest a short and efficient defenestration and afterwards we'll wiggle our genitals towards his general direction so his remains will be in shame on the pavement.

kind regards,

Jos ;-)
Sep 12 '09 #19
@JosAH
the imagery is so vivid. i totally agree. (wiping tears from my eyes)

in complete admiration,
Loon
Sep 12 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: surrealtrauma | last post by:
I want to ask what's the differences between doubly liked list and linear liked list, and also the circular doubly liked list in terms of implementation. THX
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
0
by: Chris Powell | last post by:
I am using Excel/Access 2000 and have two large Excel files (25,000 rows each) that I wish to create linked tables in Access rather than importing into Access. The two source Excel files change...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
4
by: sandeep | last post by:
When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container...
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
6
by: The Natural Philosopher | last post by:
I am trying to create what amounts to an array of 'structures'. I.e. I want to dynamically add to and access an object as myobject.anothernumber // actually represents a nesting level. and ...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.