Unless there's something that maintains a list of hosts (like a directory server) you have to check what's there now. And that means trying to talk to them over the LAN (that's what nmap does well).
You could write a script that generated the address for your subnet and ping'd them. You'd need the network (from ip address and subnet mask) to generate addresses. And they'd all need to reply to pings if you're to ping them. This link does that for network 192.168.1 (mask 255.255.255.0): http://www.cyberciti.biz/faq/mapping-lan-with-linux-unix-ping-command/
writing a script would be the best but the thing is that this script does
the listing of ip addresses,but I want a list of the hostnames..
Is there a way to change this script?