You are here:

C++/problem with linklist

Advertisement


Question
QUESTION: hi

i have a problem in this code below.i want to write a linkedlist that "head" always points to the first node.
i mean that if in this code i enter 'a'&'b'&'c' the code print "a&b&c"for 'p->LastN' and 'a' for 'head->LastN'
can you help me whit this? or tell me what's my problem whit this code


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

struct node{

  char LastN[10];
   struct node*link;   
};
void main(){

  int i=0,j=0,n=0;

  struct node*p,*head;
   head=p;
  p=(struct node*)malloc(sizeof(struct node));
  

  printf("How many student do you want to enter?\n");
  scanf("%d",&n);

  for(i=0;i<=n-1;i++){

     printf("Enter LastName:\n");
      scanf("%s",&p->LastN[j]);
  }
   printf("p->LastN=%s , head->LastN=%s \n",p->LastN,head->LastN);
  
}


thanks
Bita

ANSWER: you need to allocate a separate node for each element in the linked list.
the link for every node should point to the next node; the link for the last node should be NULL.
head should point to the first node of the list.

also, main should return an int.

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

typedef struct node node ;
struct node
{

  char LastN[64] ;
  node* link ;
} ;

int main()
{
      int i = 0, n = 0 ;
      node* head = NULL ;
      node* p = NULL ;

      puts( "How many student do you want to enter?" ) ;
      scanf( "%d", &n ) ;

      for( i=0; i<n ; i++ )
      {
         if( head == NULL ) p = head = malloc( sizeof(node) ) ;
         else
         {
         p->link = malloc( sizeof(node) ) ;
         p = p->link ;
         }
         p->link = NULL ;
         printf( "Enter LastName:\n" ) ;
         scanf( "%63s", p->LastN ) ;
      }

  for( p = head ; p != NULL ; p = p->link )
         printf( "last name: '%s'\n", p->LastN ) ;

  return 0 ;
}


---------- FOLLOW-UP ----------

QUESTION: thanx for answering but i have a problem  when i enter c&b&a i wrote my problem in the code  

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

struct node{

  char LastN[10];
   struct node*link;   
};
void main(){

  int i=0,j=0,n=0,t=0;

  struct node*head=NULL;
  struct node*p=NULL;
  struct node*q=NULL;

  printf("How many student do you want to enter?\n");
   scanf("%d",&n);

  for(i=0;i<=n-1;i++)
  {
     if(head==NULL)

        p=head=(struct node*)malloc(sizeof(struct node));

     else
     {
        p->link=(struct node*)malloc(sizeof(struct node));
        p=p->link;

     }

     p->link=NULL;
     printf("Enter LastName:\n");
      scanf("%s",&p->LastN[j]);
  }

  t=n-2;
   
  q=head;

  

  
  

printf("\nhead->link->LastN:%s,head->link->link->LastN=%s\n",head->link->LastN,head->link->link->LastN);//in this line it prints head->link->LastN=b and head->link->link->LastN=a
         //but in the line below
         //when i compare those it should compares b and a
         //and we know that b>a and it should print **** for us but it doesn't
         //can you tell me why it doesn't print ****


if( head->link->LastN > head->link->link->LastN )
      printf("****\n");
}



ANSWER: in C, a string is an array of characters (memory containing a sequence of characters) ending in a NULL character (which has a numeric value of zero). In C, an array decays to a pointer to the first element, and therefore, a string is usually handled as a pointer of type char* or const char* (which holds the memory location of the first character of the string).

because C strings are pointers, you cannot use operators like '==' or '<' to compare strings. these would compare merely the addresses of memory where the strings are located, not the strings (contents of the memory locations) themselves.
to compare strings in C, use the function strcmp declared in <string.h>  
http://www.freebsd.org/cgi/man.cgi?query=strcmp&manpath=FreeBSD+7.0-RELEASE&form





---------- FOLLOW-UP ----------

QUESTION: thanx alot vijayan for your help,i used it and it works,but a question:
whats the diffrent between:

a[i]=b[j]

and

strcpy(a,b)
?

thanx
Bita


Answer
a[i]=b[j] ;

copies the char at position j in string b to the char at position i in string a.

strcpy( b, a ) ;

copies the char at position 0 in string b to the char at position 0 in string a.
then copies the char at position 1 in string b to the char at position 1 in string a.
then copies the char at position 2 in string b to the char at position 2 in string a.
and so on till a null (zero) char is copied.

a[i]=b[j] ; copies one character.
strcpy( b, a ) ; copies the entire string  (including the null char) char by char.

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

int main()
{
       char a[] = "abcdefghij" ;
       char b[ sizeof(a) ] = "0123456789" ;

       a[3] = b[7] ;
       printf( "%s\n", a ) ; /* a is now "abc7efghij" */

       strcpy( a, b ) ;
       printf( "%s\n", a ) ; /* a is now "0123456789" */

       return 0 ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.