| | |
Linked list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
For inserting a node at the beginning y the foll code wont work?
C Syntax (Toggle Plain Text)
struct node { int data; struct node *-next; }*head,*run,*ptr; typedef struct node n; void create() { if(head==NULL) { head=n*(malloc(sizeof(n)); } else { ptr=n*(malloc(sizeof(n)); ptr=head; scanf("%d",&ptr->data); head=ptr->data; ptr=ptr->next; }
•
•
•
•
For inserting a node at the beginning y the foll code wont work?
C Syntax (Toggle Plain Text)
struct node { int data; struct node *-next; }*head,*run,*ptr;
c Syntax (Toggle Plain Text)
struct node *-next;
c Syntax (Toggle Plain Text)
struct node * next;
And also the malloc is not used like this:
c Syntax (Toggle Plain Text)
head=n*(malloc(sizeof(n));
c Syntax (Toggle Plain Text)
head=(n*)malloc(sizeof(n));
Last edited by csurfer; Mar 28th, 2009 at 1:44 pm.
I Surf in "C"....
This code does not wont work, you can't compile this code (feel the difference
).
Well, you declare n as an alias of struct node type. Why?
These function works with declared elsewhere global variables. Why?
Let's forget for a minute that n is a structure type, not a number. The 1st statement allocates memory and saves a pointer to the new object in ptr pointer variable. The next statement overwrites this value immediately. Why?
And so on...
It seems you must reread your C textbook right now...
). c Syntax (Toggle Plain Text)
struct node *-next; /* Why minus sign? */
c Syntax (Toggle Plain Text)
typedef struct node n;
c Syntax (Toggle Plain Text)
void create() { ... }
c Syntax (Toggle Plain Text)
ptr=n*(malloc(sizeof(n)); ptr=head;
And so on...
It seems you must reread your C textbook right now...
Lets just take a look at this code: You don't need line 1 because you just set it to point to a space of memory that has already been allocated (
C Syntax (Toggle Plain Text)
ptr=n*(malloc(sizeof(n)); ptr=head; scanf("%d",&ptr->data); head=ptr->data; ptr=ptr->next;
head=n*(malloc(sizeof(n)); ). Lines 2 and 3 look alright to me. Line 4 doesn't work (I sure hope that doesn't compile) because head is a pointer (to a struct node) and you are trying to assign to it a plain old integer. Since ptr and head point to the same thing, line 3 takes care of what you want. Line 5 may seem to make sense, but it actually does nothing. At the end of this function call, ptr will be deleted. A few final notes: the first time you call this function, it will only allocate memory, not load any data. And you seem to be missing a closing } for the function. ![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C Forum
- Previous Thread: Output multiple files
- Next Thread: Question about my FUNC
Views: 442 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function functions getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






