insert sort alphabetically using doubly linked list - c

Is this correct? I used the code I did when sorting numbers I don't know if it works the same with strings. When I run it, there are no errors detected, but when I try to view it, the inputs does not appear.
void add(node **h, node **t){
node *temp, *ptr;
char s[20];
temp = (node*) malloc(sizeof(node));
printf ("-INSERT-");
printf("Fruit: ");
scanf("%s", temp->fruit);
printf("Color: ");
scanf("%s", temp->color);
printf("Texture: ");
scanf("%s", temp->texture);
if ( (*h)->next==(*t) ) {
temp->next = (*h)->next;
temp->prev = (*h);
(*h)->next->prev = temp;
(*h)->next = temp;
}
else{
if(strcmp(s, temp->fruit)){
temp->next = (*h)->next;
temp->prev = (*h);
(*h)->next->prev = temp;
(*h)->next = temp;
}
else if(strcmp(s, temp->fruit)){
temp->next = (*t);
temp->prev = (*t)->prev;
(*t)->prev->next = temp;
(*t)->prev = temp;
}
else{
while(strcmp(s, ptr->fruit)){
ptr = ptr->next;
}
temp->next = ptr->next;
temp->prev = ptr;
ptr->next->prev = temp;
ptr->next = temp;
}
}

Related

I was trying to make a simple doubly linked list with operations Insert, Delete and Display and

The insert seems to go smoothly but the display only shows the head element only. I wanted to do this on my own and tried to use the logic. I am confused whether the fault lies in the Insert function or the Display.
I am not really that great at programming and just started learning C++.Thank you for your help.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
void insert(struct node **head)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
if ((*head) == NULL)
{
int x;
printf("\nEnter the value of the starting node :");
scanf("%d", &x);
newnode->data = x;
(*head) = newnode;
}
else
{
int pos, x;
printf("\nEnter the pos ");
scanf("%d", &pos);
if (pos == 0)
{
newnode->next = (*head);
newnode->prev = NULL;
(*head)->prev = newnode;
(*head) = newnode;
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
}
else
{
struct node *temp;
struct node *ptr = (*head);
while(ptr->next!=NULL)
{
for (int i = 0; i < pos - 1; i++)
{ ptr = ptr->next;}
}
if (ptr->next == NULL)
{
newnode->prev = ptr;
newnode->next = NULL;
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
}
else
{
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
temp = ptr->next;
newnode->prev = ptr;
newnode->next = temp;
ptr->next = newnode;
temp->prev = newnode;
}
}
}
}
void delete (struct node **head)
{
struct node *ptr;
ptr = (*head);
if ((*head) == NULL)
{
printf("\nUnderflow\n");
}
else
{
int pos;
printf("\nEnter the pos ");
scanf("%d", &pos);
struct node *temp;
for (int i = 0; i < pos; i++)
{
ptr = ptr->next;
}
temp = ptr->next;
temp->prev = ptr->prev;
ptr->next = NULL;
ptr->prev = NULL;
}
}
void display(struct node **head)
{
struct node *ptr = (*head);
if (ptr != NULL)
{
printf(" %d ",ptr->data);
}
else
{
printf("\nUnderflow OR empty\n");
}
}
int main()
{
while (1)
{
int x;
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n\nChoose option :\n");
scanf("%d", &x);
switch (x)
{
case 1:
{
insert(&head);
break;
}
case 2:
{
delete (&head);
break;
}
case 3:
{
display(&head);
break;
}
default:
{
printf("\nWrong operation.Select again :");
continue;
}
}
}
return 0;
}
The display was supposed to show the list like
a-> b -> c......
In the display function you should take the next node and print that too
// head->next1->next2->NULL
struct node *ptr = (*head);
while (ptr != NULL) {
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL");

Delete function in linked-list

Do you understand why when i call the delete function here the output become an infinite loop? Without that function the code works.
int main(){
node *head = NULL;
end(&head);
begin(&head);
begin(&head);
end(&head);
begin(&head);
end(&head);
begin(&head);
delete(&head);
display(head);
return 0;
}
void delete(node **head){
node *tmp,*prev = NULL;
int num;
printf("Insert the number that you want to delete: ");
scanf("%d",&num);
tmp = *head;
if (tmp->data == num){
*head = tmp->next;
free(tmp);
return;
}
while(tmp->data!=num && tmp!=NULL){
prev = tmp;
tmp = tmp->next;
}
if(tmp == NULL){
return;
}
prev->next = tmp->next;
free(tmp);
}
Those are my other function:
void begin(node **head){
node *new;
int num;
new = malloc(sizeof(node));
if(new == NULL){
perror("malloc");
EXIT_FAILURE;
}
printf("Insert number at the beginning: ");
scanf("%d",&num);
new->data = num;
new->next = *head;
*head = new;
}
void end(node **head){
int num;
node *tmp,*new;
new = malloc(sizeof(node));
if(new == NULL){
perror("malloc");
EXIT_FAILURE;
}
printf("Insert number at the end: ");
scanf("%d",&num);
new->data = num;
tmp = *head;
if(tmp == NULL){
tmp = malloc(sizeof(node));
tmp->data = num;
tmp->next = NULL;
}
while(tmp->next!=NULL){
tmp = tmp->next;
}
new->next = NULL;
tmp->next = new;
}
void display(node *head){
node *tmp;
if(head == NULL){
printf("Empty list");
}
else{
while(tmp!=NULL){
printf("%d ",tmp->data);
tmp = tmp->next;
}
}
}
temp != NULL condition should be ahead of temp->data != num inside while loop.
Because if the temp reaches to the end of list without any match then temp will be NULL and you can't have check value(temp->data != num) for NULL pointer.
Ok i understand the problem. I forgot to add tmp = head in the else{} condition in the display() function

double linked list with c

ok, now i have to make double linked list on c.
there are 7 functions which act on main.
append.
insertAt.
deleteAt.
print.
print_revers.
newnode.
newDLL.
i can amend only 5 functions
append, insertAt, deleteAt, print, print_reverse
finally i can make append,print,print_reverse
however, i can't make insertAt,deleteAt, because of index.
1. i can't understand why the code
else {
while (index-- >= 0) {
temp = temp->next;
}
make memory collide. for using index, i need to move node to collect position and connect to newnode. but it doesn't work...
2. also what's return; 's role? i have not seen such type of return.
3. how can i make deleteAt using index? i think deleteAt and insertAt have quiet similar algoritum. so i try to make insertAt first and deleteAt last. but what i write doesn't work well..
i can find a lot of data of doublelinkedlist on internet. but i can't find using index.... i have been listening c language lecture on only two month, so sorry about the spagettii code...
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int val;
struct Node *prev;
struct Node *next;
} Node;
typedef struct {
Node *head;
int size;
} DLL;
Node *newnode(int n)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->val = n;
temp->prev = NULL;
temp->next = NULL;
return temp;
}
DLL *newDLL() {
DLL *temp = (DLL *)malloc(sizeof(DLL));
temp->head = NULL;
temp->size = 0;
return temp;
}
void append(DLL *list, Node *newnode) {
struct Node* temp = list->head;
struct Node* newNode = newnode;
if (list->head == NULL) {
list->head = newNode;
list->size++;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
list->size++;
temp->next = newNode;
newNode->prev = temp;
}
void insertAt(DLL *list, int index, Node *newnode) {
struct Node* temp = (Node *)malloc(sizeof(Node));
if (index < 0 || index >= list->size + 1) {
printf("out of range\n");
}
else if (index == 0) {
newnode->next = list->head;
list->head->prev = newnode;
list->head = newnode;
}
else {
while (index-- >= 0) {
temp = temp->next;
}
temp->val = newnode->val;
temp->next = list->head->next;
list->head->next = temp;
temp->prev = list->head;
if (temp->next != NULL)
temp->next->prev = temp;
}
}
void deleteAt(DLL *list, int index) {
//save reference to first link
struct Node* temp = (Node *)malloc(sizeof(Node));
//if only one link
if (list->head->next == NULL) {
list->head->prev = NULL;
}
else {
list->head->next->prev = NULL;
}
list->head = list->head->next;
//return the deleted link
return;
}
void print(DLL *list) {
struct Node* temp = list->head;
printf("Forward: ");
while (temp != NULL) {
printf("[%d] ", temp->val);
temp = temp->next;
}
printf("\n");
}
void print_reverse(DLL *list) {
struct Node* temp = list->head;
if (temp == NULL) return; // empty list, exit
// Going to last Node
while (temp->next != NULL) {
temp = temp->next;
}
// Traversing backward using prev pointer
printf("Reverse: ");
while (temp != NULL) {
printf("[%d] ", temp->val);
temp = temp->prev;
}
printf("\n");
}
int main() {
DLL *list = newDLL();
int i;
for (i = 1; i < 6; i++) {
append(list, newnode(i));
}
print(list);
deleteAt(list, -1);
deleteAt(list, 5);
deleteAt(list, 0);
print(list);
deleteAt(list, 2);
print(list);
deleteAt(list, 2);
print(list);
insertAt(list, -1, newnode(6));
insertAt(list, 3, newnode(6));
insertAt(list, 0, newnode(7));
print(list);
insertAt(list, 1, newnode(8));
print(list);
insertAt(list, 4, newnode(9));
print(list);
print_reverse(list);
return 0;
}
The part where you insert at index has problems:
temp = malloc is wrong, it should start with temp = head.
in the insertion:
temp->val = newnode->val;
temp->next = list->head->next;
list->head->next = temp;
temp->prev = list->head;
temp->next should not be head->next, it should be newnode. newnode->next should be temp->next etc.

why is the below code not scanning all inputs properly?

This code is a solution to the given problem :
https://www.hackerearth.com/international-women-hackathon-2016/algorithm/jp-and-rotations/
Issue is after I give m operations as R 1 ,L 2 ,L 1 one below other as specified in the sample input , it doesn't take any further input and directly prints output as 2 , I am unable to get the mistake I am making here ,please help me out .
#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data ;
struct node *next,*prev;
};
int main()
{
int n ,m,a,count=0,i,j;
char rot;
scanf("%d %d ", &n ,&m);
struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2;
if(head1==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head1=temp;
ptr=head1;
head1->prev=NULL;
}
for(i=1;i<=n-1;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
ptr->next=temp;
temp->prev=ptr;
ptr=temp;
temp->next=NULL;
}
rear1=ptr;
temp=NULL;
ptr=NULL;
fflush(stdout);
if(head2==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head2=temp;
ptr=head2;
}
for(i=1;i<=n-1;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
ptr->next=temp;
ptr=temp;
ptr->next=NULL;
}
rear2=ptr;
ptr=NULL;temp=NULL;
fflush(stdout);
for(i=0;i<m;i++)
{
scanf("%c %d",&rot,&a);
fflush(stdout);
if(rot=='L')
{
ptr=head1;
for(j=0;j<a;j++)
{
temp=ptr->next;
rear1->next=ptr;
ptr->prev=rear1;
rear1=ptr;
head1=temp;
ptr=head1;
}
count++;
ptr=NULL;
temp=NULL;
if(head1->data==head2->data && rear1->data==rear2->data)
{
break;
}
}
else if(rot=='R')
{
temp=head1;
ptr=rear1->prev;
for(j=0;j<a;j++)
{
temp->prev=rear1;
rear1->prev->next=NULL;
rear1->next=temp;
head1=rear1;
temp=head1;
rear1=ptr;
ptr=rear1->prev;
}
count++;
temp=NULL;
ptr=NULL;
if(head1->data==head2->data && rear1->data==rear2->data)
{
break;
}
}
}
printf("%d",count);
return 0;
}
Try removing space after the format specifier in the scanf statement.
It will not give you correct final output.But,I think thats what you needed to know.
Try this code:
#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data;
struct node *next, *prev;
};
int main()
{
int n, m, a, count = 0, i, j;
char rot;
scanf("%d %d", &n, &m);
struct node *head1 = NULL, *rear1 = NULL, *ptr = NULL, *temp = NULL, *head2 = NULL, *rear2 = NULL;
if (head1 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head1 = temp;
ptr = head1;
head1->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
ptr->next = temp;
temp->prev = ptr;
ptr = temp;
temp->next = NULL;
}
ptr->next = head1;
head1->prev = ptr;
rear1 = ptr;
temp = NULL;
ptr = NULL;
fflush(stdout);
fflush(stdin);
if (head2 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head2 = temp;
ptr = head2;
head2->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
ptr->next = temp;
temp->prev = ptr;
ptr = temp;
ptr->next = NULL;
}
ptr->next = head2;
head2->prev = ptr;
rear2 = ptr;
ptr = NULL; temp = NULL;
fflush(stdout);
fflush(stdin);
for (i = 0; i<m; i++)
{
scanf("%c %d", &rot, &a);
fflush(stdout);
fflush(stdin);
if (rot == 'L')
{
ptr = head1;
for (j = 0; j<a; j++)
{
temp = ptr->next;
rear1->next = ptr;
ptr->prev = rear1;
rear1 = ptr;
head1 = temp;
ptr = head1;
}
count++;
ptr = NULL;
temp = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
else if (rot == 'R')
{
temp = head1;
ptr = rear1->prev;
for (j = 0; j<a; j++)
{
temp->prev = rear1;
rear1->prev->next = NULL;
rear1->next = temp;
head1 = rear1;
temp = head1;
rear1 = ptr;
ptr = rear1->prev;
}
count++;
temp = NULL;
ptr = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
}
printf("%d", count);
return 0;
}

when i am selecting delete option before inserting any value in linked list i am getting segmentation fault error

#include<stdio.h>
#include<malloc.h>
typedef struct nnode
{
int value;
struct nnode *next;
} node;
void insert(node **ptr, int val) //for insertion
{
node *temp, *temp2;
temp = *ptr;
if (*ptr == NULL)//if list is empty
{
temp = (node *) malloc(sizeof (node));
temp->value = val;
temp->next = NULL;
*ptr = temp;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp2 = (node *) malloc(sizeof (node));
temp2->value = val;
temp2->next = NULL;
temp->next = temp2;
}
}
void display_node(node **ptr)
{
node *temp;
temp = *ptr;
while (temp != NULL)
{
printf("%d--->", temp->value);
temp = temp->next;
}
printf("null");
}
void de_node(node **ptr)
{
int val;
node *temp, *temp2;
temp = *ptr;
temp2 = temp->next;
printf("\nenter the value to be deleted\n");
scanf("%d", &val);
if ((*ptr) == NULL)
{
printf("list is empty .....");
return;
}
else if ((*ptr)->value == val)
{
*ptr = (*ptr)->next;
free(temp);
}
else
{
while ((temp->next->value != val)&&(temp2->next != NULL))
{
temp = temp->next;
temp2 = temp->next;
}
if (temp2->next == NULL)
{
printf("\nvalue not found");
}
if (temp->next->value == val)
{
temp->next = temp2->next;
free(temp2);
}
}
}
void main()
{
node *head = NULL;
int ch;
int n;
while (1)
{
printf("\nenter your choice\n");
printf("1.ADD ELEMENT\n");
printf("2.DELETE ELEMENT\n");
printf("3.DISPLAY LIST\n");
printf("4.EXIT\n");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\n enter data \n");
scanf("%d", &n);
insert(&head, n);
display_node(&head);
break;
case 2:de_node(&head);
display_node(&head);
break;
case 3:display_node(&head);
break;
case 4:exit(0);
}
}
}
my problem is before inserting anything in list when i am deleting element
i.e when i am trying to delete an element when list is empty
then according to my code it should print "list is empty....."
but instead it is giving segmentation fault error.
The problem is here:
temp = *ptr;
temp2 = temp->next;
You have temp set to *ptr, but you haven't yet checked if *ptr is NULL. So when you try to dereference it with temp->next, you get the segfault.
Since you're not using temp2 until later, move this line right before your while loop:
temp2 = temp->next;

Resources