Solved

Example Linked List??

Posted on 2000-04-06
10
325 Views
Last Modified: 2013-12-14
Hi,
I am looking for some example linked lists. Is there any good ones about?
I'd like to see one that works in
co-ordination with a stack. Also one that canbe ordered on the basis of the users inputs, i.e the user can assign an ID each node to state which is next in the list?..
Thanks
0
Comment
Question by:Leo79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 8

Expert Comment

by:stochastic
ID: 2692268
Leo79,

I think you'll need to amplify your question. Do you want a practical example of using linked lists? Or do you want code that will support linked list creation with user input of IDs?

And when you say 'co-ordination with a stack', what do you exactly mean? If you specify exactly what you need, I think you will get a lot of responses.

- stochastic
0
 
LVL 2

Expert Comment

by:vijay_a73
ID: 2692321
Leo79,
  Your question is answerable only in parts. ie., when the linked lit is modelled as a stack we cannot assign id to the nodes and order them on some arbitrary merit. Remember a stack is a LIFO data structure.
  On the other hand we can design a linked list ordering the nodes on the basis of an id.
  Which one do you want?
-  vijay_a73
0
 

Expert Comment

by:euy
ID: 2692335
    Hi there Le079. I have here a code involving linked lists, with variable number of nodes, depending on user inputs. All you have to do is to compile this one (I used Visual Studio ver. 6.0), and run it to see the result. Here is the complete code.

#include<stdio.h>
#include<memory.h>
#include<string.h>
#include<stdlib.h>


typedef struct s_alm_dat AlmDat, *AlmDatPtr;
struct s_alm_dat {
      char Name[30];
      char Surname[30];
      int Age;
      char Status[2];
      AlmDatPtr next;
      AlmDatPtr Single_next;
};

AlmDatPtr headPtr, tailPtr;
AlmDatPtr SinglePtr, Single_tailptr;

void Input_Data();
void Display_Data();
void Display_Single_Only();
void Get_Single();



void main()
{
      Input_Data();
      Get_Single();
      Display_Data();
      Display_Single_Only();
}



void Input_Data()
{
      char name[30];
      char surname[30];
      char age[4];
      int age2;
      int cmp;
      char status[2];
      AlmDatPtr nodePtr;

      while(1)
      {
            printf("\n\nPlease enter name: ");
            gets(name);
            cmp = strcmp(name, "X");
            if(cmp==0)
                  break;

            printf("Please enter surname: ");
            gets(surname);
            printf("Please enter age : ");
            gets(age);
            age2 = atoi(age);
            printf("Please enter status : ");
            gets(status);

            nodePtr = (AlmDatPtr)malloc(sizeof(AlmDat));
            if (nodePtr == NULL){
                  printf("Error in allocation\n");
            }
            strcpy(nodePtr->Name, name);
            strcpy(nodePtr->Surname, surname);
            strcpy(nodePtr->Status, status);
            nodePtr->Age = age2;
            nodePtr->next = NULL;
            nodePtr->Single_next = NULL;

            if(headPtr == NULL)
            {
                  headPtr = nodePtr;
                  tailPtr = nodePtr; /* copy of the headPtr (from tail side) */
                                     /* head and tail both starts frm same address */
            }
            else
            {
                  tailPtr->next = nodePtr; /* by advancing tail, the head also advances */
                  tailPtr = nodePtr;  /* set the value of tail to the latest value again */
            }
      }
}



void Display_Data()
{
    AlmDatPtr workingPtr;
      int x;

      for(x=0; x<6; x++)
            printf("\n");

    workingPtr = headPtr;
    printf("\t\tName\t\tSurname\t\tAge\t\tStatus\n");
      printf("\t\t------------------------------------------------------\n");
    while(workingPtr != NULL){
        printf("\t\t%s\t\t%s\t\t%d\t\t%s\n", workingPtr->Name,
                      workingPtr->Surname,workingPtr->Age,workingPtr->Status);
        workingPtr = workingPtr->next;  
      }
      printf("\n\n");
}

void Display_Single_Only()
{
      AlmDatPtr workingPtr;

      workingPtr = SinglePtr;
      printf("\t\tName\t\tSurname\t\tAge\t\tStatus\n");
      printf("\t\t------------------------------------------------------\n");
    while(workingPtr != NULL){
            printf("\t\t%s\t\t%s\t\t%d\t\t%s\n", workingPtr->Name,
                      workingPtr->Surname,workingPtr->Age,workingPtr->Status);
        workingPtr = workingPtr->Single_next;  
      }
      printf("\n\n");
}



void Get_Single()
{
      AlmDatPtr workingPtr;

      workingPtr = headPtr;
      while(workingPtr != NULL)
      {
            if(workingPtr->Status[0]=='S')
            {
                  if(SinglePtr==NULL)
                  {
                        SinglePtr = workingPtr;
                        Single_tailptr = workingPtr;
                  }
                  else
                  {
                        Single_tailptr->Single_next = workingPtr;
                        Single_tailptr = workingPtr;
                  }
            }
            workingPtr = workingPtr->next;
      }
}



NOTE:
        When the program prompts for Status, you should input either S for single or M for married. Only these two inputs are accepted. And if you would want to exit the program, just type X when the prompt "Please enter name:" appears.
        Two sets of displays will appear afterwards, the first shows all the nodes of the linked list produced, while the second one shows the nodes whose status is S (for single) only.
        I hope this could help you. The routine which checks all nodes containing S status is similar to redirecting the tail of linked lists anywhere while traversing all nodes. So if you want you can modify this routine to fit your needs. Good luck and happy studying,....


euy



     
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Leo79
ID: 2696076
Hi again,

I am looking for a linked list that will be linked on the basis of an ID.
It doesn't have to sort the list. I mean i don't want it to be order alphabetically or numerically.
The next node will be the one specificied order by the <<linked to>> field.

Here's the structure i have it might make it a bit clearer.

struct links{
char ID[];
int input1;
int input2;
int result;
char linkedto[];
};

.. Also what i wanted to do was to added the numbers in Input1 and Input2. Assign the result to Result and push it to the next node using a stack.
Any examples similiar to this ??
Anything to do with stacks will be a help.

Thanks

0
 

Author Comment

by:Leo79
ID: 2696558
Hi again,

I am looking for a linked list that will be linked on the basis of an ID.
It doesn't have to sort the list. I mean i don't want it to be order alphabetically or numerically.
The next node will be the one specificied order by the <<linked to>> field.

Here's the structure i have it might make it a bit clearer.

struct links{
char ID[];
int input1;
int input2;
int result;
char linkedto[];
};

... Also what i wanted to do was to added the numbers in Input1 and Input2. Assign the result to Result and push it to the next node using a stack.
Any examples similiar to this ??
Anything to do with stacks will be a help.

Thanks



0
 

Expert Comment

by:tkirby052098
ID: 2701704
Are the ID fields always unique, or do the values repeat frequently?
Also, does the linkedto field contain the same values as the ID field?

When you say 'stack', that implies that not only do you want to add structures to the 'stack', but you also need to remove them too?  Would a dynamic array of structures do the job?
0
 

Author Comment

by:Leo79
ID: 2701856
The ID fields are always unique.
And the Linkedto field contains the same
values as those in the ID field.

The idea i had of the stack was to pass the output from one structure , as an input to the next structure. A dynamic array might help , but how would i get the output from one structure , to the next structure??
0
 

Expert Comment

by:tkirby052098
ID: 2701961
The reason I mentioned the array was since your linkto field does not actually point* to the next structure in the tree, you will have to search all of the records to find it.

The bigger your database becomes the longer it will take to add new records.  One of the advantages of using a linked list is that you don't need to traverse the entire database in order to add/delete a new record.

May I suggest:

struct links{
                    char ID[MAXIDLEN];  // need fixed size here!
                    int input1;
                    int input2;
                    int result;
                    struct links *pParent;  // do you need this?
                    struct links *pNext;     // pointer to next node
                    };

Then, keep an index of names and pointers, so that the user can select a name from a list, and you add the associated pointer to the linked list.

I believe you said this was a digital logic simulator, right?  So, you will be doing a time vs. signal propagation through the tree, right?  You might want to add a field in the links structure for propagation time, since this varies.  How do you handle a single input going to multiple gates?  Or what if the gates have different types of inputs, how do you know which input is which?

Food for thought:  your database is built around representing the various gates.  Perhaps it should represent the connection nodes instead, where a gate is a collection of connections and the relationships that define them.
0
 
LVL 1

Accepted Solution

by:
Vinay earned 100 total points
ID: 2704492
Hello,
I was working on linked list for a long period & we have created some nice examples. frankly speaking they r not real life ex. but pretty good to know power of linked list! In that example my idea was that use can specify the location (according to id) where next link has to  be added.Three options basically - At start , At End , Or after any Specified Id. I have logic as well as programme for this type of linked list. If this is ur requirement then please let me know ur email id & i will send u the complete code or EXE whatever u want! If this is not ur requirement then please specify in brief so that i can help u.
Bye....
0
 

Author Comment

by:Leo79
ID: 2705091
Hey Vinay,
Definately, that would be a great help.
My e-mail is : annamaria50@yahoo.co.uk
Can you send me all that you have.
Thanks
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question