Thread: c linked list question.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    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.

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, 04:05 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04: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, 10:33 PM