C Doubly Linked List Program

I have been writing a C program to work with Doubly Linked Lists. But it does not work on Dev Cpp. Can anybody point out any error? It works until the part where I add a new record and then it crashes. I'm guessing there is a problem with the scanf() function.

//C double-linked list program struct stu { int roll,ph,ch,ma; stu *plink,*nlink; }; void addstu(stu *p,stu *n) { //add stu *s; int roll,ph,ch,ma; printf("HELLO STUDENT!\nYour roll no.? "); scanf("%d",&roll); printf("\nPhysics score? "); scanf("%d",&ph); printf("\nChemistry? "); scanf("%d",&ch); printf("\nMath? "); scanf("%d",&ma); s->roll=roll; s->ph=ph; s->ch=ch; s->ma=ma; s->plink=p; s->nlink=n; } void remstu(stu *s) { //remove stu *next; next->plink=s->nlink; free(s); } // Main function int main() { stu *s; int choice=0, ct=0; while(choice!=4) { printf("\n\t\tWELCOME TO RECORDS\n\n1. ADD\n2. REMOVE\n3. SEE\n4. EXIT\nEnter your choice(1-4): "); scanf("%d",&choice); switch(choice) { case 1: stu *t; addstu(s,t); s=t; free(t); ++ct; break; case 2: remstu(s); break; case 3: for(int i=0;i<ct;++i) { printf("\n\nRoll no. %d\nPhysics %d\nChemistry %d\nMath %d",s->roll,s->ch,s->ph,s->ma); s=s->plink; } break; case 4: exit(0); break; default: printf("Please enter correct choice(1-4)!"); } } return 0; }

-------------Problems Reply------------

s->roll=roll;

The pointer s in function addstu() is never initialized and dereferencing it leads to undefined behavior hence the crash.

Do

s = malloc(sizeof(struct stu)); /* Allocate memory */

I hope you have

typedef struct stu stu;

After reading your code, you have a lot of undefined behaviors going on.

Lets take case 1 as example:

stu *t;
addstu(s,t);
s=t;
free(t);
++ct;

The first problem is that when you call addstu the variable s is not initialized. Uninitialized (non-static) local variables have an indeterminate value, and will in practice seem to be random. The same goes for the variable t. So when in addstu you will set the nodes links to uninitialized pointers.

You call addstu which should create a new node, and add to the list. But what happens to that node you should create? Nothing, it's not passed on from the addstu function, the function doesn't even link it into the list properly (disregarding the uninitialized variables for a moment) so no new nodes will be added.

You then assign t to s, thereby initializing s but with an indeterminate value so nothing really changes.

You then continue to call free the memory for t (even though you haven't allocated any memory), and as s == t at that moment, that's the same as freeing s. Assignment of pointer only copies the pointer and not the memory that it points to.

There are probably many other problems with the code, but I only got that far. I suggest you start over, and with something simpler to learn how pointers and dynamic memory allocations work. Then come back to this problem, but before you write any code, try to draw the operations on paper. For example, what should an empty list look like? How would it look after adding the first node? After the second and third node? How would it look if you add a node to the end of the list? To the beginning of the list? Once you got all that clear, you can start writing your code.

Category:c# Views:0 Time:2018-09-13
Tags: linked list

Related post

  • Circular Doubly Linked List Program in Java (Homework Help) 2012-02-17

    Basically the program is supposed to create a "round table" of executives, with a chairman who cannot be changed. I kinda sorta almost know what I'm doing and I'm about halfway through my methods for inserting and removing executives, but I just trie

  • Do you use linked lists, doubly linked lists and so on, in business programming? 2009-06-22

    Are data structures like linked lists something that are purely academic for real programming or do you really use them? Are they things that are covered by generics so that you don't need to build them (assuming your language has generics)? I'm not

  • Doubly Linked List in a Purely Functional Programming Language 2009-12-04

    How does one go about doing doubly linked lists in a pure functional language? That is, something like Haskell where you're not in a Monad so you don't have mutation. Is it possible? (Singly linked list is obviously pretty easy). --------------Soluti

  • Doubly Linked Lists and Trees: To use or not to use? 2010-01-05

    I've always been taught that the more pointers you need in a piece of code, the less elegant it is. Also, I know that the only data structure that you 'need' is a singly linked tree. As a result, I've always tried my best to avoid such atrocities as

  • adding elements in to the doubly linked list 2010-05-30

    Hi this is my code for main class and doubly linked class and node class but when I run the program ,in the concole will show this"[email protected]" instead of the random numbers .please help me thanks! main class: public

  • Deleting nodes in a doubly linked list (C++) 2010-07-06

    I have problems understanding why when I create two or more nodes (as shown below), the function void del_end()will only delete the char name[20] and not the whole node . How do I fix this problem without memory leak? #include <iostream> using

  • Class for Doubly-Linked List 2010-07-23

    I have to code a class for handling a Doubly-Linked List that doesn't have an empty head or tail node. I have modified some code I obtained that is supposed to be for a Doubly-Linked List but it is throwing the following exception. How do I prevent t

  • Quickly searching a doubly linked list 2010-10-13

    I currently have a simple database program that reads keys in from a text file and stores them in a doubly linked list (values are read later if they are required). Currently, I do a sequential search on the list, but that is clearly rather slow. I w

  • Doubly Linked Lists Implementation 2011-01-04

    I have looked at most threads here about Doubly linked lists but still unclear about the following. I am practicing the Goodrich and Tamassia book in Java. About doubly linked lists, please correct me if I am wrong, it is different from a singly link

  • How do I insert a new node before first node of a doubly-linked list? 2011-03-15

    I am looking at how to insert a new node before the first node of a doubly-linked list. I'm getting confused with the auxiliary nodes required for this operation and the sequence of steps in which to perform the operation. I would be grateful for a h

  • Creating a doubly linked list from a list in OCaml 2011-04-27

    I am often told that using the Lazy module in OCaml, one can do everything you can do in a lazy language such as Haskell. To test this claim, I'm trying to write a function that converts a regular list into a static doubly linked list in ocaml. type

  • Help with doubly Linked List in C++ 2011-05-21

    so I am having a little trouble wrapping my head around this problem. I have created a doubly linked list called dlist (which contains a linknode helper class). I have another class called DeckOps that is used along with the linked list. dlist alread

  • How in C doubly linked list assigning pointer basic question 2011-07-11

    Sorry for asking a basic question, I am learning C and I got confused with assigning a value for a list. I got confused with my own question that pops into my head.. :( So for instance, I have a struct below typedef struct { int value_in_use; } struc

  • Differences implementing Singly vs Doubly linked list for the Multiplication of two polynomials 2011-09-15

    Whats the differences implementing Singly linked list or Doubly linked list for the Multiplication of two polynomials. And Especially I am trying for implementing program to multiply two polynomials using doubly linked list. An algorithm or a workabl

  • Doubly linked list, under the hood 2011-09-16

    Lets say you have 2 classes: - Node - Doubly linked list (DLL) Your main creates a bunch of nodes, who know something about themselves, like their name for example and then calls add(Node*) method of the DLL. In order to keep track of these Node poin

  • How to deep copy a Doubly Linked List in Java 2011-10-06

    I've been having quite a bit of trouble trying to make a deep copy of a doubly linked list in my java program. My method thus far is: public class DblLinkQueue implements Queue { public Object clone() { DblLinkQueue copy = null; Node currNode, tmp =

  • Deleting links in a doubly linked list 2011-10-15

    I am writing a doubly linked list based code in C. I had wrongly assumed that deleting the head node by doing free(head_node). And I could see the computer slowing down as the run progressed (which apparently is due to memory leak). I searched stacko

  • Free-ing a doubly linked list - hangs 2011-10-16

    I am trying to free a doubly linked list. This is the function I am using. static void clean_list(particles * plist) { particles *n_list = plist; particles *temp_nlist; while(n_list){ temp_nlist = n_list->next; free(n_list); n_list = temp_nlist; }

  • Doubly Linked List: Sorting nodes in ascending order 2011-11-01

    I'm working on a program using doubly linked lists. I have already gotten the functionality to insert nodes, remove nodes, retrieve the value from a node, display the list, output the size of the list, telling if the list is empty, and deleting all n

  • Using XOR to implement doubly linked list with one pointer per node 2011-12-29

    I'm having trouble with XOR doubly link list that has one pointer that must be contain NEXT and PREV pointers of nodes. I must xor address of pointers to do so, but I can't. I can allocate a memory address that XORed from two address but I can't get

  • Doubly Linked List - removal method 2012-03-06

    I am trying too delete a node in a doubly linked list, by an index that a user inputs. It seems to make sense for me but after "removing the node" and reprinting the list's contents, nothing has changed. I'm sure it's something stupid that I'm missin

  • Doubly linked list printing everything correctly but receiving segmentation fault at end of print function 2012-05-03

    Ok, so I'm almost finished with this program for my final project and I am receiving a segmentation fault... The program will do everything correctly, and it will print to the screen everything however it does not get out of the printWordLength() fun

  • Is a lock (wait) free doubly linked list possible? 2009-05-11

    Asking this question with C# tag, but if it is possible, it should be possible in any language. Is it possible to implement a doubly linked list using Interlocked operations to provide no-wait locking? I would want to insert, add and remove, and clea

  • doubly linked list illustration 2009-05-14

    I'm trying to illustrate a doubly linked list problem. this is from an old test i've been studying lately. The question is as follows: draw what the final linked after this code: ListNode n1 = new ListNode(); ListNode n2 = new ListNode(); ListNode n3

  • Understanding double pointer in doubly linked list in C 2009-07-16

    I have an exam tomorrow and I was trying to understand this doubly linked list example that the instructor placed on the class website but I'm having a hard time understanding a bit of it... Here's the code: #include <stdio.h> #include <stdl

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.121 (s). 11 q(s)