C Linked List 29: Sort a given linked list by bubble sort [C Programming]

preview_player
Показать описание
Sort a given linked list by bubble sort.

Expected Output :
Input the number of elements in the linked list : 5
Input the elements in the linked list :
15
33
49
6
65

Sorted order is :
6 15 33 49 65

=================================================

Рекомендации по теме
Комментарии
Автор

That keyboard sound and typing satisfies so much

lacuna
Автор

Why do you set nodectr= numberofnodes - 2?

rafaelmora
Автор

Thanks a lot. For some reason I made my self suffered so bad for hours XD. Now just realised that swapping data is enough XD. Ty again

dokusan
Автор

so we just swap the node data and no need to swap the node address right ?

huycao
Автор

Thanks a It really helped 😊 more power.

veejayrecana
Автор

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef char data;

struct linked_list
{
data d;
struct linked_list *next;
};

typedef struct linked_list element;
typedef element *link;

link string_to_list (char s[]);
int count(link head);
link lookup(data c, link head);
void delete_item(data c, link head);
void display(link head);

main()
{
char ch, str[100];
int choice;
link current_loc;

link head = NULL;
do{
printf("\nBasic List Operations\n");
printf("[1] Creating a list\n");
printf("[2] Counting elements\n");
printf("[3] Looking up an element\n");
printf("[4] Deleting an elemenet\n");
printf("[5] Display elements in the list\n\n");
printf("[6] Display in ascending order\n");
scanf("%d", &choice);

switch(choice)
{
case 1: printf("\nInput a string:");


= string_to_list(str);


case 2: display(head);
list has %d elements.", count(head));

case 3: if(head == NULL)
is empty");


Character to lookup in the list");

= getchar();
= lookup(ch, head);
== NULL)
%c not in the list.", ch);

is in the list.", current_loc->d);


case 4: if(head == NULL)
is empty");


character to delete in the list:");

= getchar();
== head->d)

= head;
= head->next;



head);



case 5: display(head);

case 6: display(head);
default: printf("\nInvalid choice");
}
printf("\n\nPress any key to try again and [x] to terminate");
fflush(stdin);
ch = getch();
}while(toupper(ch) != 'X');
return 0;
}
link string_to_list(char s[])
{
link head;
if(s[0] == '\0')
return NULL;
else
{
head = malloc(sizeof(element));
head->d = s[0];
head->next = string_to_list(s + 1);
return head;
}
}

int count(link head)
{
link temp = head;
if(temp == NULL)
return 0;
else
return(1 + count(temp->next));
}

void display(link head)
{
link temp = head;
if(temp == NULL)
printf("\nList is empty\n");
else
{
printf("\nElements in the list\n");
while(temp != NULL)
{
printf("%c ", temp->d);
temp = temp->next;
}
}
}

link lookup(data c, link head)
{
if(head == NULL)
return NULL;
else if(c == head->d)
return head;
else
return(lookup(c, head->next));
}

void delete_item(data c, link head)
{
link node_to_delete = lookup(c, head);
if(node_to_delete == NULL)
{
printf("%c is not in the list.\n", c);
return;
}
while(head->next != node_to_delete)
head = head->next;
head->next = head->next->next;
free(node_to_delete);
}
im trying to add an ascending and descending code to this, can you hlep me out?

SpiiiiiceeMAN
visit shbcf.ru