473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Move Pointer Back to the Beginning of a Single Linked List

7
Is there any simple way to move the next pointer back to the beginning of a single linked list after it travers thru the list?
Jul 26 '07 #1
4 8242
ravenspoint
111 100+
Set it equal to a pointer to the head of the list, which you have kept somewhere convenient.
Jul 26 '07 #2
gzeng
7
Set it equal to a pointer to the head of the list, which you have kept somewhere convenient.
Thanks for your instant reply.
How do I do it? In the following is the piece of code. At end I need to point head back to 10 not 6.

************************************************** ****************************
Expand|Select|Wrap|Line Numbers
  1. void main() {
  2.    node * curr, * head, *reversed_node, *sorted_node, *temp1, *temp2;
  3.    int i;
  4.  
  5.    head = NULL;
  6.  
  7.    //build up head: 10, 9, ..., 1
  8.    for(i=1;i<=10;i++) {
  9.       curr = (node *)malloc(sizeof(node));
  10.       curr->val = i;
  11.       curr->next  = head;
  12.       head = curr;
  13.    }
  14.  
  15.    // Delete a node with val 5
  16.    temp1 = head;
  17.  
  18.    // At end, temp1 is the node preceding the one with val 5
  19.    while (temp1->next != NULL & temp1->next->val != 5)
  20.           temp1 = temp1->next;
  21.  
  22.    cout << temp1->val << endl;      // print out 6
  23.  
  24.    // At end head points to 6 not 10!
  25.    while (head) {
  26.        if (head == temp1) {
  27.            head -> next = temp1->next->next;
  28.            break;
  29.        }
  30.  
  31.        head = head->next;
  32.   }
Jul 26 '07 #3
sicarie
4,677 Expert Mod 4TB
gzeng-

Please use code tags when you post (They're located on the right side of the page when you reply or start a thread). Thanks!
Jul 26 '07 #4
gzeng
7
I found the solution myself. At end, I added:

head = curr;


Thanks for your instant reply.
How do I do it? In the following is the piece of code. At end I need to point head back to 10 not 6.

************************************************** ****************************
Expand|Select|Wrap|Line Numbers
  1. void main() {
  2.    node * curr, * head, *reversed_node, *sorted_node, *temp1, *temp2;
  3.    int i;
  4.  
  5.    head = NULL;
  6.  
  7.    //build up head: 10, 9, ..., 1
  8.    for(i=1;i<=10;i++) {
  9.       curr = (node *)malloc(sizeof(node));
  10.       curr->val = i;
  11.       curr->next  = head;
  12.       head = curr;
  13.    }
  14.  
  15.    // Delete a node with val 5
  16.    temp1 = head;
  17.  
  18.    // At end, temp1 is the node preceding the one with val 5
  19.    while (temp1->next != NULL & temp1->next->val != 5)
  20.           temp1 = temp1->next;
  21.  
  22.    cout << temp1->val << endl;      // print out 6
  23.  
  24.    // At end head points to 6 not 10!
  25.    while (head) {
  26.        if (head == temp1) {
  27.            head -> next = temp1->next->next;
  28.            break;
  29.        }
  30.  
  31.        head = head->next;
  32.   }
Jul 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
8
by: Roman Mashak | last post by:
Hello, I'm studying linked lists and implemented the following: struct list_node { void *data; struct list_node *next; };
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.