list - LinkedList Delete End in C -
i trying program simple text editor in c , using linkedlist. have problems deleteend function. did go wrong?
#include <stdio.h> #include <stdlib.h> #include<conio.h>
i store character, , coordinates of character in structure this:
struct node { struct node *previous; char c; int x; int y; struct node *next; }*head;
this called whenever letter typed in.
void characters(char typed, int xpos, int ypos) //assign values of node { struct node *temp,*var,*temp2; temp=(struct node *)malloc(sizeof(struct node)); temp->c=typed; temp->x=xpos; temp->y=ypos; if(head==null) { head=temp; head->next=null; } else { temp2=head; while(temp2!=null) { var=temp2; temp2=temp2->next; } temp2=temp; var->next=temp2; temp2->next=null; } }
print new node if there changes.
void printer() //to print { struct node *temp; temp=head; while(temp!=null) { gotoxy(temp->x,temp->y); printf("%c",temp->c); temp=temp->next; } }
now here, not know why last element of head won't deleted.
void deletesend() { struct node *temp,*last; temp=head; while(temp!=null) { last=temp; temp=temp->next; } if(last->previous== null) { free(temp); head=null; } last=null; temp->previous=last; free(temp); }
this begins.
main() { char c; //for storing character int x,y; //for position of character clrscr(); for(;;) { c=getch(); x=wherex(); y=wherey(); if(c==0x1b) //escape exit { exit(0); } else if (c==8) //for backspace { deletesend(); // clrscr(); printer(); } else //normal characters { characters(c,x,y); printer(); } } }
try :
void deletesend() { struct node *temp,*last; temp=head; last = temp; while(temp != null && temp->next!=null) { last=temp; temp=temp->next; } if(last == temp) { free(temp); head=null; } else { free(last->next); last->next = null; }
in algorithm, try work null pointer, can't previous node think.
Comments
Post a Comment