2 Replies - 1167 Views - Last Post: 26 April 2012 - 10:39 AM Rate Topic: -----

#1 Emon_MQ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 06-August 09

Problem with deleting the last node of singly list

Posted 26 April 2012 - 09:37 AM

Hi,I am not able to delete the last node using my deleteList().What am I doing wrong here??for any other node here I can delete but as soon as I try to delete the last node the program crashes.I have given the full code here.Any suggestion would be really helpful.Thanks.
#include<stdio.h>

struct listNode
{
  char entry;
  struct listNode *next;     
};

struct list
{
 int count;
 struct listNode *head;      
};

typedef struct listNode ListNode;
typedef struct list List;

void createList(List **myList);
ListNode *makeAListNode(char ch);
void setPosition(int p,List **myList,ListNode **current);
void insertList(List **myList,int p,char ch);
void printList(List *myList);
void deleteNode(List **myList,int p,char *ch);
int main()
{
 List *myList=NULL;
 createList(&myList);
 insertList(&myList,0,'a');
 insertList(&myList,1,'a');
 insertList(&myList,2,'a');
 char ch;
 deleteNode(&myList,-1,&ch);
// printList(myList);
    
 system("PAUSE");
 return 0;   
}
void createList(List **myList)
{
 *myList=malloc(sizeof(List));
 (*myList)->head=NULL;
 (*myList)->count=0;    
}
ListNode *makeAListNode(char ch)
{
  ListNode *newNode=malloc(sizeof(ListNode));
  newNode->entry=ch;
  return newNode;
         
}
void setPosition(int p,List **myList,ListNode **current)
{
 int i;
 *current=(*myList)->head;
 for(i=0;i<p;i++)
   *current=(*current)->next; 
}
void insertList(List **myList,int p,char ch)
{
  if(p<0||p>(*myList)->count)
  {
     printf("\nInvalid position\n");
     return;                        
  }
  ListNode *newNode=makeAListNode(ch); 
  if(p==0)
  {
   newNode->next=(*myList)->head;
   (*myList)->head=newNode;
   (*myList)->count++;
   return;       
  }
  ListNode *current,*temp;
  setPosition(p-1,myList,&current);
  temp=current->next; 
  newNode->next=temp;
  current->next=newNode;
  (*myList)->count++;
  return;  
}
void printList(List *myList)
{
 ListNode *current=myList->head;
 while(current!=NULL)
 {
   printf("%c->",current->entry);
   current=current->next;                         
 }    
}
void deleteNode(List **myList,int p,char *ch)
{
  if(p<0||p>(*myList)->count)
  {
   printf("\nInvalid position\n");
   return;
  }
  ListNode *current,*temp;
  current=(*myList)->head;
  if(p==0)
  {
   (*myList)->head=(*myList)->head->next;
   free(current);
   (*myList)->count--;
   return;       
  }
  setPosition(p,myList,&current);
  temp=current->next;
  *ch=temp->entry;
  current->next=temp->next;
  free(temp);
  (*myList)->count--;
  
      
}




Is This A Good Question/Topic? 0
  • +

Replies To: Problem with deleting the last node of singly list

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Problem with deleting the last node of singly list

Posted 26 April 2012 - 10:02 AM

Moved to C++ forum, Please reserve C++ Advanced Discussion forum for not help related topics.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Problem with deleting the last node of singly list

Posted 26 April 2012 - 10:39 AM

deleteNode(&myList,-1,&ch);



Why would you pass -1? Your own code blocks such things. if(p<0||p>(*myList)->count). Your set position doesn't check and will let you loop any where.

Also, you're getting seriously carried away with those pointer pointers. The idea of wrapping a list in a struct is so you don't have to...

I'd start the code like so:
#include<stdio.h>

typedef struct listNode {
	char entry;
	struct listNode *next;     
} ListNode;

typedef struct {
	int count; /* do you really need a count, btw? */
	ListNode *head;
} List;

void initList(List *);
ListNode *makeAListNode(char ch);
ListNode *getNodeAt(List *, int);

int insertList(List *,int,char); /* return if success */
int deleteAt(List *,int,char *); /* return if success */
int getListSize(List *);
void printList(List *);

int main() {
	List list;
	char ch;

	initList(&list);
	insertList(&list,0,'a');
	insertList(&list,1,'a');
	insertList(&list,2,'a');
	printList(&list);
	char ch;
	deleteAt(&list, getListSize(&list)-1, &ch);
	printList(&list);
	return 0;   
}



Though, to be perfectly honest, an index in a linked list is unnatural. Perhaps you want:
#include<stdio.h>

typedef struct listNode {
	char entry;
	struct listNode *next;     
} ListNode;

typedef struct {
	ListNode *head, *tail;
} List;

void listInit(List *);
void listInsertFront(List *,char);
void listInsertBack(List *,char);
void listDeleteFront(List *);
void listDeleteBack(List *);
void listPrint(List *);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1