Like Tree4Likes
  • 1 Post By MK27
  • 1 Post By AndrewHunter
  • 1 Post By MK27
  • 1 Post By stitches

Queue

This is a discussion on Queue within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> struct Node{ int Data; struct Node *Link; }; typedef struct Node *QueuePointer; void append(QueuePointer *Head,QueuePointer *Tail, int Num) ...

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

    Queue

    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 Serve(QueuePointer *Head, QueuePointer *Tail, int *item)
    
    {
    	QueuePointer Temp;
    	Temp=(QueuePointer)malloc(sizeof(struct Node));
    	if (Head == NULL)
    		printf("Queue is empty.  Cannot delete");
    	else
    	{
    		item->Head=Data;
    		Temp=Head;
    		Head=Head->Link;
    		if (Head == NULL)
    			Tail = NULL;
    		free(Temp);
    	}
    }
    
    
    void main()
    {
    	QueuePointer Tail,Head;
    	int x,choice;
    	int Num[5];
    	Tail=NULL;
    	clrscr();
    
    	printf("Enter [1] to add and [2] to delete: ");
    	scanf("%d",&choice);
    
    	if (choice==1);
    	{
    	for(x=1; x<6; x++)
    
    {
    	printf("Enter Values to Append: ");
    	scanf("%d",&Num[x]);
    }
    
    	for(x=0; x<5; x++)
    {
    	append(&Tail,&Head,Num[x]);
    	printf("\n%d",Num[x]);
    }
    	if(choice==2)
    {
    	for(x=0; x<5; x++)
    	{
    	void(Serve);
    	printf("Data Deleted");
    	}
    }
    
    getch();
    }
    }
    ok,please recorrect this,i cant managed to make this work...ive been working with this for 3 hours straight and cant manage to make it work,if its possible lesser complicated stuff,i cant really understand those terms that ya'll use...i learn through application,so if anyone can make this work...please help me :\ this is frustrating TY

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by stitches View Post
    i cant really understand those terms that ya'll use...i learn through application
    That better include learning to understand the terms, if you actually want to communicate with people. Also, if you understand the concept, it is easy to use the term referring to it. People who claim to "understand but not know the words" usually in fact misunderstand and may fool themselves by believing they "just do not know the words".

    Terminology is important, and it is about words and the meaning of words. For example, the difference between the declaration, definition, and initialization of a variable has a specific technical meaning that you will need to understand if you want to succeed at C programming. Hands-on (you use the term "through application") coding is a lot about words and meaning anyway (notice that we use languages made up, in part, of keywords. Not keynumbers or keyicons or keyideas). If you are resistant to wordage and language use you will not enjoy programming or get far with it.

    I am sure we all at least occasionally end up over our heads WRT terminology, and I know that when you first start with C and programming there are a lot of new terms everywhere, some cryptic and some innocuous. You need to learn them, or you will remain lost. Fortunately, words are the prime medium of human communication, so when you find one you do not understand, there are a number of ready available methods for you to deal with your problem.

    1) Look up the term on-line. If you google "C programming [word]" you will get material. Wikipedia is a good source for general programming terms, a lot of words there are disambiguated into separate articles to do specifically with computing.

    2) If after that you are still not satisfied with your understanding, ask a direct question. Do not be shy. "What does [word] mean?" is straightforward and you will get at least some straightforward answers

    Fair enough? I'm not just saying that because l'm long-winded, lol. I'm saying it because it is important. I've highlighted in green some important words/terms below. Be sure you know what they mean.

    Lets have a look at a couple of things that jump out right away in that code:

    Code:
    void main()
    Main() returns an int, whether you like it or not. This is not a serious issue in your program, but if you do not respect this convention you will hear the same thing over and over until you do. Since main() returns an int, you must do so in your code. Ie, a skeletal main() is:

    Code:
    int main(void) {
        return 0;
    }
    More serious is this:

    Code:
    	for(x=0; x<5; x++)
    	{
    	void(Serve);
    	printf("Data Deleted");
    	}
    That is not how to call a function in C. I think what you want is:

    Code:
    Serve(Head, Tail, i);
    This is still not going to fix a design flaw in the code, namely that the user can choose either 1 or 2, then the program ends. If I choose 2, there is no data to delete anyway.
    But first things first.

    Code:
    void Serve(QueuePointer *Head, QueuePointer *Tail, int *item)
    
    {
    	QueuePointer Temp;
    	Temp=(QueuePointer)malloc(sizeof(struct Node));
    	if (Head == NULL)
    		printf("Queue is empty.  Cannot delete");
    	else
    	{
    		item->Head=Data;
    		Temp=Head;
    You are treating an int pointer as if it were a pointer to a struct, and (further...) AFAICT you have no struct with a member "Head". You do have one with a member Data. But you do not have any variable named Data, so that is an assignment with an invalid right hand side (item->Head) and an invalid left hand side (Data).

    I suspect that you have gotten a little ahead of yourself with C, and are trying to modify/copy some existing code you've found (perhaps a tutorial), but without actually understanding the original. That's kind of a viscous circle, lol. I think you might want to try some simpler things until you understand basics better before you try to implement a queue. $0.02. If you want to keep going with this anyway, correct the things I've pointed out. Methinks they are certainly not the only problems, but they are a start.

    Finally, your indenting is very screwy. Have a look at this:

    Indent style - Wikipedia, the free encyclopedia

    Choose one of the styles there and imitate it. PLEASE. For your own good.
    Last edited by MK27; 09-11-2011 at 11:33 AM.
    AndrewHunter likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Why should we continue to help you when you don't take any of the advice given? This is getting old really quick.......
    CommonTater likes this.
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stitches View Post
    ok,please recorrect this,i cant managed to make this work...ive been working with this for 3 hours straight
    WOW... 3 whole hours? I've spent more time than that just thinking about code I need to write.

    No we're not going to correct it for you... that's your job not ours.

    and cant manage to make it work,if its possible lesser complicated stuff,i cant really understand those terms that ya'll use...i learn through application,so if anyone can make this work...please help me :\ this is frustrating TY
    No you don't learn by example... Nobody does and it's silly to claim you do. Handing you working code only makes you more prone to pull this particular bonar again... You learn C the same way everyone else learns... by deliberate study which you appear unwilling to do.

    I will agree with Andrew... you're sure burning up our willingness to help you in a big hurry. You don't listen to advice, you split threads for no reason, you demand we fix your mistakes... This is NOT how you get us on your side, my friend.
    Last edited by CommonTater; 09-11-2011 at 12:01 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by AndrewHunter View Post
    Why should we continue to help you when you don't take any of the advice given? This is getting old really quick.......
    @stitches: I notice in this other thread you say your English is not good. Unfortunately, fluent English is a very big advantage in computer programming of all kinds. You may want to look at this and run it thru google translate:

    On English and Programming

    I *not* am a xenophobic American, lol, but I have to agree with the author: For better or worse, English is the universal language of programming, and you will be beating your head against a wall resisting that. I am sure that is frustrating and you have my sympathies.

    Focus on reading and writing; in my experience it is easier to learn than speech, because you do not have to do it in real time or worry about pronunciation, etc. Probably this is something you can do "through application" while learning programming. Just be aware of the significance.
    AndrewHunter likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Code:
    	for(x=0; x<5; x++)
    	{
    	void(Serve);
    	printf("Data Deleted");
    	}
    Well that aint gonna do anything useful, and certainly isn't going to call the Serve function.
    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"

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Quote Originally Posted by AndrewHunter View Post
    Why should we continue to help you when you don't take any of the advice given? This is getting old really quick.......
    cause i dont understand a thing some people are saying? + its complicated seriously,when there are easier way why go for the complicated stuff?to make others look way too smart?

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Quote Originally Posted by MK27 View Post
    That better include learning to understand the terms, if you actually want to communicate with people. Also, if you understand the concept, it is easy to use the term referring to it. People who claim to "understand but not know the words" usually in fact misunderstand and may fool themselves by believing they "just do not know the words".

    Terminology is important, and it is about words and the meaning of words. For example, the difference between the declaration, definition, and initialization of a variable has a specific technical meaning that you will need to understand if you want to succeed at C programming. Hands-on (you use the term "through application") coding is a lot about words and meaning anyway (notice that we use languages made up, in part, of keywords. Not keynumbers or keyicons or keyideas). If you are resistant to wordage and language use you will not enjoy programming or get far with it.

    I am sure we all at least occasionally end up over our heads WRT terminology, and I know that when you first start with C and programming there are a lot of new terms everywhere, some cryptic and some innocuous. You need to learn them, or you will remain lost. Fortunately, words are the prime medium of human communication, so when you find one you do not understand, there are a number of ready available methods for you to deal with your problem.

    1) Look up the term on-line. If you google "C programming [word]" you will get material. Wikipedia is a good source for general programming terms, a lot of words there are disambiguated into separate articles to do specifically with computing.

    2) If after that you are still not satisfied with your understanding, ask a direct question. Do not be shy. "What does [word] mean?" is straightforward and you will get at least some straightforward answers

    Fair enough? I'm not just saying that because l'm long-winded, lol. I'm saying it because it is important. I've highlighted in green some important words/terms below. Be sure you know what they mean.

    Lets have a look at a couple of things that jump out right away in that code:

    Code:
    void main()
    Main() returns an int, whether you like it or not. This is not a serious issue in your program, but if you do not respect this convention you will hear the same thing over and over until you do. Since main() returns an int, you must do so in your code. Ie, a skeletal main() is:

    Code:
    int main(void) {
        return 0;
    }
    More serious is this:

    Code:
    	for(x=0; x<5; x++)
    	{
    	void(Serve);
    	printf("Data Deleted");
    	}
    That is not how to call a function in C. I think what you want is:

    Code:
    Serve(Head, Tail, i);
    This is still not going to fix a design flaw in the code, namely that the user can choose either 1 or 2, then the program ends. If I choose 2, there is no data to delete anyway.
    But first things first.

    Code:
    void Serve(QueuePointer *Head, QueuePointer *Tail, int *item)
    
    {
    	QueuePointer Temp;
    	Temp=(QueuePointer)malloc(sizeof(struct Node));
    	if (Head == NULL)
    		printf("Queue is empty.  Cannot delete");
    	else
    	{
    		item->Head=Data;
    		Temp=Head;
    You are treating an int pointer as if it were a pointer to a struct, and (further...) AFAICT you have no struct with a member "Head". You do have one with a member Data. But you do not have any variable named Data, so that is an assignment with an invalid right hand side (item->Head) and an invalid left hand side (Data).

    I suspect that you have gotten a little ahead of yourself with C, and are trying to modify/copy some existing code you've found (perhaps a tutorial), but without actually understanding the original. That's kind of a viscous circle, lol. I think you might want to try some simpler things until you understand basics better before you try to implement a queue. $0.02. If you want to keep going with this anyway, correct the things I've pointed out. Methinks they are certainly not the only problems, but they are a start.

    Finally, your indenting is very screwy. Have a look at this:

    Indent style - Wikipedia, the free encyclopedia

    Choose one of the styles there and imitate it. PLEASE. For your own good.
    lemme get on that ill try my best

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    WOW... 3 whole hours? I've spent more time than that just thinking about code I need to write.

    No we're not going to correct it for you... that's your job not ours.



    No you don't learn by example... Nobody does and it's silly to claim you do. Handing you working code only makes you more prone to pull this particular bonar again... You learn C the same way everyone else learns... by deliberate study which you appear unwilling to do.

    I will agree with Andrew... you're sure burning up our willingness to help you in a big hurry. You don't listen to advice, you split threads for no reason, you demand we fix your mistakes... This is NOT how you get us on your side, my friend.
    kfine thnx smart guy

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    thanks for the BIG HELP pfffffffft

  11. #11
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You obviously think we owe you something.

    You are wrong.
    Code:
    while(!asleep) {
       sheep++;
    }

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Tell you what Stitches ... I can't speak for all of us, but I'm betting most would agree...

    We are not talking down to you and we are not deliberately talking over your head. We are making the most often true assumption that you have at least enough knowledge to write "hello world" successfully... Questions are not a problem... simply ignoring the answers is.

    If there is a language problem, you will find reasonable patience.
    BUT .. If you don't understand you need to SAY you don't understand.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    sorry im caught up with other things in real life + my education
    MK27 likes this.

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. Queue Help
    By smhillis in forum C Programming
    Replies: 4
    Last Post: 08-03-2009, 06:11 PM
  3. Help with Queue
    By blork_98 in forum C Programming
    Replies: 2
    Last Post: 02-24-2006, 04:31 PM
  4. I need a queue
    By Flex in forum C Programming
    Replies: 3
    Last Post: 03-08-2002, 03:14 PM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21