![]() |
| ||
| Linked List: Inserting a new node 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: cout<<"Enter a position and number to add to Link List"<<endl; in the createList function. Here is my full program: (Please ignore the functions that are commented out) #include <iostream> |
| ||
| 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"); } |
| All times are GMT -4. The time now is 11:02 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC