LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2012, 04:05 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
linklist


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

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

int main()
{
    /* This won't change, or we would lose the list in memory */
    struct node *empinfo;
    /* This will point to each node as it traverses the list */
  //  struct node *conductor;

    empinfo = malloc( sizeof(struct data) );
    empinfo->next =     NULL;

 empinfo->x = 12;


}
this is giving errors
 
Old 07-23-2012, 04:19 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Fix them. For start:

Code:
typedef struct node {
  int x;
  struct node *next;
} node;

Last edited by NevemTeve; 07-23-2012 at 04:20 AM.
 
Old 07-23-2012, 04:32 AM   #3
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
oppss...tht was a silly one.didnt took a look at it..sorry
 
Old 07-23-2012, 06:54 AM   #4
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct data {
    char empname[20];
    int empID;
    struct data * next;

};

//typedef struct list_el item;

 main() {

    int i=0;
    char input[20];

    struct data *head =NULL;
    struct data *prev, *curr;
    //puts("enter employee name:");
    //while(gets(input)!=NULL && input[0] !='\0')
        for(i=0 ;i<10 ; i++)
        {


        curr = (struct data *)malloc(sizeof(struct data));
        puts("name:");
        gets(input);
    if(head == NULL)
        head =curr;
    else
    prev->next =curr;

    curr->next =NULL;
    strcpy(curr->empname,input);
    puts("enter empid:");
    scanf("%d",&curr->empID);

    while(getchar() !=  '\n')
        continue;
    puts("enter next name:");
if(head ==NULL)
    printf("No data entered");
else

    printf("names are");
    curr =head;


while(curr !=NULL)
{




    printf("name is :%s and id is %d \n",curr->empname, curr->empID);
    curr= curr->next;
}


curr=head;
while (curr !=NULL)
{
    free(curr);
    curr =curr->next;
}

return 0;
}

can we use scanf instead of gets in this code
if yes then pls help
 
Old 07-23-2012, 08:03 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
what kind of error you got? how do you compile?
 
Old 07-23-2012, 08:04 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
's nice. Any questions?

Note: Never ever use gets. Didn't your compiler warn you? Use these flags: -W -Wall -Wextra -pedantic

Last edited by NevemTeve; 07-23-2012 at 08:06 AM.
 
Old 07-23-2012, 08:21 AM   #7
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
's nice. Any questions?


Note: Never ever use gets. Didn't your compiler warn you? Use these flags: -W -Wall -Wextra -pedantic
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct data {
    char empname[20];
    int empID;
    struct data * next;

};

struct data *list;
//typedef struct list_el item;

 main() {

    int i=0;
    char input[20];
    int id_number;
    struct data *head =NULL;
    struct data *first, *last,*var;
    char name[20];
//  for(i=0 ;i<2 ;i++)
//  {
new = (struct data *)malloc(sizeof(struct data));
    if(!new){
        printf("malloc failed");
        return -1;
        }

    printf("enter the name:");
    scanf("%s" , &name);

    strcpy(new->empname,name);

    printf("enter the id");
    scanf("%d", &id_number);



    new->empID =id_number;
    new->next =NULL;

   printf("name is %s\n" ,new->empname);
    printf("Employee id is: %d\n" ,new->empID);



}

now i wnt to add a new node to this ..pls tell me how can i do tht?????????????
 
Old 07-23-2012, 08:30 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
tthebegning or totheend?

What variables head, first, last and var are supposed to do?

Last edited by NevemTeve; 07-23-2012 at 08:32 AM.
 
Old 07-23-2012, 08:38 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by batman4 View Post
now i wnt to add a new node to this ..pls tell me how can i do tht?????????????
Spell out your words. And you do this by reading your textbook, and learning. This sounds very much like homework.

Show us what you've done/tried so far, and what error(s) you're getting, and we'll be glad to help.
 
Old 07-23-2012, 08:39 AM   #10
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
tthebegning or totheend?

What variables head, first, last and var are supposed to do?
]


have to add a node from begining
these variables are use to add only'
if u dnt wnt to use it wont b a prob
 
Old 07-23-2012, 08:43 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In another topic of yours I have already showed you some come, haven't you seen it?
http://www.linuxquestions.org/questi...0/#post4732580

add-to-end:
Code:
newelement->next= NULL;
if (!head) {
    head= newelement;
} else {
    tail->next= newelement;
}
tail= newelement;
add-to-start:
Code:
newelement->next= head;
if (!head) {
    tail= newelement;
}
head= newelement;
delete-from-end:
Code:
if (!head) { /* do nothing */
} else if (!head->next) {
    head= tail= NULL;
} else {
    prev= head;
    act= head->next;
    while (act->next) {
       prev= act;
       act= act->next;
    }
    prev->next= NULL;
    tail= prev;
}
delete-from-start:
Code:
if (head) {
    head= head->next;
    if (!head) {
        tail= NULL;
    }
}

Last edited by NevemTeve; 07-23-2012 at 09:02 AM.
 
2 members found this post helpful.
Old 07-23-2012, 09:10 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In 1975, I figured out how to do this all by myself ... without an Internet or anything else to help me but .

Go and do likewise. Or, stop pretending and get out of the business
 
4 members found this post helpful.
Old 07-23-2012, 11:57 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by batman4 View Post
]
have to add a node from begining
these variables are use to add only'
if u dnt wnt to use it wont b a prob
Once more, SPELL OUT YOUR WORDS. It's "you", not "u", etc......if you can't be bothered typing out full words, why should we be bothered typing out a reply?

NevemTeve...well done. You are more generous than I'd be; I wouldn't do homework for someone.
 
1 members found this post helpful.
Old 07-24-2012, 01:05 AM   #14
batman4
Member
 
Registered: Jul 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Once more, SPELL OUT YOUR WORDS. It's "you", not "u", etc......if you can't be bothered typing out full words, why should we be bothered typing out a reply?

NevemTeve...well done. You are more generous than I'd be; I wouldn't do homework for someone.
thanks ..n it was not a homework
yesterday i left early
n didnt implemented it
now i have written my program.
and NevemTeve thanku soo much for your help.i dint wanted to stop myself from studying c .And people like you can help is going further

i m learning c programming.
so to push myself i am using this forum.
 
Old 07-24-2012, 09:46 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by batman4 View Post
thanks ..n it was not a homework
yesterday i left early n didnt implemented it now i have written my program.
and NevemTeve thanku soo much for your help.i dint wanted to stop myself from studying c .And people like you can help is going further

i m learning c programming. so to push myself i am using this forum.
Great..now push yourself to SPELL OUT YOUR WORDS...are you missing that?? Quit with saying "dint", "n", etc.

If it wasn't homework, what were you trying to do with such a simple program?? And since you have written it, why don't you post it here (as you were asked to before), so everyone can benefit from it?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
en-queue linklist in c batman4 Programming 4 07-20-2012 11:53 AM
sorted linklist function harrylee2003 Programming 2 08-05-2006 11:04 PM
problem with linklist vijeesh_ep Programming 6 07-31-2004 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration