Click here to Skip to main content
15,886,570 members

Error in sorted linked list C program

Member 14066698 asked:

Open original thread
Hallo everyone , i am supposed to write a program to insert integers into a sorted linked list. and then delete any element the user chooses to. in the code i am unable to insert elements into my linked list. can someone help me?


Thanks in advance

the code is
[C] sorted linked list - Pastebin.com[^]

What I have tried:

C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
 
struct node {
// int data;
int key;
struct node *next;
};
 
struct node* init(){
struct node *head =0;
 
return head;
 
}
 
int isempty(struct node * head){
 
if(head == NULL)
return 1;
else
return 2;
}
 
//display the list
void printList(struct node * head) {
 
struct node *ptr = head;
printf("\n");
int n;
n=isempty(head);
if(n == 1)
printf("list is empty");
 
//start from the beginning
while(ptr != NULL) {
printf("%d ",ptr->key);
ptr = ptr->next;
}
 
}
 
void create(struct node * head,int num) {
 
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
int n;
n=isempty(head);
if(n == 1)
head->next = new;
while(tmp != NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
if(prev !=NULL)
prev->next = new;
}
 
/* struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node ));
tmp->data=data;
tmp->link=start;
start=tmp;*/
 
//delete a link with given key
struct node* delete(struct node * head, int del) {
 
//start from the first link
struct node* current = head;
struct node* previous = NULL;
struct node* temp = NULL;
 
//if list is empty
int n;
n=isempty(head);
if(n == 1) {
printf("list is empty");
return NULL;
}
 
//navigate through list
while(current->key != del) {
if (current == head){
temp = head;
temp = temp->next;
free(head);
head = temp;
}
 
//if it is last node
if(current->next == NULL) {
 
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
 
//found a match, update the link
if(current == head) {
//change first to point to next link
head = head->next;
} else {
//bypass the current link
previous->next = current->next;
}
 
return current;
}
 
int main() {
 
int op;
int num;
struct node* head;
head=init();
 
do{
printf("\n Menu \n 1.Insert \n 2.delete element \n 3.display List \n 4. end program ");
printf("n \n \n please enter an option : ");
scanf("%d",&op);
 
switch (op) {
case 1:
printf("Enter data:");
scanf("%d",&num);
create(head, num);
break;
case 2:
printf("Enter data:");
scanf("%d",&num);
delete(head,num);
break;
case 3:
 
printList(head);
break;
case 4:
free(head);
exit(0);
 
default:
printf("\n enter an option : ");
}
}while(1);
 
}
Tags: C

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900