Need of linked list program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 2
Reputation: chat2fanna is an unknown quantity at this point 
Solved Threads: 0
chat2fanna chat2fanna is offline Offline
Newbie Poster

Need of linked list program

 
0
  #1
Jul 29th, 2009
Hi guys or gals ,
i'm in trouble with my class teacher, with in two days i have to finish my lab work .... i'm in need of c program using linked list
my concept is
1. creating a linked list
2.Inserting a number in front of it
3.Insert a nubmber last of it.
4.Searching a number in the created list
5.Deleting a number from the list
6.Deleting the first number
7.Deleting Last Number

I tried little bit
please help me with the same datamembers ..
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<alloc.h>
  4. struct list
  5. {
  6. int data;
  7. struct list *next;
  8. }*head,*temp,*node;
  9. typedef struct list sl;
  10. void main()
  11. {
  12. node=(sl*)malloc(sizeof(sl));
  13. printf("Enter the numbers");
  14. scanf("%d",&node->data);
  15. if(head==Null)
  16. {
  17. head=node;

below that i dont know how to do please help me
Last edited by John A; Jul 30th, 2009 at 2:31 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Need of linked list program

 
0
  #2
Jul 29th, 2009
And how long did you spend on this?

Draw it on paper! Use multiple colors to work out insertions and removals.

Your amount of code is inadequate. Maybe spend more time listening in the classroom and taking notes? Work on a team with fellow classmates?

What I'm providing here is really TOO much information for the microscopic amount of work you did!

Unswizzle your structure, especially since you are typedefingi it
  1. typedef struct list
  2. {
  3. int data;
  4. list *next;
  5. } list;
list *head,*temp,*node;


Pre-initialize!
Head = NULL;


If insertion of a node at front of list
Allocate the node
if Node != NULL then allocation successful!

Node->next = Head <-- What was head pointing at?
Head = Node <-- Head now points to new node

Repeat for each prepended node!

If all new nodes are to be appeneded
  1. Node *pPre;
  2. if (NULL == Head) // First in list
  3. do it like above
  4. else
  5. pTmp = Head
  6. while ( NULL !=pTmp->next) Loop until there is no next
  7. pTmp = pTmp->next
  8.  
  9. pTmp is NOW last node
  10. pNode->next = NULL
  11. pTmp->next = pNode


For node removal, since you are using a single-linked list you need to have your node to be removed to be the one after the node you're referencing.
  1. if node First in list
  2. head = head->next->next;
  3. else <-- ptmp is before the node to remove
  4. pTmp->next = pTmp->next->next;
Last edited by wildgoose; Jul 29th, 2009 at 2:46 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC