![]() |
|
#1
|
|||
|
|||
Help in C Print is not working with LinkListHello
i have this code that i am using struct to display student grade,and names but the problem that i can not print the content of my date using pointers i have done the two function any help to fix this error this is the code i could not contenu my assignment Write function void push(struct student*,char* ,int) that adds student’s data to the list Write function struct student pop(struct student*) that removes and returns the first student off the list Write function void print(struct student*) that prints information about all student. Write function int isThere(struct student*, char*) that will return true if the student is in the list, false otherwise. CPP / C++ / C Code:
Last edited by LuciWiz : 09-Mar-2006 at 01:59.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
|||
|
|||
Re: Help in C Print is not working with LinkListQuote:
What compiler/tools are you using that this code even compiles? You have two mallocs, but no frees. In your readStu (I have no idea why this isn't called readStudent) function, you are trying to return the global st1...yet another poor variable name. I don't see any reason why you need a global variable. Why return a global? It is already visible to the entire world. If this code is for some class, you need to go back and review structures, pointers and memory management before continuing any further. If you're just trying to learn C programming on your own, you've kind of have jumped into the deep end before learning how to swim. Take a look at an existing implementation of a single-ended linked list (usually just called a linked list). Basically, the studentList structure is representative of an element in the linked list. However, a complete LL implementation will have the necessary functions to return the head and tail of the list. In other words, you need another structure that contains an "element" pointer called head and another called tail. Since your element type is a studentList structure, I'd probably rename it to just a "Student" or lower-case if you must. Then, inside of your "other" structure called List (or even StudentList) you will probably want something that looks like: CPP / C++ / C Code:
Note that I made GPA a float and not an int. This may or may not be something of value to you. But if my GPA was 2.5, I'd certainly want to move up to a B, wouldn't you? I'd then create "list management" functions. These have nothing directly to do with student names and GPAs, but you do need to ensure that you can add list elements (Student structures) and remove list elements from the list without leaking memory. (Using malloc without a reciprocal free.) Once you can manage your list, then you will want to go about adding elements to it that have some reasonable value. In other words, setup a "Enter Student" kind of function that, when completed adds a Student structure to your list. Your list management functions should manage the "next" pointer inside of your Student (list element) structure. When there are no elements in the list, both of List's "head" and "tail" members should be NULL. I would probably choose to implement an "add Student list element to the list" function such that the Student list element was already initialized to whatever values I desired for it, so that the name and GPA were already set to the desired values (probably by a "create or new Student" function). Note that list management functions do not usually include the terms "push" and "pop" as these are generally associated with stack management functions. A stack is a first-in, last-out (FILO) kind of storage mechanism. Usually, one would want to implement "add," "remove," "head," "tail," "empty," etc...kind of functions for list management. The following code is a quick hack to give you a starting point of how to go about doing what I think you're trying to accomplish. It is by no means "production-ready" or useful for anything more than trying to get your head around some of the concepts of (singly) linked lists and how to implement the kinds of basic functionality that (hopefully) won't result in memory leaks. CPP / C++ / C Code:
There are still a lot of improvements that can/should be made to this code, including checking the return codes of the printStudentList and emptyStudentList functions. This is just a quick hack, and I can not really devote more than about 15 minutes to a problem such as this kind. I don't mean to be rude or anything, but your code formatting, quality and variable naming are in dire need of improvement. While you are certainly free to adopt your own style (and are under no obligation to use mine), I do recommend that you strongly consider evaluating elements of my style that make the code easier to read, understand, modify and maintain and apply any lessons learned to your own style. If you can, try to separate the work into small little chunks and have a function that does one little chunk at a time. For example, emptyStudentList uses removeStudentFromHeadOfList. While there may be more efficient ways of emptying the entire list (than using a function call per list element), this method is very easy to implement and debug. You can step through your code with a debugger and see the result of each step very plainly. Chances are that you won't have to worry about optimizing the code further, but if you ever do, it will be much easier because you already know how to remove one of the elements and free the student storage associated with that element...and you can verify that it works. Note that removeStudentFromHeadOfList also uses freeStudent. If you have a way of allocating a student on the heap, it is probably wiser to ensure that you have a function to deallocate the student so that you can pass any student pointers that you have to the deallocation function and deallocate them one-by-one. You can easily write a "chunk" deallocation function if needed. A good rule of thumb is to NEVER write a "malloc" without also writing a "free." If you expect to malloc more than one of an item, then you probably will want your "free items" to be in a function. By separating responsibilities of doing the work into several functions, it is much easier to add functionality a little bit at a time as you go. This enables you to test as you go. Without getting into methodologies and advanced test concepts, if you write a function and then test it, you can at least know that it can do what you want. Make each function as short and simple to read and understand and do not try to do too much in any one function. Your life will be easier and your code will be cleaner, clearer and much more maintainable and modifiable over time. And just so that you know, it is not difficult to "break" the code I posted above. All one would have to do is create a Student instance on the stack and pass the address of it to freeStudent. There is no good, portable way (at least none that I know of) to prevent it without doing a whole lot more work...and that is exceptionally rare to see even in production code. What this means is that even though it is very easy to break the code, there are often unspoken and unwritten "rules" to using code/functions. The clearer that you can make your's to understand, the more useful it will be to you and others with whom you may work. :davis: |
|
#3
|
|||
|
|||
Re: Help in C Print is not working with LinkListHeloo
Thanx for your advice but really i want to learn to write code in the you are writing it ;i mean the style is it standard |
|
#4
|
|||
|
|||
Re: Help in C Print is not working with LinkListQuote:
I'm not quite sure if you're asking a question or not, but "standard" is a matter of opinion among coders. It is very hard to get people to agree on anything much less something as individual-oriented as writing good code. Even my code isn't perfect. For example, I left in an unused integer declaration on line 209 in emptyStudentList. The error is benign, but I should have caught it. That's what happens when you toss together a quick hack in 15 minutes. Also, I barely tested it at all. There could be worse issues just under the surface. However, it appears to me that if you were to continuously add Students to the StudentList, that you could add reasonably any number and the system would "work," though there is nothing that it can even reasonably do except print the list. What is probably more appropriate is a doubly-linked list with some function pointers that encapsulate the allocation/deallocation of elements and the various list management operations necessary for robust list interaction, which is sorely lacking in this implementation. For example, there is no "search" method and there is no practical way to iterate backwards through the list...at least without modifying the allocation/deallocation such that block allocation was performed and then memory realloc'ated when necessary...then obtaining the tail and using pointer arithmetic, one could successfully navigate in reverse sequence through the list. Obviously, simpler is just adding a "prev" element pointer and modifying the list management functions to keep it up-to-date in a fashion very similar to way that both the "next" and the "head" and "tail" are done in the Student and the StudentList respectively. All of these "nice features" for the list exist outside of the "structure" of the list, even if the function pointers for element allocation/deallocation, searching, traversal, etc. were put into the list, the functions the function pointers pointed to would live outside of the list "proper." This is the basis for C++ classes and one of the reasons why C++ is so much better when appropriate. I don't really use C that much any more, but I learned it a fairly long time ago and the concepts behind a lot of it are still very valuable and useful today. Once you understand what a linked-list does and how it works "inside your head," it is easy to implement one in code--to the point of being nearly trivial to write. I'm also sure that someone could write a complete linked list-based application in a single main function. The code would be much more difficult to understand, maintain and debug and just about anyone coming across it would find it very difficult to modify should some new functionality be desired for the application. Regarding style, some people are perfectly happy putting three and four statements on a single line. Some don't mind writing their entire application inside of main. There are numerous guidelines and style guides for writing C (and C++). In reality, you're likely to have to discover a style that meets your coding needs and works within the coding projects that you're involved. :davis: |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| merging two sorted linklist problem | etymologist | C++ Forum | 3 | 10-May-2005 01:55 |
| working with html HELP | dopee | MySQL / PHP Forum | 10 | 18-Jan-2005 04:31 |
| Need help with 2-D array print out | crizzo | C++ Forum | 2 | 27-Jun-2004 22:36 |
| Guestbook error | BobbyDouglas | Web Design Forum | 1 | 16-Oct-2003 22:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The