Thread: Help in Linked List Operations

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    35

    Help in Linked List Operations

    Guys, please help me with these operations on linked list will ya? Please..

    a. initializeList – to initialize the count to 0 and head to null.

    b. deleteList – to delete any nodes in the linked list.

    c. listEmpty – check if the linked list is empty.

    d. printList – to print each number in the linked list.

    this is my header file.
    Code:
    #ifndef LINKEDLISTH
    
    #define LINKEDLISTH
    
    typedef int ListEntry;
    
    typedef struct listnode {
    
    ListEntry entry;
    struct listnode *nextnode;
    
    } ListNode;
    
    typedef int Position;
    
    typedef struct list {
    int count;
    ListNode *head;
    } List;
    
    ListNode *makeListNode(ListEntry x);
    void setPosition(Position p, List *L, ListNode **current);
    void insertList(Position p, ListEntry x, List *L);
    void Error(const char *ErrorDesc);
    void initializeList(List *ptr);
    void deleteList();
    void printList(ListNode *L);
    
    
    
    #endif
    this is my main file.
    Code:
    //This file contains all functions implementation in linkedlist.h
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "linkedlist.h"
    
    int main(void)
    {
    	List *pList;
    	ListNode *listnode;
    	ListEntry x;
    	Position p;
    
    	initializeList(&pList); //question 1 a
    
    	printf("Enter the number of nodes: ");
    	scanf("%d", &p);
    	printf("Enter values:");
    	scanf("%d", &x);
    	while (x!=-999)
    		{
    		insertList(p, x, &pList);
    		scanf("%d", &x);
    		}
    		
    	printList(&listnode);
    	deleteList();
    	
    	return 0;
    }
    
    
    //read data n put the data into a structure
    ListNode *makeListNode(ListEntry x)
    {
    	ListNode *newPtr = (ListNode*)malloc(sizeof(ListNode));
    
       if (newPtr == NULL) {
          Error("Out of memory");
       }
       else {
    
    		newPtr->entry = x;
    		newPtr->nextnode = NULL;
       }
    
       return newPtr;
    }
    
    
    void setPosition(Position p, List *L, ListNode **current)
    {
    	int i;
    
    	ListNode *nodePtr = L->head;
    	*current = nodePtr;
    
    	if( p < 0 || p > L->count )
    	{
    		Error("Invalid position\n");
    	}
    	else
    	{
    		for ( i = 0; i < p; i++)
    		{
    			nodePtr = nodePtr->nextnode;
    			*current = nodePtr;
    		}
    	}
    }
    
    void insertList(Position p, ListEntry x, List *L)
    {
    	ListNode *newNodePtr = makeListNode( x );
    	ListNode *prevPtr = NULL;
    
    	if ( p == 0 )
    	{
    		newNodePtr->nextnode = L->head;
    		L->head = newNodePtr;
    	}
    	else
    	{
    		setPosition( p-1,L,&prevPtr );
    		prevPtr->nextnode = newNodePtr;
    	}
    	L->count++;
    }
    
    void Error(const char *ErrorDesc)
    {
    	printf("%s",ErrorDesc);
    }
    
    void initializeList(List *ptr)
    {
    	ptr->count=0;
    	ptr->head=NULL;
    }
    
    void deleteList(List *pList)
    {
    	int numDelete;
    	ListNode *pPre;
    	ListNode *pCur;
    
    	pCur=pList->head;
    
    	printf("Enter integer to be deleted : ");
    	scanf("%d", &numDelete);
    		while (pCur!=NULL)
    		{
    			if (pCur->entry==numDelete)
    			{
    				pPre->nextnode=pCur->nextnode;
    				free(pCur);
    			}
    			else
    				pCur=pCur->nextnode;
    		}
    }
    
    
    void printList(ListNode *L) {
    		while(L!=NULL) {
    			printf("%d\t", L->entry);
    			L = L->nextnode;
    		}
    	}
    Please correct me if there is any mistake..
    Last edited by aren34; 03-19-2011 at 08:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use [code][/code] tags for posting code please.
    You should still be able to edit your post to correct this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    ok.. first timer here.. sorrry..

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What is your thought behind this?

    Code:
    printf("Enter values:");
    scanf("%d", &x);
    while (x!=-999)
    {
    	insertList(p, x, &pList);
    	scanf("%d", &x);
    }
    You start to asking for number of nodes. Is -999 suppose to be an exit condition that you enter manually?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    Quote Originally Posted by Subsonics View Post
    What is your thought behind this?

    Code:
    printf("Enter values:");
    scanf("%d", &x);
    while (x!=-999)
    {
    	insertList(p, x, &pList);
    	scanf("%d", &x);
    }
    You start to asking for number of nodes. Is -999 suppose to be an exit condition that you enter manually?
    I wish to prompt user to enter a series of number and if user want to exit, he/she has to enter -999.. And print out the lists.. Please help me out. thanks!

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    One thing I note is that:

    Code:
    List *pList;
    
    // is used like this
    
    initializeList(&pList);
    When the implementation looks like this:

    Code:
    void initializeList(List *ptr)
    {
        ptr->count=0;
        ptr->head=NULL;
    }
    List *pList, is only a pointer variable not an actual List type. You would need to declare it like this: List list;

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    35

    Unhappy

    Quote Originally Posted by Subsonics View Post
    One thing I note is that:

    Code:
    List *pList;
    
    // is used like this
    
    initializeList(&pList);
    When the implementation looks like this:

    Code:
    void initializeList(List *ptr)
    {
        ptr->count=0;
        ptr->head=NULL;
    }
    List *pList, is only a pointer variable not an actual List type. You would need to declare it like this: List list;
    Declare as List list? Would you enlighten me pls? I'm really bad in this linked list stuffs..

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by aren34 View Post
    I wish to prompt user to enter a series of number and if user want to exit, he/she has to enter -999.. And print out the lists.. Please help me out. thanks!
    So it's an exit condition. I just thought it was odd since you did just ask for the number of nodes just before, but then continue on ignoring this and instead use "number of nodes" as a position. In an empty list there is only one valid position.

    Quote Originally Posted by aren34 View Post
    Declare as List list? Would you enlighten me pls? I'm really bad in this linked list stuffs..
    Basically get rid of the *.


    Edit: I would recommend that you (temporarily) get rid of what you now have in main(), then test each list function separately to make sure they all work as intended in isolation.
    Last edited by Subsonics; 03-19-2011 at 09:22 AM.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    Quote Originally Posted by Subsonics View Post
    So it's an exit condition. I just thought it was odd since you did just ask for the number of nodes just before, but then continue on ignoring this and instead use "number of nodes" as a position. In an empty list there is only one valid position.




    Basically get rid of the *.


    Edit: I would recommend that you (temporarily) get rid of what you now have in main(), then test each list function separately to make sure they all work as intended in isolation.

    After i get rid of the *, i get these error..
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(15) : error C2440: 'function' : cannot convert from 'List *' to 'List'
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(15) : warning C4024: 'initializeList' : different types for formal and actual parameter 1
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(23) : warning C4047: 'function' : 'List *' differs in levels of indirection from 'List **'
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(23) : warning C4024: 'insertList' : different types for formal and actual parameter 3
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(27) : warning C4047: 'function' : 'ListNode *' differs in levels of indirection from 'List **'
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(27) : warning C4024: 'printList' : different types for formal and actual parameter 1
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(98) : error C2232: '->count' : left operand has 'struct' type, use '.'
    1>c:\users\acer\desktop\data structure assignment 2\linkedlist.c(99) : error C2232: '->head' : left operand has 'struct' type, use '.'


    Regarding the nodes things, Does this mean i have to ignore the number of nodes? and let p=0? if i dun initialize p, error will pop out..

    Quote Originally Posted by Subsonics View Post
    So it's an exit condition. I just thought it was odd since you did just ask for the number of nodes just before, but then continue on ignoring this and instead use "number of nodes" as a position. In an empty list there is only one valid position.



    Basically get rid of the *.


    Edit: I would recommend that you (temporarily) get rid of what you now have in main(), then test each list function separately to make sure they all work as intended in isolation.
    Sorry that I juz saw your Edit... how do i test each function without using the main? Im really troubling you a lot, sir..

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Look at your own insertList implementation. the variable 'p' is used to mark a position in the list. Regarding the errors, again look at your own initializeList function, you need to send the address of an actual existing node since you are manipulating that node in the function. An empty pointer wont do. If you get errors further down, then test each function separately to isolate the problem instead of trying to solve everything at once.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    Quote Originally Posted by Subsonics View Post
    Look at your own insertList implementation. the variable 'p' is used to mark a position in the list. Regarding the errors, again look at your own initializeList function, you need to send the address of an actual existing node since you are manipulating that node in the function. An empty pointer wont do. If you get errors further down, then test each function separately to isolate the problem instead of trying to solve everything at once.
    hmmm.. the p is to mark the position in the list, but how do i manipulate it?

    i'll try to fix the initializeList function.. and is there a way to test the functions one by one without dealing with the main function?

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by aren34 View Post
    hmmm.. the p is to mark the position in the list, but how do i manipulate it?
    I don't know, the way you use it in main(), on an empty list p should probably be zero.

    Quote Originally Posted by aren34 View Post
    i'll try to fix the initializeList function.. and is there a way to test the functions one by one without dealing with the main function?
    There is nothing wrong with your initializeList function. You could just wrap your current main() inside /* */ then create a new temporary one to test the functions one by one. For example create a single node and add 1 value. Does it work? etc.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    could you give me an example on creating a single node, add value and print?

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could do this:

    Code:
    int main()
    {
        List list;
    
        initializeList(&list);
        insertList(0, 123, &list);
    
        return 0;
    }
    Print wont work since you have created a special struct for the node, in your current code '*listnode' is an empty pointer, I don't see how you could use it to print the list, it's unrelated to the list.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    35
    Quote Originally Posted by Subsonics View Post
    You could do this:

    Code:
    int main()
    {
        List list;
    
        initializeList(&list);
        insertList(0, 123, &list);
    
        return 0;
    }
    Print wont work since you have created a special struct for the node, in your current code '*listnode' is an empty pointer, I don't see how you could use it to print the list, it's unrelated to the list.

    I see.. I tried the one you wrote here.. It got no error however there is nothing shown on the command prompt.. just a line "press any key to continue..."

    hmm.. Actually what I'm blur here is that I got List and ListNode.. dont know how to differentiate between them I never know ListNode is empty in this situation...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM