Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Meet and Greet (http://www.go4expert.com/community/meet-and-greet/)
-   -   Why the code is not working? (http://www.go4expert.com/community/code-t21575/)

sumedha 30Mar2010 13:21

Why the code is not working?
 
Code:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
 
struct node
{
 int num;
 struct node *next;
};
typedef struct node NODE;
NODE *start=NULL;
NODE *last=NULL;
void eledisplay();
eleinsert(NODE *start);
eledelete(NODE *start);
void main()
{
 int ch;
 do
 {
  printf("1.insert\n");
  printf("2.Delete\n");
  printf("3.Display\n");
  printf("4.Exit");
  printf("Enter ur choice");
  scanf("%d",&ch);
  switch(ch)
  {
  case 1:start=eleinsert(start);
  break;
  case 2:eledelete(start);
  break;
  case 3:eledisplay();
  break;
  case 4:return;
  }
 }
  while(ch!=4);
}
NODE *eleinsert(NODE *start)
{
 NODE *p;
 p=(NODE *)malloc(sizeof(NODE));
  printf("Enter ele to insert\n");
 scanf("%d",&p->num);
 if(start==NULL)
 {
  p->next=p;
  start=p;
  last=p;
 }
 else
 {
  p->next=start;
  start=p;
  last->next=p;
 }
 return start;
}

void eledisplay()
{
 NODE *p;
 p=start;
 if(p==NULL)
 {
  printf("List is empty");
 }
 else
 {
  while(p->next!=start)
  {
  printf("%d",p->num );
  p=p->next;
  }
 }
 
}
NODE *eledelete(NODE *start)
{
 NODE *p;
 p=start;
 if(p==NULL)
 {
  printf("List is empty");
 }
 else
 {
  p=start;
  start=start->next;
  printf("Deleted ele is:%d",p->num);
  last->next=p;
 }
 return  start;
}


shabbir 30Mar2010 18:27

Re: Why the code is not working?
 
Who says its not working. I am sure it is working but not as you expect it to?

sumedha 31Mar2010 12:25

Re: Why the code is not working?
 
ya,Its working bt
When all nodes r deleted, it stiil displays deleted nodes.
Why?

shabbir 31Mar2010 15:14

Re: Why the code is not working?
 
Your deletion logic is not at all right. Search the forum and you will find code on how to delete a node in a linked list


All times are GMT +5.5. The time now is 23:20.