Help With Linked Lists - Struggling to Get Started

  • Thread starter heavyc
  • Start date
In summary, the conversation revolves around creating and manipulating a linked list in C++6.0. The goal is to insert 10 random numbers ranging from 1-5 into the linked list, display the number of occurrences of each number, delete duplicate items, and finally display the sorted and unique linked list. The person is seeking help as they are having trouble starting the task due to difficulties with their teacher and using C++6.0. They have shared some code they have attempted to use.
  • #1
heavyc
16
0
I was wondering if anyone can help me with linked lists?
I have to create a linked list with 10 random numbers rangeing from 1-5 and insert them into a node in the appriote postion of a linearly linked list and display the number of times each number of occurences of each item. and i have to create a function that would delete the replicate items in the linked list and only leave one of the same numbers. and then display the linked list sorted and unique.

I am having trouble starting this out because my teacher sucks and the book is horrible. I have tried help from computer people but it is still not sticking. I am sure if someone can help me get started i can probably get the rest done but i have no idea to get this started because i have only used c++.net and the teacher wants it done in c++6.0 so that is some of the trouble i am having so if someone can help i would be grateful.
 
Physics news on Phys.org
  • #2
some code i think

#include <iostream>

using namespace std;

struct node {
int data;
node *next;
};

struct node *push_front ( node *list, int data )
{
node *p = new node;

p->data = data;
p->next = list;
list = p;

return list;
}

int main()
{
node *list = 0;
node *save;

for ( int i = 0; i < 10; i++ )
list = push_front ( list, rand() % 5 + 1 );

while ( list != 0 ) {
save = list->next;
cout<< list->data <<' ';
delete list;
list = save;
}

cout<<'\n';
}

can someone please check me on this and see if this is a good start...
 
  • #3


I understand that learning new concepts and skills can be challenging, especially when the resources available are not helpful. It is important to approach learning with patience and determination, and to seek out additional resources when needed. In this case, it may be helpful to look for online tutorials or guides on how to create and manipulate linked lists in C++6.0. Additionally, reaching out to classmates or joining a study group can provide valuable support and assistance. Remember to break down the task into smaller, manageable steps and to seek help when needed. With perseverance and the right resources, you will be able to successfully complete this task. Good luck!
 

1. What are linked lists and why are they useful?

Linked lists are data structures that store a collection of items in a linear order. Each item, also known as a node, contains a value and a pointer to the next item in the list. Linked lists are useful because they allow for efficient insertion and deletion of items, as well as dynamic memory allocation.

2. How do I create a linked list?

To create a linked list, you first need to define a node structure that contains a value and a pointer to the next node. Then, you can use this structure to create individual nodes and link them together using their pointers. Alternatively, you can use an existing linked list library or class provided by your programming language.

3. How do I insert an item into a linked list?

To insert an item into a linked list, you first need to find the correct position for the new item. This can be done by traversing the list and comparing the values of the nodes. Once you have found the correct position, you can create a new node with the desired value and link it to the rest of the list by updating the pointers of the neighboring nodes.

4. How do I delete an item from a linked list?

To delete an item from a linked list, you first need to find the node containing the item. Again, this can be done by traversing the list and comparing the values. Once you have found the node, you can update the pointers of the neighboring nodes to bypass the node to be deleted. Finally, you can delete the node and free up the memory it occupied.

5. What are some common challenges when working with linked lists?

Some common challenges when working with linked lists include efficiently searching for a specific item, handling empty lists, and avoiding memory leaks. It is also important to properly maintain the links between nodes when inserting or deleting items to avoid breaking the list. Additionally, linked lists may not be the most efficient data structure for certain operations, such as random access or sorting.

Similar threads

  • Introductory Physics Homework Help
Replies
12
Views
1K
  • Introductory Physics Homework Help
Replies
22
Views
966
  • Engineering and Comp Sci Homework Help
Replies
2
Views
830
Replies
1
Views
511
  • Introductory Physics Homework Help
Replies
3
Views
198
  • Introductory Physics Homework Help
Replies
17
Views
1K
  • Introductory Physics Homework Help
Replies
14
Views
832
  • Introductory Physics Homework Help
Replies
7
Views
1K
  • Science and Math Textbooks
Replies
2
Views
719
  • Introductory Physics Homework Help
Replies
2
Views
2K
Back
Top