943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 988
  • C++ RSS
Nov 20th, 2008
0

Linked List: Inserting a new node

Expand Post »
Hello.

I am currently having problems. I am asking a user to input the position and number of where they would like to insert into the Linked List. After the user input the information, the number does not appear in the Linked List, but everything else does. Can someone help me fix this problem?

I am asking the user:

C++ Syntax (Toggle Plain Text)
  1. cout<<"Enter a position and number to add to Link List"<<endl;
  2. cin>>index;
  3. cin>>number;

in the createList function.


Here is my full program: (Please ignore the functions that are commented out)


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodeType
  5. {
  6. int info;
  7. nodeType *link;
  8. };
  9.  
  10.  
  11. void createList (nodeType*& first, nodeType*& last);
  12. void deleteFront (nodeType*& first,nodeType*& last);
  13. nodeType *GetNth (nodeType *first, int index);
  14. void insertBack (nodeType*& first, nodeType*& last, int info);
  15. void InsertNth (nodeType*& first, nodeType*& last,
  16. int index, int info);
  17. int modifyNum (nodeType*& first);
  18. void printList (nodeType*& first);
  19. int searchNum (nodeType*& first, int info);
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25. nodeType *first, *last;
  26. int index;
  27. int info;
  28.  
  29. createList(first,last);
  30. printList(first);
  31.  
  32.  
  33. GetNth(first,index);
  34. InsertNth(first,last,index,info);
  35. // printList(first);
  36.  
  37. searchNum(first,info);
  38. modifyNum(first);
  39. // printList(first);
  40.  
  41. insertBack(first,last,info);
  42. // printList(first);
  43.  
  44. deleteFront(first,last);
  45. // printList(first);
  46.  
  47.  
  48.  
  49. system("PAUSE");
  50. return 0;
  51. }
  52.  
  53.  
  54.  
  55.  
  56. void createList(nodeType*& first, nodeType*& last)
  57. {
  58. int number;
  59. nodeType *newNode;
  60. int counter = 0;
  61. int index = 0;
  62.  
  63. first = NULL;
  64. last = NULL;
  65. cout<<"Enter an integer (-999 to stop): ";
  66. cin>>number;
  67. cout<<endl;
  68. while (number != -999)
  69. {
  70. InsertNth (first, last, counter++, number);
  71. if (counter == 1)
  72. {
  73. last = first;
  74. }
  75. cout<<"Enter an integer (-999 to stop): ";
  76. cin>>number;
  77. cout<<endl;
  78. }
  79. cout<<"Enter a position and number to add to Link List"<<endl;
  80. cin>>index;
  81. cin>>number;
  82. if (first == NULL)
  83. cout<<"Empty list"<<endl;
  84. else
  85. cout<<"There are "<<counter<<" items in the linked list"<<endl;
  86. }
  87.  
  88.  
  89.  
  90. void deleteFront (nodeType*& first,nodeType*& last)
  91. {
  92. //if (first != NULL)
  93. //{
  94. //nodeType* next = first;
  95. //first = first->link;
  96. //if (last == next)
  97. //{
  98. //last = NULL;
  99. //}
  100. //delete (next);
  101. //}
  102. }
  103.  
  104.  
  105. int modifyNum (nodeType*& first)
  106. {
  107.  
  108. int searchCount = 0;
  109. int num = 1;
  110. int Newnum = 4;
  111. bool found = false;
  112. nodeType *current;
  113.  
  114. current = first;
  115.  
  116. while (current != NULL)
  117. {
  118. if (current->info == num)
  119. {
  120. current->info = 5;
  121. cout<<endl;
  122. }
  123. current = current->link;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. nodeType *GetNth(nodeType *first,int index)
  130. {
  131. nodeType *current = first;
  132. while ((current != NULL) && (index > 0))
  133. {
  134. current = current->link;
  135. index--;
  136. }
  137. return (current);
  138. }
  139.  
  140.  
  141.  
  142. void insertBack (nodeType*& first, nodeType*& last, int info)
  143. {
  144. //nodeType *newNode = new nodeType; // create new node
  145. //newNode->info = info;
  146. //newNode->link = NULL;
  147. //if (last == NULL)
  148. //{
  149. //first = last = newNode;
  150. //}
  151. //else
  152. //{
  153. //last->link = newNode;
  154. //}
  155. }
  156.  
  157.  
  158.  
  159. void InsertNth(nodeType*& first,nodeType*& last,int index, int info)
  160. {
  161. nodeType *newNode = new nodeType; // create new node
  162. newNode->info = info;
  163. newNode->link = NULL;
  164. int number;
  165.  
  166.  
  167. if (first == NULL)
  168. {
  169. first = newNode;
  170. }
  171. else
  172. {
  173. nodeType *next = first;
  174. while ((next->link != NULL) && (index > 0))
  175. {
  176. next = next->link;
  177. index--;
  178. }
  179. newNode->link = next->link;
  180. next->link = newNode;
  181. if (newNode->link == NULL)
  182. {
  183. last = newNode;
  184. }
  185. }
  186.  
  187. }
  188.  
  189.  
  190.  
  191. void printList (nodeType*& first)
  192. {
  193. nodeType *next = first;
  194. int index = 0;
  195. while (next != NULL)
  196. {
  197. cout<<endl;
  198. cout << "Position: " << index << ", Number = " << next->info << endl;
  199. index++;
  200. next = next->link;
  201. }
  202. cout << endl << endl;
  203. }
  204.  
  205.  
  206. int searchNum (nodeType*& first, int info)
  207. {
  208. int index = 0;
  209. nodeType *next = first;
  210. while ((next != NULL) && (next->info != info))
  211. {
  212. next = next->link;
  213. index++;
  214. }
  215. if (next->info != info)
  216. {
  217. index = -1;
  218. }
  219. return (index);
  220. }
Last edited by NinjaLink; Nov 20th, 2008 at 2:35 am.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List: Inserting a new node

try this code.


/* Program to implement operations on singly linked list */

#include<stdio.h>
#include<conio.h>
/* declare structure for a node of singly linked list */
struct node
{
int info;
struct node *next;
}*sll,*start;

void insert();
void display();
void delet();
void search();

main()
{
int ch;
start=NULL;
clrscr();
do
{
printf("\n Menu: ");
printf("\n 1. Insert \t 2. Delete \t 3. Display \t 4. Search \t 5. Exit");
printf("\n Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\t\t Insert a node at any position");
insert();
break;
case 2: printf("\n\t\t Delete a node");
delet();
break;
case 3: printf("\n\t\t Display the list");
display();
break;
case 4: printf("\n\t\t Search for a node");
search();
break;
case 5: printf("\n\t\t Exiting");
exit(1);
} /* end of switch */
}while(ch!=5); /* end of do-while*/
}
void insert()
{
struct node *temp,*r,*s;
int inf;
temp=malloc(sizeof(struct node *)); /* Allocating memory for new node*/
temp->next=NULL;
printf("\n Enter information");
scanf("%d",&temp->info);
if(start==NULL) /* if this is first node*/
{
start=temp;
}
else /* second node & so on */
{
printf("\n After which item you want to insert");
scanf("%d",&inf);
r=start;
while(r->info!=inf)
{
r=r->next;
}
s=r->next;
r->next=temp;
temp->next=s;
}
}

void display()
{
struct node *temp;
temp=start;
if(temp==NULL) /* check whether list is empty*/
{
printf("\n Linked list is empty");
return;
}
else
{
do
{
printf("\n %d",temp->info);
temp=temp->next;
}while(temp!=NULL);
}
}
void delet()
{
struct node *temp,*r;
int inf;
printf("\n Which item you want to delete");
scanf("%d",&inf);
temp=start;
if(temp==NULL) /* list is empty*/
{
printf("\n Linked list is empty");
return;
}
else
{
while(temp->info!=inf)
{
temp=temp->next;
}
if(temp->next==NULL) /* if this is last node*/
{
printf("\n Deleted node is %d",temp->info);
r=start;
while(r->next!=temp)
{
r=r->next;
}
r->next=NULL;
}
else if(temp==start) /* if this is first node*/
{
printf("\n Deleted node is %d",temp->info);
start=start->next;
}
else /* if this is some middle node*/
{
printf("\n Deleted node is %d",temp->info);
r=start;
while(r->next!=temp)
{
r=r->next;
}
r->next=temp->next;
}
free(temp);
}
}

void search()
{
int inf;
struct node *temp;
printf("\n Enter the item which you want to search");
scanf("%d",&inf);
temp=start;
do
{
if(temp->info==inf)
{
printf("\n Item found");
return;
}
else
{
temp=temp->next;
}
}while(temp!=NULL);
printf("\n Item not found");
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
paramdhingra is offline Offline
5 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to insert a combox to a column of list ctrl in MFC?
Next Thread in C++ Forum Timeline: Discrete solution help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC