insert in Linked List Turbo C - c

I am trying to program a text editor in C. I am having trouble with inserting an element in a linked list. The program simply won't insert anything in the middle of the linked list.
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
I used singly linked list.
struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;
this works fine:
void characters(int typed, int xpos, int ypos) //assign values of a 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;
}
}
this works just fine too.
void printer() //to print everything
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
gotoxy(temp->x,temp->y);
printf("%c",temp->c);
temp=temp->next;
}
}
this works just fine too:
void deletesEnd //delete at the end
{
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;
}
}
THIS IS THE PROBLEM:
void checker(int ch, int xpos, int ypos)
{
int flag=0;
struct node *temp,*temp1,*insert_node;
temp=head;
while(temp!=NULL)
{
if(temp->x==xpos && temp->y==ypos)
{
temp1=temp;
temp=insert_node;
insert_node->c=ch;
insert_node->x=xpos;
insert_node->y=ypos;
insert_node->next=temp1;
flag=1;
break;
}
else
temp= temp->next;
}
free(temp);
free(temp1);
if(flag==0)
characters(ch,xpos,ypos);
}
main()
{
int c; //for storing the character
int x,y; //for the position of the character
clrscr();
for(;;)
{
c=getch();
x=wherex();
y=wherey();
if(c==27)
exit(0);
else if(c==0|| c==224)
{
switch(getch())
{
case 72: //for up
gotoxy(x,y-1);
break;
case 80: //for down
gotoxy(x,y+1);
break;
case 75: //for left
gotoxy(x-1,y);
break;
case 77: //for right
gotoxy(x+1,y);
break;
}
}
else if(c==13)
{
printf("\n");
}
else if(c==8) //for backspace
{
deletesEnd();
clrscr();
printer();
}
else //for normal characters
{
checker(c,x,y);
// characters(c,x,y);
printer();
}
}
}
I tried to debug it, it goes inside the loop with the conditional statement of ((temp->x==xpos && temp->y==ypos)) Thus, the program is supposed to insert an element but it doesn't. :(

Maybe you have to malloc a struct node for the element you want to insert first, not just declare a struct node *.
Try to add struct node *insert_node = (struct node *)malloc(struct node) in your checker method.

Try concept of insertion from this one - It can insert a node from front end
Just make a new pointer to node and name it create
struct node *create;
int item;
printf("Enter a number you want to insert\n\t");
scanf("%d",&item);
create = (struct node*)malloc(sizeof(struct node*));
create->info = item;
create->ptr = first;
HEAD = create;

Related

Singly linked list program error while using malloc function(updated one)

