SHAREiDEAS is an exclusive blog for tech scholars where we will discuss the various topics concerned with the development of tech knowledge,besides various presentations,experiences,and tech talks can also be done
Monday, 1 October 2012
HCL Analytical
Q1. The question shows a pair of words in which the first is
related to the second in some way. It
is followed by a single word which bears a similar relation
to one of the given alternatives. Find
the correct alternative to complete the analogy.
Melt:Liquid::Freeze: ?
a. Ice
b. Condense
c. Solid
d. Crystal
Q2. Fill in the blankGuilt is to Past as Hope is to ......
a. Present
b. Future
c. Today
d. Despair
e. Hopeless
Q3. From the given choices select the odd man out:
a. Bird
b. Kite
c. Crow
d. Pigeon
e. Sparrow
Q4. Find the missing pattern
BOQD : ERTG :: ANPC : ?
a. DQSF
b. FSHU
c. SHFU
d. DSQF
Q5. Find the missing number
5 : 24 :: 8 : ?
a. 65
b. 63
c. 62
d. 64
Q6. From the given choices select the odd man out
a. DFHEG
b. TWXUV
c. OQSPR
d. JLNKM
Q7. If HARD is coded as 1357 and SOFT is coded as 2468, what
will 21448 stand for?
a. SHAFTb. SHORT
c. SHOOT
d. SHART
Q8. Find the next number in the series
1, 6, 13, 22, 33, ......
a. 44
b. 45
c. 46
d. 47
Q9. The question contains some statements followed by some conclusions.
Decide which of the
given conclusions logically follow from the given
statements, disregarding commonly known
facts.
Statements:
I) All tomatoes are red.
II) All grapes are tomatoes.
Conclusions:
I) All grapes are red.
II) Some Tomatoes are grapes.
a. Only conclusion I follows.
b. Only conclusion II follows.
c. Neither I nor II follows
d. Both I and II follow.
e. Either I or II follows
Q9. Old woman's son is my daughter's uncle, then what
relation has the old woman to me ?
a. Sister
b. Mother
c. Grandmother
d. Mother - in - law
Q10. Ramu was facing East.He walked 4 km forward and then
after turning to his right walked 6
km. Again he turned to his right and walked 7 km. After this
he turned back.Which direction was
he facing at the time?
a. East
b. North
c. South
d. West
e. North-East
Q11. Raman is not wearing purple and Aman is not wearing
black.Raman and Sahil wear different
colours.Avinash alone wears green.What is Sahil's colour, if
all four of them are wearing different
colours.
a. Green
b. Black
c. Purple
d. Can't Say
Q12. The question is followed by two arguments numbered I
and II. You have to decide which of
the arguments is a strong argument and which is a weak
argument.
Statement :Should a total ban be put on trapping wild
animals?
Arguments :
I. Yes, Trappers are making a lot of money.
II. No, bans on hunting and trapping are not effective.
a. Only argument I is strong.
b. Only argument II is strong.
c. Either I or II is strong.
d. Neither I nor II is strong.
e. Both I and II are strong
Q13. The question contains a statement followed by two
Assumptions I and II. Find out which
assumption(s) is implicit.
Statements:
Vitamin E tablets improve circulation, keep your complexion
in glowing condition.
Assumptions :
I. People like glowing complexion.
II. Complexion becomes dull in the absence of circulation.
a. Only assumption I is implicit
b. Only assumption II is implicit
c. Both assumption I and II are implicit.
d. Neither assumption I nor II is implicit
Q14. Study the statement(s) and the conclusions and select
the correct option.
Statement :
No country is absolutely self - dependent these days.
Conclusions :
I. It is imposible to grow and produce all that a country
needs.
II. Countrymen in general have become lazy.
a. Only Conclusion I follows
b. Only Conclusion II follows
c. Both the conclusion I and II follow
d. Either conclusion I or II follows
e. Neither conclusion I nor II follows
Q15. In the question a statement is followed by some courses
of action . A course of action is a
step or administrative decisions to be taken for
improvement, follow-up, or further action in
regard to the problem, policy, etc.You have to assume
everything in the statement to be true and
then decide which of the given suggested course(s) of action
logically follows for pursuing.
Statement:
'The World Bank has approved a $ 300 million loan to finance
a project to construct coal ports by Madras
Port Trusts.
Courses of Action :
I. India should take financial help from other international
financial institutions to develop such ports in
other places.
II. India should not seek any help from the international
financial institutions.
a. Only I follows
b. Only II follows
c. Either I or II follows
d. Neither I nor II follows
e. Both I and II follo
HCL Prepatory Questions Part 2
1. Meaning of SHABBY ?
2. log value of 1/49 base 7.?
3. Value of (1+3+5....+3983)/1992 ?
4. The least perfect square number which is also factor of 7936 ?
Ans) None of these. I cant remember options.
5. Pipe.A full the tank thrice as Pipe B. If two combinedly fill the tank in 36 mins. What s the time taken to fill the tank by B alone?
6. The value of 25^1.5 ?
7. Synonym of traditional?
http://www.ChetanaS.org
8. Mohan sells the bike worth Rs.10000 to ragu for profit 10%.Later a month Ragu sells the bike to mohan for loss of 10% ?Which of the following is correct?
a. mohan loses 100
b. mohan gains 100
c. mohan gains 1000
d. mohan gains 1100
9. A company can give ads in three ways by 3 magazines, 2 tv, 2 radio. But the company chooses 2 magazines 1 tv, 1 radio for ads. Therefore how many ways company gave ads?
10. A bag consists of three blue balls and three green balls. Two balls are taken at random. whats the probability the two of the balls taken are same colour?
11. One rupee coin was tossed thrice. whats the probability that first will be tails second and third will be heads?
12. A B C made investment in a bank in the ratio of 1:3:5.After a year A invests double the amount of previously invested, B and C invested half of the amount invested before. Then what is the ratio now.
13. In a language if FORMAT is coded as IOUMDT then BUSINESS is coded as?
14. 84:134::38:?
15. WOLLS:XPMMT::QGIIT:?
16. The total no of prime factors in (4*2)^34 (7*9)^24 (24)^15 is ?
Sequence the words given below in a logical way such as size, nature, relations etc
17. room, key, door, lock, light-on
18. atomic structure, chemistry, electron, bohr’s theory
http://www.ChetanaS.org
19. A man points a lady and told “her only brother’s son is the brother of my wife”. What way the man related to the lady?
2. log value of 1/49 base 7.?
3. Value of (1+3+5....+3983)/1992 ?
4. The least perfect square number which is also factor of 7936 ?
Ans) None of these. I cant remember options.
5. Pipe.A full the tank thrice as Pipe B. If two combinedly fill the tank in 36 mins. What s the time taken to fill the tank by B alone?
6. The value of 25^1.5 ?
7. Synonym of traditional?
http://www.ChetanaS.org
8. Mohan sells the bike worth Rs.10000 to ragu for profit 10%.Later a month Ragu sells the bike to mohan for loss of 10% ?Which of the following is correct?
a. mohan loses 100
b. mohan gains 100
c. mohan gains 1000
d. mohan gains 1100
9. A company can give ads in three ways by 3 magazines, 2 tv, 2 radio. But the company chooses 2 magazines 1 tv, 1 radio for ads. Therefore how many ways company gave ads?
10. A bag consists of three blue balls and three green balls. Two balls are taken at random. whats the probability the two of the balls taken are same colour?
11. One rupee coin was tossed thrice. whats the probability that first will be tails second and third will be heads?
12. A B C made investment in a bank in the ratio of 1:3:5.After a year A invests double the amount of previously invested, B and C invested half of the amount invested before. Then what is the ratio now.
13. In a language if FORMAT is coded as IOUMDT then BUSINESS is coded as?
14. 84:134::38:?
15. WOLLS:XPMMT::QGIIT:?
16. The total no of prime factors in (4*2)^34 (7*9)^24 (24)^15 is ?
Sequence the words given below in a logical way such as size, nature, relations etc
17. room, key, door, lock, light-on
18. atomic structure, chemistry, electron, bohr’s theory
http://www.ChetanaS.org
19. A man points a lady and told “her only brother’s son is the brother of my wife”. What way the man related to the lady?
HCL Prepatory Questions Part 1
logics and Techniques
__________________
1)How many segment registers are there in the
8086 processor?
a) 4 b) 6 c) 8 d) none
Ans: a
2. What is the addressing mode of this instruction MOV AL, [BX];
a) direct addressing mode
b) register indirect addressing mode
ANS: b I am not sure of it.
3. What is the type of file system used in the CD ROM?
a) VFAT B)
4) About CPU I think but answer is DMA.
5) If we double the clock speed, then?
a) It increases the speed of the processor b) It increases the speed of the system bus c) both of the above D) None
6) Data recovery is done in which layer?
a) physical b) datalink c) network d) transport
7) What is thrashing?
ANS: swapping in and out the pages frequently from memory.
8) By using 3 nodes how many trees can be formed?
ANS:5
9) They give one tree, and ask the post order traversal for that tree?
ANS:C
10) Page cannibalation is?
ANS:C
Aptitude section:
1) They give one scenario and ask questions on that. that is easy. Those are 5 questions.
2) They ask 2 questions in English, I didn't answer those ones. They are - Find the odd one?
3) They give 2 questions on missing digits. They r matrix type, I didn't remember the questions, But I know answers.
for first one 8. For second one 28 is the answer.
4)Two persons start walking from the same place in opposite directions. After walking for 4 mts, both of them take the left and walk for another 3 mts. Then
what is the distance b/w them?
Ans:10 mt.
5)One person start from his home towards college which is 53 km far away. Another person started from college towards home after an hour. the speed of first one is 4kmph and the second one is 3 kmph. Then, what is the distance from home to their meeting point?
AND:21 km.
6)3 machines can complete the work in 4,5, and 6 hours respectively. due to power failures they did the work alternatively. Then what is time taken to complete the work?
ANS:9/20
7)Two persons take the pair of dies and throws them. If 12 appers first one wins, If two consecutives 7 s appear then second one wins. What is the probability to
win first one in the game?
a)6/15 b)3/13 c)2/13
a) 4 b) 6 c) 8 d) none
Ans: a
2. What is the addressing mode of this instruction MOV AL, [BX];
a) direct addressing mode
b) register indirect addressing mode
ANS: b I am not sure of it.
3. What is the type of file system used in the CD ROM?
a) VFAT B)
4) About CPU I think but answer is DMA.
5) If we double the clock speed, then?
a) It increases the speed of the processor b) It increases the speed of the system bus c) both of the above D) None
6) Data recovery is done in which layer?
a) physical b) datalink c) network d) transport
7) What is thrashing?
ANS: swapping in and out the pages frequently from memory.
8) By using 3 nodes how many trees can be formed?
ANS:5
9) They give one tree, and ask the post order traversal for that tree?
ANS:C
10) Page cannibalation is?
ANS:C
Aptitude section:
1) They give one scenario and ask questions on that. that is easy. Those are 5 questions.
2) They ask 2 questions in English, I didn't answer those ones. They are - Find the odd one?
3) They give 2 questions on missing digits. They r matrix type, I didn't remember the questions, But I know answers.
for first one 8. For second one 28 is the answer.
4)Two persons start walking from the same place in opposite directions. After walking for 4 mts, both of them take the left and walk for another 3 mts. Then
what is the distance b/w them?
Ans:10 mt.
5)One person start from his home towards college which is 53 km far away. Another person started from college towards home after an hour. the speed of first one is 4kmph and the second one is 3 kmph. Then, what is the distance from home to their meeting point?
AND:21 km.
6)3 machines can complete the work in 4,5, and 6 hours respectively. due to power failures they did the work alternatively. Then what is time taken to complete the work?
ANS:9/20
7)Two persons take the pair of dies and throws them. If 12 appers first one wins, If two consecutives 7 s appear then second one wins. What is the probability to
win first one in the game?
a)6/15 b)3/13 c)2/13
Saturday, 29 September 2012
Wipro Interview Experience-Nairita Biswas
Wipro interview
experience
By god grace I was lucky enough to directly go to the
interview round without giving the written test.
In the interview :
Me: good morning
Interviewer: good
morning. Sit down.
Interviewer
(looking at my resume and marksheet): tell me about self
me : My name is
Nairita Biswas.. I am from Bikaner,Rajasthan.. I am currently pursuing BTech in
IT from sathyabama University with an aggregate of…
I did my schooling frm XYZ school with an average of..
Told about ur hobbies,family background and
Cuming from a small town my aim is to achieve big in life..
Interviewer: how
big??
Me: if u give me
a this job then I want to b the face behind the success of your company.
Interviewer was
looking at my resume and said u r the only gal I have met who loves cricket( as
I have mentioned in my resume)..he asked me who is your favourite cricketer?
Me (smiling):
Sachin tendulkar.. and I added that
nt only me my frnd is also crazy
abt cricket.
Interviewer: what
is internet? What is network? What are the types of network? What is
polumorphism?
Me : told them
the basic definition
Interviewer: r u
good in programming?
Me: yes sir
Interviewer: can
u any program in “C”?
Me: yes sir
Interviewer: dat
means u r good in programming?
Me : yes sir
..(confidently)
Interviewer: what
r ur weakness??
Me: told
After dat he asked me whether I accept the 1 yr bond or nt
and m I flexible to all the places..
He told me abt the training process..and dats all..
In the interview u have to be confident .that is really vry
important ..so all the best guyz..
Cognizant Interview Experience of Sandeep Raghuvanshi
Name: Sandeep Raghuvanshi
Company: Cognizant Technology Solutions.
Date: 14th September,2012
First we had an aptitude paper on 13th,Questions were mainly on reasoning and Verbal Ability..about 1500 students appeared of whom nearly 550 were shortlisted..
Next Day we had HR interview...My interview duration was around 35 minutes
Myself:Gud Morning Sir
HR:you must be Sandeep..(Shook hands)...plz sit down
Myself:thnx
He didnt ask me anything lyk Tell Me about urself and directly went to Technical question
HR:What is Linked List..???
Myself:Answered
HR:its Types???
Name: Sandeep Raghuvanshi
Company: Cognizant Technology Solutions.
Date: 14th September,2012
First we had an aptitude paper on 13th,Questions were mainly on reasoning and Verbal Ability..about 1500 students appeared of whom nearly 550 were shortlisted..
Next Day we had HR interview...My interview duration was around 35 minutes
Myself:Gud Morning Sir
HR:you must be Sandeep..(Shook hands)...plz sit down
Myself:thnx
He didnt ask me anything lyk Tell Me about urself and directly went to question
HR:Wha Linked List
Myself:Answered
HR:Types???
Myself:Answered
HR:Difference and Practical Scenarios when they are used??
Myself:Answered
HR:What is ER Model??
Myself:Answered
HR:What makes you think that you are a good programmer if you know java??
Myself:Answered advantages of Java
HR:How Exceptions are thrown and Handled in Java??
Myself:Answered
HR:Whats your project???
Myself:Answered
HR:Explain...??
Myself:Answered
HR:What languages u know???
Myself:Answered
HR:your Family???
Myself:Answered
HR:Accomplishments????
Myself:Showed Certificates and the Website i created..
HR:Long Term Goals??
Myself:Answered
HR:I dont lyk your Tie...plz Impress me??(i thought its a joke...but answered)..later realised it was stress ques..
HR: Do you think Software field will give you evrything u want in life???
Myself:Yes Sir....i am driven by passion to create S/w's Blah Blah Blah...
HR:Well it was lovely talking to you,u have any doubts...
Myself:Answered
Stood up(both of us) and Shook hands....i knew i had made it
Later i was selected
Company: Cognizant Technology Solutions.
Date: 14th September,2012
First we had an aptitude paper on 13th,Questions were mainly on reasoning and Verbal Ability..about 1500 students appeared of whom nearly 550 were shortlisted..
Next Day we had HR interview...My interview duration was around 35 minutes
Myself:Gud Morning Sir
HR:you must be Sandeep..(Shook hands)...plz sit down
Myself:thnx
He didnt ask me anything lyk Tell Me about urself and directly went to Technical question
HR:What is Linked List..???
Myself:Answered
HR:its Types???
Name: Sandeep Raghuvanshi
Company: Cognizant Technology Solutions.
Date: 14th September,2012
First we had an aptitude paper on 13th,Questions were mainly on reasoning and Verbal Ability..about 1500 students appeared of whom nearly 550 were shortlisted..
Next Day we had HR interview...My interview duration was around 35 minutes
Myself:Gud Morning Sir
HR:you must be Sandeep..(Shook hands)...plz sit down
Myself:thnx
He didnt ask me anything lyk Tell Me about urself and directly went to question
HR:Wha Linked List
Myself:Answered
HR:Types???
Myself:Answered
HR:Difference and Practical Scenarios when they are used??
Myself:Answered
HR:What is ER Model??
Myself:Answered
HR:What makes you think that you are a good programmer if you know java??
Myself:Answered advantages of Java
HR:How Exceptions are thrown and Handled in Java??
Myself:Answered
HR:Whats your project???
Myself:Answered
HR:Explain...??
Myself:Answered
HR:What languages u know???
Myself:Answered
HR:your Family???
Myself:Answered
HR:Accomplishments????
Myself:Showed Certificates and the Website i created..
HR:Long Term Goals??
Myself:Answered
HR:I dont lyk your Tie...plz Impress me??(i thought its a joke...but answered)..later realised it was stress ques..
HR: Do you think Software field will give you evrything u want in life???
Myself:Yes Sir....i am driven by passion to create S/w's Blah Blah Blah...
HR:Well it was lovely talking to you,u have any doubts...
Myself:Answered
Stood up(both of us) and Shook hands....i knew i had made it
Later i was selected
Linked List In C Programming(nutshell)
Linked lists are a way to store data with structures so that the programmer can
automatically create a new place to store data whenever necessary. Specifically,
the programmer writes a struct definition that contains variables holding
information about something and that has a pointer to a struct of its same type
(it has to be a pointer--otherwise, every time an element was created, it would
create a new element, infinitely). Each of these individual structs or classes
in the list is commonly known as a node or element of the list.
One way to visualize a linked list is as though it were a train. The programmer always stores the first node of the list in a pointer he won't lose access to. This would be the engine of the train. The pointer itself is the connector between cars of the train. Every time the train adds a car, it uses the connectors to add a new car. This is like a programmer using malloc to create a pointer to a new struct.
In memory a linked list is often described as looking like this:
In memory a linked list is often described as looking like this:
The representation isn't completely accurate in all of its details, but it will suffice for our purposes. Each of the big blocks is a struct that has a pointer to another one. Remember that the pointer only stores the memory location of something--it is not that thing itself--so the arrow points to the next struct. At the end of the list, there is nothing for the pointer to point to, so it does not point to anything; it should be a null pointer or a dummy node to prevent the node from accidentally pointing to a random location in memory (which is very bad).
Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.
In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, or next and prev(ious).

A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node
A technique known as XOR-linking requires the ability to do bit operations on addresses, and therefore may not be available in some high-level languages.
In the case of a circular doubly linked list, the only change that occurs is that end, or "tail", of the said list is linked back to the front, or "head", of the list and vice versa.
In some implementations, an extra sentinel or dummy node may be added before the first data record and/or after the last one. This convention simplifies and accelerates some list-handling algorithms, by ensuring that all links can be safely dereferenced and that every list (even one that contains no data elements) always has a "first" and "last" node.
So far we know what the node struct should look like:
Think back to the train. Let's imagine a conductor who can only enter the train through the first car and can walk through the train down the line as long as the connector connects to another car. This is how the program will traverse the linked list. The conductor will be a pointer to node, and it will first point to root, and then, if the root's pointer to the next node is pointing to something, the "conductor" (not a technical term) will be set to point to the next node. In this fashion, the list can be traversed. Now, as long as there is a pointer to something, the traversal will continue. Once it reaches a null pointer (or dummy node), meaning there are no more nodes (train cars) then it will be at the end of the list, and a new node can subsequently be added if so desired.
Here's what that looks like:
Finally, the code at the end can be used to add a new node to the end. Once the while loop as finished, the conductor will point to the last node in the array. (Remember the conductor of the train will move on until there is nothing to move on to? It works the same way in the while loop.) Therefore, conductor->next is set to null, so it is okay to allocate a new area of memory for it to point to (if it weren't NULL, then storing something else in the pointer would cause us to lose the memory that it pointed to). When we allocate the memory, we do a quick check to ensure that we're not out of memory, and then the conductor traverses one more element (like a train conductor moving on to the newly added car) and makes sure that it has its pointer to next set to 0 so that the list has an end. The 0 functions like a period; it means there is no more beyond. Finally, the new node has its x value set. (It can be set through user input. I simply wrote in the '=42' as an example.)
To print a linked list, the traversal function is almost the same. In our first example, it is necessary to ensure that the last element is printed after the while loop terminates. (See if you can think of a better way before reading the second code example.)
For example:
One way to visualize a linked list is as though it were a train. The programmer always stores the first node of the list in a pointer he won't lose access to. This would be the engine of the train. The pointer itself is the connector between cars of the train. Every time the train adds a car, it uses the connectors to add a new car. This is like a programmer using malloc to create a pointer to a new struct.
In memory a linked list is often described as looking like this:
In memory a linked list is often described as looking like this:
---------- ---------- - Data - - Data - ---------- ---------- - Pointer- - - -> - Pointer- ---------- ----------
The representation isn't completely accurate in all of its details, but it will suffice for our purposes. Each of the big blocks is a struct that has a pointer to another one. Remember that the pointer only stores the memory location of something--it is not that thing itself--so the arrow points to the next struct. At the end of the list, there is nothing for the pointer to point to, so it does not point to anything; it should be a null pointer or a dummy node to prevent the node from accidentally pointing to a random location in memory (which is very bad).
Types
Singly linked listSingly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.
Doubly linked list
A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node
Multiply linked list
In a multiply linked list, each node contains two or more link fields, each field being used to connect the same set of data records in a different order (e.g., by name, by department, by date of birth, etc.). (While doubly linked lists can be seen as special cases of multiply linked list, the fact that the two orders are opposite to each other leads to simpler and more efficient algorithms, so they are usually treated as a separate case.)Circular list
In the last node of a list, the link field often contains a null reference, a special value used to indicate the lack of further nodes. A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.In the case of a circular doubly linked list, the only change that occurs is that end, or "tail", of the said list is linked back to the front, or "head", of the list and vice versa.
Sentinel nodes
Empty lists
An empty list is a list that contains no data records. This is usually the same as saying that it has zero nodes. If sentinel nodes are being used, the list is usually said to be empty when it has only sentinel nodes.Hash linking
The link fields need not be physically part of the nodes. If the data records are stored in an array and referenced by their indices, the link field may be stored in a separate array with the same indices as the data records.List handles
Since a reference to the first node gives access to the whole list, that reference is often called the address, pointer, or handle of the list. Algorithms that manipulate linked lists usually get such handles to the input lists and return the handles to the resulting lists. In fact, in the context of such algorithms, the word "list" often means "list handle". In some situations, however, it may be convenient to refer to a list by a handle that consists of two links, pointing to its first and last nodes.Programmitically
So far we know what the node struct should look like:
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main()
{
/* This will be the unchanging first node */
struct node *root;
/* Now root points to a node struct */
root = malloc( sizeof(struct node) );
/* The node root points to has its next pointer equal to a null pointer
set */
root->next = 0;
/* By using the -> operator, you can modify what the node,
a pointer, (root in this case) points to. */
root->x = 5;
}
This so far is not very useful for doing anything. It is necessary to
understand how to traverse (go through) the linked list before it really becomes
useful. This will allow us to store some data in the list and later find it
without knowing exactly where it is located. Think back to the train. Let's imagine a conductor who can only enter the train through the first car and can walk through the train down the line as long as the connector connects to another car. This is how the program will traverse the linked list. The conductor will be a pointer to node, and it will first point to root, and then, if the root's pointer to the next node is pointing to something, the "conductor" (not a technical term) will be set to point to the next node. In this fashion, the list can be traversed. Now, as long as there is a pointer to something, the traversal will continue. Once it reaches a null pointer (or dummy node), meaning there are no more nodes (train cars) then it will be at the end of the list, and a new node can subsequently be added if so desired.
Here's what that looks like:
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main()
{
/* This won't change, or we would lose the list in memory */
struct node *root;
/* This will point to each node as it traverses the list */
struct node *conductor;
root = malloc( sizeof(struct node) );
root->next = 0;
root->x = 12;
conductor = root;
if ( conductor != 0 ) {
while ( conductor->next != 0)
{
conductor = conductor->next;
}
}
/* Creates a node at the end of the list */
conductor->next = malloc( sizeof(struct node) );
conductor = conductor->next;
if ( conductor == 0 )
{
printf( "Out of memory" );
return 0;
}
/* initialize the new memory */
conductor->next = 0;
conductor->x = 42;
return 0;
}
That is the basic code for traversing a list. The if statement ensures
that the memory was properly allocated before traversing the list. If the
condition in the if statement evaluates to true, then it is okay to try and
access the node pointed to by conductor. The while loop will continue as long as
there is another pointer in the next. The conductor simply moves along. It
changes what it points to by getting the address of conductor->next.
Finally, the code at the end can be used to add a new node to the end. Once the while loop as finished, the conductor will point to the last node in the array. (Remember the conductor of the train will move on until there is nothing to move on to? It works the same way in the while loop.) Therefore, conductor->next is set to null, so it is okay to allocate a new area of memory for it to point to (if it weren't NULL, then storing something else in the pointer would cause us to lose the memory that it pointed to). When we allocate the memory, we do a quick check to ensure that we're not out of memory, and then the conductor traverses one more element (like a train conductor moving on to the newly added car) and makes sure that it has its pointer to next set to 0 so that the list has an end. The 0 functions like a period; it means there is no more beyond. Finally, the new node has its x value set. (It can be set through user input. I simply wrote in the '=42' as an example.)
To print a linked list, the traversal function is almost the same. In our first example, it is necessary to ensure that the last element is printed after the while loop terminates. (See if you can think of a better way before reading the second code example.)
For example:
conductor = root;
if ( conductor != 0 ) { /* Makes sure there is a place to start */
while ( conductor->next != 0 ) {
printf( "%d\n", conductor->x );
conductor = conductor->next;
}
printf( "%d\n", conductor->x );
}
The final output is necessary because the while loop will not run once it
reaches the last node, but it will still be necessary to output the contents of
the next node. Consequently, the last output deals with this. We can avoid this
redundancy by allowing the conductor to walk off of the back of the train. Bad
for the conductor (if it were a real person), but the code is simpler as it also
allows us to remove the initial check for null (if root is null, then conductor
will be immediately set to null and the loop will never begin): conductor = root;
while ( conductor != NULL ) {
printf( "%d\n", conductor->x );
conductor = conductor->next;
}
Friday, 28 September 2012
Pointers in C
We are available online at http://sathyabamauniv.webs.com
Introduction.
Pointers are addresses.
Pointer notation.
Array notation.
Don't return a pointer to a local variable.
Null termination.
These notes apply to C++ as well, but I find it inconceivable that anyone could be programming in C++ without yet understanding these concepts.
For instance, the value of a pointer may be 42435. That number is an address in the computer's memory which is the start of some data. We can dereference the pointer to look at or change the data.
e.g.:
For instance, callers of this function must allocate the return data before calling the function with the address of that return data. Normally this data is allocated and unallocated automatically simply by declaring a normal variable.
e.g.:
During our Interview..a lot of question were given on Polymorphism and Pointers....they stressed not on "static" but on "Dynamic" polymorphism...Students who Answered only static were mostly rejected.. Detatiled idea of polymorphism will be in next blog..Here lets see whats all about Pointers.
__________________________________________________________________________________Introduction
A great many 'C programmers' appear to be lacking a basic understanding of pointers and memory allocation. However, they are essential to programming in C, and are really quite simple and clearly defined. Therefore, I will describe the use of pointers and the memory allocation issues which you must bear in mind.These notes apply to C++ as well, but I find it inconceivable that anyone could be programming in C++ without yet understanding these concepts.
Pointers are addresses
A pointer stores an address in memory. You will often need to pass around a pointer to some data rather than copying that data each time it is passed.For instance, the value of a pointer may be 42435. That number is an address in the computer's memory which is the start of some data. We can dereference the pointer to look at or change the data.
Pointer notation
The notation for manipulating pointers is not obvious because it uses the asterisk (*) for two different purposes, and in C++ the ampersand (&) is used in an additional way (declaring by-reference function parameters). However, in C there are really only three ways in which you will use the asterisk and ampersand with pointers. You may need to refer back to these three examples for a while, but eventually you will find their use straightforward.Declaring a pointer
int* example; /* Declares a pointer to an int. */
Getting the address of a variable.
int something = 3;
example = &something. /* Makes example point to the address of something. */
Dereferencing a pointer
*example = 2; /* Sets the value of the data pointed to by example. */
Array Notation
The square-bracket array notation is really just a short cut to prevent you from having to do pointer arithmetic.e.g.:
char array[5]; /* array is a pointer to array[0]. /*
array[2] = 12; /* This is equivalent to *(array+2) = 12; */Don't forget that when you pass an array in to a function you are really just passing a pointer to its first element, so the function also needs to know how many elements there are.
Don't return a pointer to a local variable.
Undisciplined C programmmers often return pointers to character arrays from functions, without documenting how that pointer should be used. If you encounter one of these functions you know that one of the following must be true:- The data pointed to by the pointer was allocated dynamically within the function with malloc() (or new in C++), and the function expects the caller to un-allocate the data dynamically with free() (or delete in C++). A char* return type, rather than a const char* return type is often an indication that this is the case, though C programmers often forget to use the const keyword properly with character arrays.
Or if the function is allocating resources in some special way (particularly if it's not just a pointer to a character array), then a second function is generally provided to un-allocate the resource when it is no longer needed. - The data pointed to by the pointer exists for the lifetime of an object or service, beyond just the lifetime of the function call. But it is still generally safer to copy the data immediately, for instance with strcpy(). A const char* return type, rather than a char* return type is often an indication that this is the case, though C programmers often forget to use the const keyword properly with character arrays.
- The data pointed to by the pointer was allocated automatically (on the "stack") within the function and was un-allocated automatically when the function returned. The data pointed to by the pointer may contain the correct data right now, but the system may decide to overwrite it at any time. Bluffer programmers think this is OK because their program compiles and it occasionally works.
Null termination.
So far we have assumed that character arrays are null-terminated, meaning that there is a 0 value byte after the last character of the text, though that byte is still inside the allocated memory. This is normal on most modern systems, and is assumed by many standard C functions such as strcpy(), but you will find some functions that do not use this principle. In this case, the function should return the length of the array, so you can avoid reading past the end, and/or should take a pre-allocated array and its length as parameters to the function.For instance, callers of this function must allocate the return data before calling the function with the address of that return data. Normally this data is allocated and unallocated automatically simply by declaring a normal variable.
e.g.:
char array[10];
int result = my_function(array, 10);
/* Remember array is the address of array[0]. */strncpy() is an example of such a function. though strcpy() assumes null-termination.
Subscribe to:
Comments (Atom)

