c linked list question.

This is a discussion on c linked list question. within the C Programming forums, part of the General Programming Boards category; I have the following question. My code is given below. Two problems I'm having with is: 1.How can I link ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    c linked list question.

    I have the following question. My code is given below. Two problems I'm having with is:
    1.How can I link the alphabets without using the arrays? Maybe use a for loop?
    2.How to reverse the order of the linked list
    Using the following struct:
    typedef char DATA;
    struct node
    {
    DATA d;
    struct node * next;
    };

    Write a program to build a single linked list using a loop. Your list should be ordered so
    that the last item added is at the beginning of the list
    note: I did not use typedef in my code as I'm new to structures and it just confuses me at the moment

    Code:
    #include <stdio.h>
    #include <stdlib.h>	/*for malloc()*/
    #include <string.h>
    
    int main()
    
    {
    	struct node{
    	char d[2];
    	struct node *next;			/*pointer to next structure*/
    	};
    
    	char *node[26]={"A","B","C","D","E","F",
    					"G","H","I","J","K","L",
    					"M","N","O","P","Q","R",
    					"S","T","U","V","W","X",
    					"Y","Z"};
    
    	struct node *first_item;		/*pointer first_item to first structure*/
    	struct node *current_item;
    	struct node *new_item;
    	int index=0;
    
    	/*allocating memory for first structure*/
    	first_item=(struct node*)malloc(sizeof(struct node));
    	
    	/*creating first structure*/
    	current_item=first_item;
    
    	/*fill the structures*/
    	while(index<26)
    	{
    	strcpy(current_item->d,node[index]);
    
    	index++;
    	
    	if (index<26)
    		{
    
    		new_item=(struct node*)malloc(sizeof(struct node));
    		current_item->next=new_item;		/*set the address of new_item to pointer next*/
    		current_item=new_item;
    		}
    	else
    		{
    		current_item->next=NULL;
    		break;
    		}
    	
    	}
    
    	/*display the results*/
    
    	current_item=first_item;
    	index=1;
    	while(current_item)
    	{
    		printf("Structure %d: ",index++);
    		printf("%s\n",current_item->d);
    		current_item=current_item->next;
    	}
    
    
    
    	getchar();
    	return(0);
    }
    Last edited by bos1234; 03-21-2011 at 07:48 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by bos1234 View Post
    1.How can I link the alphabets without using the arrays? Maybe use a for loop?
    For ASCII, which is almost certainly what you want, the letter 'A' has a decimal value of 65. The letter 'B' is 66, 'C' is 67etc. That means 'B' = 'A' + 1, 'C' = 'A' + 2, etc. The character literals 'A', 'B', 'C', etc are just another way to say 65, 66, 67 in a C program. So you can use a loop to add a number to the letter 'A' and store it in each list node.

    2.How to reverse the order of the linked list
    The general idea is to traverse the list front to back, popping the first node off and sticking it on the front of a new list. You don't actually need two separate lists though, if you don't care about preserving the original list. To clarify, you have the list A..Z. First you remove the A, and stick it into an empty list. Then the B is in front. You remove that and stick it in front of the A. Then, remove the C from the front of the old list and stick it on the front of the new list. Keep doing this until the original list is empty.
    Last edited by anduril462; 03-21-2011 at 09:58 AM. Reason: typo and clarification

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by bos1234 View Post
    2.How to reverse the order of the linked list
    Why are you trying to reverse the array? Just store the new nodes at the beginning of the list as you create them. It's actually easier than storing new nodes at the end. (node->next = list; list = node; )
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21