I am having trouble inserting the second element the program just waits and exits, the first element is printing correctly, the problem occurs when I try to enter 2nd element using function Insert.
please help
case 7: uses the function Insert which passes a "d" which is data,and Display
prints the list.
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void Insert(int d){
struct node *temp,*newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*temp;
void Insert(int d){
struct node *newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n", );
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}

Palindrom check of linkedlist

Here is the code i wrote to check if a singly linked list of integers is a palindrome or not.
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
struct list *insert(int data,struct list *node)
{
if(node==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->data=data;
node->next=NULL;
}
else
{
struct list *newnode=(struct list *)malloc(sizeof(struct list));
newnode->data=data;
newnode->next=node;
node=newnode;
}
return node;
}
int palindrome(struct list *node,int n)
{
int i=0;int j=0;
int arr1[n],arr2[n];
struct list *current;
current=node;
while(current!=NULL)
{
arr1[i]=current->data;
i++;
current=current->next;
}
i=0;j=0;
for(i=n-1;i>=0;i--)
{
arr2[j]=arr1[i];
j++;
}
for(i=0;i<n;i++)
{
if(arr1[i]!=arr2[i])
{
return 0;
}
}
return 1;
}
void main()
{
int n;
scanf("%d",&n);
struct list *node=NULL;
int i=1;int value;
for(i=1;i<=n;i++)
{
scanf("%d",&value);
insert(value,node);
}
int status=palindrome(node,n);
printf("%d",status);
}
But the code returns 0 even in case of valid palindrome inputs like "121" and also in non - palindrome inputs like "154". Please help. Thanks
You need to write
node = insert(value,node);
in main. Otherwise the head node is not changed because the function insert deals with a copy of the node.

I want to swap the linkedlist terms pairwise, my code is giving me Segmentation Fault

I want to swap the linkedlist terms pairwise.
Here is my code. It is giving me Segmentation Fault-core dumped
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert(struct node *n)
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(n==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
int main()
{
struct node *n;
head=NULL;
n=head;
int i;
for(i=0;i<6;i++)
{
insert(n);
n=head;
}
display(n);
pairswap(n);
display(n);
}
void display(struct node *n)
{
struct node *temp;
temp=n;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void pairswap(struct node *n)
{
struct node *temp,*temp1,*temp2;
temp=n;
temp1=temp->next;
while(temp!=NULL)
{
int tempnum;
tempnum=temp->data;
temp->data=temp1->data;
temp1->data=tempnum;
if(temp==n)
{
head=temp;
head->next=temp1;
}
else
{
temp2->next=temp;
temp2->next->next=temp1;
}
temp2=temp1;
temp=(temp->next)->next;
temp1=temp->next;
}
n=head;
}
Please learn about the debuggers.
Somewhere at the end of the list the value of
(temp->next)->next
is NULL, which you are putting it in variable temp.
Before making this assignment temp1=temp->next, you need to check if temp is NULL and take proper action.
The problem is pairswap function. In the while loop you are using the condition:
while(temp!=NULL)
and inside the loop you are assigning:
temp=(temp->next)->next
which will get crashed on the second last node since you are trying to dereference NULL pointer.
Use the following condition:
while((temp->next)->next!=NULL)
or
while (temp1->next != NULL)

creating a binary search tree with strings

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
struct node{
char *name;
struct node *lchild;
struct node *rchild;
}*root;
void find(char *str,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL)
{
*loc=NULL;
*par=NULL;
return;
}
if(!(strcmp(str,root->name)))
{
*loc=root;
*par=NULL;
return;
}
if(strcmp(str,root->name)<0)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(!(strcmp(str,ptr->name)))
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(strcmp(str,ptr->name)<0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}
void insert(char *str)
{
struct node *parent,*location,*temp;
find(str,&parent,&location);
if(location!=NULL)
{
printf("Name already present\n");
return;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->name=str;
temp->lchild=NULL;
temp->rchild=NULL;
if(parent==NULL)
root=temp;
else
if(strcmp(str,parent->name)<0)
parent->lchild=temp;
else
parent->rchild=temp;
}
void displayin(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
displayin(ptr->lchild);
printf("%s -> ",ptr->name);
displayin(ptr->rchild);
}
}
int main()
{
root=NULL;
char str[20];
while(1)
{
printf("Enter name: ");
fflush(stdin);
gets(str);
insert(str);
printf("Wants to insert more item: ");
if(getchar()=='y')
insert(str);
else
break;
}
displayin(root);
getch();
getchar();
return 0;
}
If i run this piece of code with following input
rakesh
rajesh
bimal
then,it is displaying the output as "bimal->" which is wrong. I don't know where the logic is going wrong. I crosschecked but not able to find mistake. Can somebody take a look on this.
One of the issue:
In your insert() function you are doing
temp=(struct node*)malloc(sizeof(struct node));
temp->name=str; //this is not correct,
//do
temp=malloc(sizeof(struct node)); // no type cast for malloc
temp->name = strdup(str); //allocate memory too
//also check you NULL and free the allocated memory.
Your are just setting the pointer location in the node you created for string to store, but it points to str array from main(). So all nodes will point to same location, which will have last value entered. In your case its "bimal".
Your find function is in any case quite obscure. This is the improved version.
void find(char *str,struct node **par,struct node **loc)
{
*par = NULL;
*loc = NULL;
struct node *ptr,*ptrsave;
if(root==NULL) return;
if(!(strcmp(str,root->name)))
{
*loc=root;
return;
}
ptrsave = NULL;
ptr = root;
while(ptr!=NULL) {
if(!(strcmp(str,ptr->name))) break;
ptrsave = ptr;
if(strcmp(str,ptr->name)<0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=ptr;
*par=ptrsave;
}

Doubly Linked List Delete Function Error

I have written the following program but the problem is in Delete function.Whenever i am trying to delete the value at first position.The whole list is lost don't know why.If i try to display list after that then some garbage values are printed.The function is working perfectly for other positions.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
struct Student
{
int rno;
char name[20];
struct Student *next;
struct Student *prev;
};
void Display(struct Student *head)
{
assert(head!=NULL);
while(head!=NULL)
{
printf("%d\t%s\t",head->rno,head->name);
head=head->next;
}
}
struct Student *Insert(struct Student *head,const int position,const int rno,const char name[])
{
//printf("%s\n",__FUNCTION__);
struct Student *temp=(struct Student *)malloc(sizeof(struct Student));
struct Student *traverse=head;
int pos=position;
if(temp==NULL)
exit(-1);
temp->rno=rno;
strcpy(temp->name,name);
temp->next=NULL;
temp->prev=NULL;
// printf("%s\n",__FUNCTION__);
if(pos==1)
{
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
else
{
for(traverse=head,pos=position;traverse->next!=NULL&&pos-2!=0;traverse=traverse->next,pos--);
if(traverse==NULL || pos-2!=0)
{
printf("Invalid Position");
}
else
{
temp->next=traverse->next;
if(temp->next!=NULL)
temp->next->prev=temp;
temp->prev=traverse;
traverse->next=temp;
}
}
return head;
}
void DeleteAll(struct Student *head)
{
struct Student *temp=head;
while(temp->next!=NULL)
{
head=head->next;
free(temp);
temp=head;
}
free(temp);
}
void Delete(struct Student *head,int pos)
{
assert(head!=NULL);
struct Student *temp=head;
struct Student *traverse=head;
int position=pos;
if(position==1)
{
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
}
else
{
while(traverse->next!=NULL&&position-1!=0)
{
traverse=traverse->next;
position--;
}
if(traverse==NULL || position-1!=0)
{
printf(".............Invalid position..........\n");
}
else
{
traverse->prev->next=traverse->next;
if(traverse->next)
traverse->next->prev=traverse->prev;
}
}
}
struct Student *CreateStudentList(const int no_of_students)
{
struct Student *head=NULL;
int i;
int rno;
char name[20];
for(i=0;i<no_of_students;i++)
{
printf("Enter roll number and name:");
scanf("%d%s",&rno,name);
head=Insert(head,i+1,rno,name);
}
return head;
}
void SimulateDoublyLinkedList()
{
struct Student *cdscpp2013=NULL;
int no_of_students;
int choice,rno,position;
char name[20];
while(choice!=5)
{
if(NULL==cdscpp2013)
{
printf("Enter number of students:");
scanf("%d",&no_of_students);
cdscpp2013=CreateStudentList(no_of_students);
}
else
{
printf("\nMenu Operations\nPress 1 for Insert\nPress 2 for Delete\nPress 3 for DeleteAll\nPress 4 for Display\nPress 5 for Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter roll number and name to ininsert:");
scanf("%d%s",&rno,name);
printf("Enter position to insert:");
scanf("%d",&position);
cdscpp2013=Insert(cdscpp2013,position,rno,name);
break;
case 2:
printf("Enter position to delete:");
scanf("%d",&position);
Delete(cdscpp2013,position);
break;
case 3:
DeleteAll(cdscpp2013);
break;
case 4:
Display(cdscpp2013);
break;
case 5:
exit(1);
default:
printf("Invalid choice....Please Enter proper option.....");
}
}
}
}
int main()
{
SimulateDoublyLinkedList();
}
When you remove the first element of a list, the caller must update its reference to the list. So your Delete function must return the pointer to the first element of the resulting list.
In this kind of operation I also find useful to use pointers to pointers... this would simplify a lot your code.
try this for delete this should work -
as temp = head , the temp->next =null will make the whole list disappear
//just removed temp->next = null;
if(position==1) {
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
free(temp);
}
Edited --
make delete function to return the head pointer and in the switch case use this
cdscpp2013=Delete(cdscpp2013,position);
instead of this
Delete(cdscpp2013,position);
then change the same for deleteall function - it should work :)

Resources