Thread: append queue

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    append queue

    hello everyone,im trying to make a simple program run but i cant seem to make it work,i have been working on this for the whole day :\ any hand

    Code:
    #include<stdio.h>
    
    struct Node{
    	int Data;
    	struct Node *Link;
    };
    typedef struct Node *QueuePointer;
    
    void append(QueuePointer *Head,QueuePointer *Tail, int Num)
    {
    	QueuePointer NewNode;
    	NewNode=(QueuePointer)malloc(sizeof(struct Node));
    	NewNode->Data=Num;
    	NewNode->Link=NULL;
    	if (Tail==NULL)
    		*Head=NewNode;
    	else
    		(*Tail)->Link=NewNode;
    		*Tail=NewNode;
    }
    
    void main()
    {
    	QueuePointer Tail;
    	int value,a,b,c;
    	int x;
    	Tail=NULL;
    	clrscr();
    
    	for(b=1; b<6; b++)
    
    {
    	printf("Enter Values to Append: ");
    	scanf("%d",&Tail);
    }
    
    {
    	for(a=1; a<6; a++)
    	append(&Tail);
    	while(Tail!=NULL)
    	{
    	printf("%d\n",&Tail);
    
    
    
    }
    }
    	getch();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We have a beautifully-written, perfectly-indented append function. And then we have the main function. Let's look at that briefly:
    1. There is no such thing as void main.
    2. Nor is there such a thing as clrscr.
    3. You cannot write directly into a QueuePointer variable using scanf, and especially when you tell scanf that the variable is an int ("%d").
    4. You read six times, but into one variable, so the first five just go away.
    5. You then try to append the same number six times.
    6. If you look at the append function up top, you will see that you need to give it two pointers-to-QueuePointers and an int. This is one more QueuePointer than you even have.
    7. Your while loop has an exit condition, which is good; however, you make no attempt to make the exit condition true inside the loop, so you will never exit.
    8. No such thing as getch.
    9. Indentation!

    I guess that's a start.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    uhm...what do you mean by this?sorry i have bad english

    There is no such thing as void main.
    Nor is there such a thing as clrscr.
    No such thing as getch.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "No such thing" = "does not exist".

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    ohh i understand what that mean buttttttttt,...

    so this means i dont have to use void main?clrscr?ang getch?

    but after i input the value i want to clear the screen :\

  6. #6

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stitches View Post
    ohh i understand what that mean buttttttttt,...

    so this means i dont have to use void main?clrscr?ang getch?

    but after i input the value i want to clear the screen :\
    The correct minimum C program is...
    Code:
    int main (void)
     {
    
        return 0;  
    }
    Main returns an integer value to the operating system. In most cases this is either 0 or an error level.

    Your compiler should be telling you it can't find a function named ... clrscr() or getch() ... pay attention to your compiler's error and warning messages!

    Both clrscr() and getch() are non-standard functions that originated with the wildly outdated Borland Turbo C compiler that nobody in their right mind would be using anymore.

    Also note that repeatedly clearing the screen can actually be disorienting to an operator, as can too many on-screen messages. If you have them doing repeated entries, let them scroll up the screen so that, should they become distracted, they can easily discover where they left off. Also don't try to keep the screen full of stuff, print only the essential information so as not to distract your operators. (Just because you can do something, does not mean you should)

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    stuck

    Code:
    #include<stdio.h>
    
    struct Node{
    	int Data;
    	struct Node *Link;
    };
    typedef struct Node *QueuePointer;
    
    void append(QueuePointer *Head,QueuePointer *Tail, int Num)
    {
    	QueuePointer NewNode;
    	NewNode=(QueuePointer)malloc(sizeof(struct Node));
    	NewNode->Data=Num;
    	NewNode->Link=NULL;
    	if (Tail==NULL)
    		*Head=NewNode;
    	else
    		(*Tail)->Link=NewNode;
    		*Tail=NewNode;
    }
    
    main()
    {
    	QueuePointer Tail,Head;
    	int value,b;
    	int x;
    
    	Tail=NULL;
    
    
    
    	for(b=1; b<6; b++)
    
    
    	{
    	printf("Enter Values to Append: ");
    	scanf("%d",&Tail);
    	}
    
    
    	printf("
    %d",Tail);
    
    
    
    	getch();
    }

    i managed to display the only 1 value i entered...why is this?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Because you only printed one value - note you only printed tail once, and not even the data in tail. In order to print all the values in your list you will need to use a loop. Additionally, read back through the FAQs tabstop posted since you still do not have the correct definition of main in your program. You may find some value in Prelude's Linked List Tutorial.

    This is pretty evident you are using pieces of code you did not write, I would suggest you sit down and actually try to learn what you are doing.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stitches View Post
    i managed to display the only 1 value i entered...why is this?
    Scroll back up and read message #2 ... just because there's no code sample in it does not mean it has no meaning.

    (Hint: There is no point asking a question if you are just going to ignore the answers.)

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    how would i learn if i wont get the concept on how to display it.I am already working out on the help you guys are giving me but i cant seem to figure it out, I usually understand through example,not through lecture.application over theory, I am also doing my part here to try and understand what you guys are sharing to me and its getting frustrating how ive been working here without getting results,not everyone is alike that can easily understand complicated theories :\ and we came from different instructors

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, although your indentation for append is good overall, there is something misleading about it here:
    Code:
    if (Tail==NULL)
        *Head=NewNode;
    else
        (*Tail)->Link=NewNode;
        *Tail=NewNode;
    It looks like if Tail is equal to NULL, NewNode is assigned to *Head, and that's all. Otherwise, NewNode is assigned to the Tail pointer's Link, then NewNode is assigned to *Tail. However, because braces were not used to introduce scope, the code, this line is run regardless of whether Tail == NULL:
    Code:
    *Tail=NewNode;
    Therefore, when Tail == NULL, NewNode is assigned to *Tail, which means you make the mistake of dereferencing a null pointer. This could have been avoided if you had written:
    Code:
    if (Tail==NULL)
    {
        *Head=NewNode;
    }
    else
    {
        (*Tail)->Link=NewNode;
        *Tail=NewNode;
    }
    That said, this correction is only a single step. I suggest that you get rid of the QueuePointer typedef. It would then be easier for you to see the levels of indirection involved. For example, checking if Tail == NULL does not necessarily mean that *Tail would not be NULL, and in a larger program this could be a problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are printing the tail. You should start at the head, and then print until you reach the tail. Also, you make Tail = NULL, and then you do this:
    Code:
    	scanf("%d",&Tail);
    You can't scan into a NULL pointer - well you can, but you're going to crash your program.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, considering I am feeling "generous" tonight:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct myList{
    	int data;
    	struct myList* next;
    };
    
    void addList(struct myList**, int);
    
    int main(void){
    	
    	//create anchor and set to null
    	struct myList* list = NULL;
    
    	int number;
    
    	//add 5 items to list
    	for(int i=0;i<5;i++){
    		printf("Enter number: ");
    		scanf("%d", &number);
    		addList(&list, number);
    	}
    	
    	//print list
    	while(list){
    		printf("%d ", list->data);
    		list = list->next;
    	}
    
    	return(0);
    }
    void addList(struct myList** current, int num){
    
    	struct myList* node=NULL, *head;
    	node = malloc(sizeof(struct myList));
    	
    	if(!node)
    		exit(1);
    	
    	node->data = num;
    	node->next = NULL;
    	
    	if(!*current){
    		*current = node;
    		return;
    	}
    	//preserve start of list
    	head = *current;
    	
    	//find tail of list
    	while((*current)->next){
    		*current = (*current)->next;
    	}
    	
    	//add node
    	(*current)->next = node;
    	
    	//restore head of list
    	*current = head;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you just post a solution then at least include a comment like:
    Code:
    /*This code was copied from http://cboard.cprogramming.com/c-programming/140820-append-queue.html*/
    And add an explanation of what you've changed so that the poster can learn from it.

    It could also pay to leave an obvious bug in there to be fixed. Oh wait nevermind

    At least in this case you've substituted a function that was capable of appending in O(1) time with one that appends in O(n) time. With any luck this will not be what was asked for and the student will have to adapt what you wrote.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  3. How to append bmp files to an EXE?
    By JoeDDDDYYY in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2003, 02:24 PM
  4. append val of int to a string
    By verryhi in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2002, 02:20 PM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 08:37 AM