my code is supposed to find all the prime numbers between 1879 and 9987. everything seems to be working as intended except for when i go to display the linked list. it will display the first 6 elements roughly (it is random how far it will display each time the program is run). as you can see by running the code that it is printing all of the prime numbers required, it just doesn't store them all. any help is appreciated. I am using Dev C for my compiler.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}n;
n *head = NULL;
void insert(int);
void display();
void erase();
void enqueue(int);
void pop();
void po$p();
void search(int);
void isprime();
int main(void)
{
int num,count, choice;
isprime();
while(1)
{
printf("\n 1. To display>");
printf("\n 2. To Delete the list>");
printf("\n 3. To insert at the end>");
printf("\n 4. Pop out of the stack>");
printf("\n 5. To delete at the back of the list>");
printf("\n 6. To search a particular node>");
printf("\n 7. To exit>");
printf("\n Enter your choice>");
scanf("%d",&choice);
switch(choice)
{
case 1:
display();
break;
case 2:
erase();
break;
case 3:
printf("Enter the data you want to insert at the end>");
scanf("%d",&num);
enqueue(num);
break;
case 4:
pop();
break;
case 5:
po$p();
break;
case 6:
printf("Enter the node you want to search>");
scanf("%d",&num);
search(num);
break;
case 7:
exit(0);
}
}
return 0;
}
void insert(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
temp->data = X;
temp->link = NULL;
if(head == NULL)
head = temp;
else
{
temp->link = head;
head = temp;
}
}
void display()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
printf("\n There is no list");
else
{
temp = head;
printf("\n head->");
while(temp != NULL)
{
printf("%d->",temp->data);
temp = temp->link;
}
printf("NULL");
}
}
void erase()
{
head = NULL;
}
void enqueue(int X)
{
n *temp,*newnode;
newnode = (n*)malloc(sizeof(n*));
newnode->data = X;
newnode->link = NULL;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
head = newnode;
else
{
temp = head;
while(temp->link != NULL)
temp = temp->link;
temp->link = newnode;
}
}
void pop()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to pop");
}
else
{
temp = head;
printf("\n element popped is %d",temp->data);
head = head->link;
free(temp);
}
}
void po$p()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to po$p");
}
else
{
temp = head;
while(temp->link->link != NULL)
temp = temp->link;
printf("\n Element po$ped is %d",temp->link->data);
temp->link = NULL;
}
}
void search(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to search");
}
else
{
temp = head;
while(temp->data != X && temp->link != NULL)
temp = temp->link;
if(temp->data == X)
printf("\n item in the list");
else if(temp->link == NULL)
printf("\n item is not in the list");
}
}
void isprime(){
int count,i,x;
for(x = 1879;x<=9987;x++){
count = 0;
for(i=2;i<=x/2;i++){
if(x%i==0){
count++;
break;
}
}
if(count==0 && x!= 1){
insert(x);
printf("%d",x);
}
}
}
Related
I am trying to write a code to implement a linked list. The program inserts a value taken from the user into a linked list until the user exits execution. The value can be inserted into the head, tail or any custom position entered by the user.
Upon compiling the program gives the error "Return value ignored: 'scanf'" on every scanf. It also gave an error saying scanf is unsafe, use scanf_s instead. I resolved that by adding '_CRT_SECURE_NO_WARNINGS' to preprocessor definitions. Also please point out if you find any other mistakes.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
} node;
typedef enum position { First = 1, Custom, Last} position;
typedef enum process {Exit, Run} process;
node* insertFirst(node* ptrHead, int val)
{
node* temp = NULL;
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
{
exit(1);
}
else
{
if (ptrHead == NULL)
{
newNode->data = val;
newNode->next = NULL;
ptrHead = newNode;
}
else
{
temp = ptrHead;
newNode->data = val;
newNode->next = temp;
ptrHead = newNode;
}
}
return ptrHead;
}
node* insertPos(node* ptrHead, int val, int loc)
{
node* temp = ptrHead;
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
{
exit(1);
}
for (int i = 0; i < loc; i++)
{
temp = temp->next;
}
newNode->data = val;
newNode->next = temp->next;
temp->next = newNode;
return ptrHead;
}
node* insertLast(node* ptrHead, int val)
{
node* temp = ptrHead;
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
{
exit(1);
}
newNode->data = val;
newNode->next = NULL;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
return ptrHead;
}
void printList(node* ptrHead)
{
node* temp = ptrHead;
while (temp->next != NULL)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
int countNodes(node* ptrHead)
{
node* temp = ptrHead;
int i = 0;
while (temp->next != NULL)
{
temp = temp->next;
i++;
}
return i;
}
int main()
{
node* ptrHead = NULL;
position location;
int num = 0;
int pos = 0;
process state = Run;
while (state == Run)
{
printf(" Welcome to my Linked List program. \n");
if (ptrHead == NULL)
{
printf("Enter first element of the list \n");
scanf("%d", &num);
ptrHead = insertFirst(ptrHead, num);
}
else
{
printf(" Enter number to choose location: \n 1 -> First\n 2-> Custom\n 3-> Last\n");
scanf("%d", &pos);
if (pos == Custom)
{
printf("Please enter the number and the placement\n");
scanf("%d", &num);
scanf("%d", &location);
if (location > countNodes(ptrHead))
{
printf("Enter position less than %d\n", countNodes(ptrHead));
}
else
{
ptrHead = insertPos(ptrHead, num, location);
}
}
else if (pos == First)
{
printf("Please enter the number\n");
scanf("%d", &num);
ptrHead = insertFirst(ptrHead, num);
}
else if (pos == Last)
{
printf("Please enter the number\n");
scanf("%d", &num);
ptrHead = insertLast(ptrHead, num);
}
else
{
printf("Wrong position entry\n");
}
}
printList(ptrHead);
printf("Enter '1' to insert another value and '0' to quit execution\n");
scanf("%d", &state);
}
return 0;
}
Few days back I made a Singly Linked list Implementation Program using C with operations - insertion at front, insertion at back , deletion at back , deletion at front and display which was working fine.
Yesterday I made another program to insert a node at certain position in singly linked list adding extra function - insertion at position , and rest functions were copied from previous code which were working fine in previous program. Now when I was performing operations on old functions in new programs which were working fine in old program , they were showing abnormal behaviour.
Note:
I am getting error in old functions not new function.
Old Program is Running Fine
Error: when I am using inserting two or more values in linked list using either of function - insertion at front or insertion at back and then using display function. Infinite loop is running
//Program Which was working fine
//Linked List Implementation
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}node;
void add_front(node **head)
{
int value;
if((*head) == NULL)
{
*head = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
(*head)->data = value;
(*head)->link = NULL;
}
else
{
node *temp;
temp = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = *head;
*head = temp;
free(temp);
}
}
void add_end(node **head)
{
int value;
if((*head) == NULL)
{
*head = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
(*head)->data = value;
(*head)->link = NULL;
}
else
{
node * temp = malloc(sizeof(node));
node * ptr = *head;
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = NULL;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
free(temp);
free(ptr);
}
}
void del_front(node **head)
{
if((*head) != NULL)
{
node *temp;
temp = *head;
printf("Value of deleted node is %d \n",(*head)->data);
*head = (*head)->link;
free(temp);
}
else
{
printf("Linked list is Empty...\n");
}
}
node *del_end(node *head)
{
if(head == NULL)
{
printf("Linked List is Empty...\n");
return NULL;
}
else if (head->link == NULL)
{
printf("Value of deleted node is %d\n",head->data);
free(head);
head = NULL;
return head;
}
else
{
node * ptr1 = head;
node * ptr2 = NULL;
while(ptr1->link != NULL)
{
ptr2 = ptr1;
ptr1 = ptr1->link;
}
printf("Value of deleted node is %d\n",ptr1->data);
free(ptr2->link);
ptr2->link = NULL;
return head;
free(ptr1);
free(ptr2);
}
}
void display(node *head)
{
if(head == NULL)
{
printf("Linked List is Empty...\n");
}
else
{
while(head != NULL)
{
printf("%d ",head->data);
head = head->link;
}
printf("\n");
}
}
int main()
{
node *head = NULL;
int choice;
printf("Linked list Implementation...\n\n");
printf("Enter \n1. To add next node at beginning of the linked list\n");
printf("2. To add next node at end of the linked list\n");
printf("3. To delete first node\n");
printf("4. To delete last node\n");
printf("5. To display the linked list\n");
printf("6.Exit\n");
do
{
printf("Enter your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
add_front(&head);
break;
case 2:
add_end(&head);
break;
case 3:
del_front(&head);
break;
case 4:
head = del_end(head);
break;
case 5:
display(head);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Wrong Choice...\n");
}
}while(choice!=6);
free(head);
return 0;
}
//program having error
// inserting a node at a given position in singly linked list
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}node;
void add_front(node **head)
{
int value;
if((*head) == NULL)
{
*head = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
(*head)->data = value;
(*head)->link = NULL;
}
else
{
node *temp;
temp = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = *head;
*head = temp;
free(temp);
}
}
void add_end(node **head)
{
int value;
if((*head) == NULL)
{
*head = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
(*head)->data = value;
(*head)->link = NULL;
}
else
{
node * temp = malloc(sizeof(node));
node * ptr = *head;
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = NULL;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
free(temp);
free(ptr);
}
}
void del_front(node **head)
{
if((*head) != NULL)
{
node *temp;
temp = *head;
printf("Value of deleted node is %d \n",(*head)->data);
*head = (*head)->link;
free(temp);
}
else
{
printf("Linked list is Empty...\n");
}
}
node *del_end(node *head)
{
if(head == NULL)
{
printf("Linked List is Empty...\n");
return NULL;
}
else if (head->link == NULL)
{
printf("Value of deleted node is %d\n",head->data);
free(head);
head = NULL;
return head;
}
else
{
node * ptr1 = head;
node * ptr2 = NULL;
while(ptr1->link != NULL)
{
ptr2 = ptr1;
ptr1 = ptr1->link;
}
printf("Value of deleted node is %d\n",ptr1->data);
free(ptr2->link);
ptr2->link = NULL;
return head;
free(ptr1);
free(ptr2);
}
}
node *insert_pos(node *head)
{
if(head == NULL)
{
printf("Linked List is Empty...\n");
return;
}
int pos;
node *temp = malloc(sizeof(node));
printf("Enter the position to insert new node : ");
scanf("%d",&pos);
if(pos == 1)
{
printf("Enter the value to insert : ");
scanf("%d",&(temp->data));
temp->link = head;
head = temp;
return head;
}
int n = 1;
while(pos!=2)
{
if(head->link == NULL)
{
printf("Linked List has only %d node\n",n);
return;
}
head = head->link;
n++;
pos--;
}
node *ptr = malloc(sizeof(node));
ptr = head->link;
printf("Enter the value to insert : ");
scanf("%d",&(temp->data));
head->link = temp;
temp->link = ptr;
free(ptr);
}
node *delete_pos(node *head)
{
return;
}
void display(node *head)
{
if(head == NULL)
{
printf("Linked List is Empty...\n");
}
else
{
while(head != NULL)
{
printf("%d ",head->data);
head = head->link;
}
printf("\n");
}
}
int main()
{
node *head = NULL;
int choice;
printf("Linked list Implementation...\n\n");
printf("Enter \n1. To add next node at beginning of the linked list\n");
printf("2. To add next node at end of the linked list\n");
printf("3. To delete first node\n");
printf("4. To delete last node\n");
printf("5. To insert a node at certain position\n");
printf("6. To delete a node at certain position\n");
printf("7. To display the linked list\n");
printf("8.Exit\n");
do
{
printf("Enter your choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
add_front(&head);
break;
case 2:
add_end(&head);
break;
case 3:
del_front(&head);
break;
case 4:
head = del_end(head);
break;
case 5:
head = insert_pos(head);
break;
case 6:
delete_pos(head);
break;
case 7:
display(head);
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Wrong Choice...\n");
}
}while(choice!=8);
free(head);
return 0;
}
There is already undefined behavior in the first function add_front in this code snippet
else
{
node *temp;
temp = malloc(sizeof(node));
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = *head;
*head = temp;
free(temp);
^^^^^^^^^^
}
You deleted the node that was added in the list.
And the function add_front has duplicated code.
The same problem exist with the function add_end In this function you decided to delete even two valid nodes.:)
else
{
node * temp = malloc(sizeof(node));
node * ptr = *head;
printf("Enter the value to insert : ");
scanf("%d",&value);
temp->data = value;
temp->link = NULL;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
free(temp);
^^^^^^^^^^
free(ptr);
^^^^^^^^^
}
You shall not delete nodes in these functions.
In the function del_end the code after the return statement never gets the control and must be removed.
printf("Value of deleted node is %d\n",ptr1->data);
free(ptr2->link);
ptr2->link = NULL;
return head;
free(ptr1); // <===
free(ptr2); // <===
If you want to add and delete nodes at the end of the list then you should declare a tw-sided singly-linked list.
recently i just learning about c and c++, and now i get assignment about creating simple music playlist in C using queue linked list, what i want ask why my song list not appear on the screen ?
is there something wrong with my code, pleas enlight me
i am sorry newbie on stackoverflow too, still not getting use to stackoverflow
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
int songSlot;
char* song[50]= {
"IDGAF - Dua Lipa",
"FRIENDS - Marshmello, Anne-Marie",
"The Middle - Zedd, Maren Morris, Grey",
"Best Part - H.E.R., Daniel Caesar",
"All The Stars (with SZA) - Kendrick Lamar, LZA",
"Wolves - Selena Gomez, Marshmello",
"God's Plan - Drake",
"Rewrite The Stars - Zac Efron, Zendaya",
"Havana - Camila Cabello, Young Thug",
"Perfect - Ed Sheeran"
};
void ShowSong(int _val){
switch (_val) {
case 1: printf(song[0]);
break;
}
}
void Insert() {
int val;
printf("What song number you want to add : \n");
scanf("%d",val);
ShowSong(val);
printf("Added to playlist\n");
system("pause");
//ShowSong(val);
if (rear == NULL) {
rear = (struct node*) malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else {
temp = (struct node*) malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void NextSong() {
temp = front;
if (front == NULL) {
printf("Underflow");
system("pause");
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
printf("\n");
printf("Skipping ",front->data );
int skipSongVal = front->data;
ShowSong(skipSongVal);
printf("\n");
free(front);
front = temp;
printf("\n");
printf("Now Playing ",front->data );
int nextSongVal = front->data;
ShowSong(nextSongVal);
printf("\n");
printf("\n");
system("pause");
}
else {
printf("Skipping ",front->data );
int nextSongVal2 = front->data;
ShowSong(nextSongVal2);
printf("\n");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void ClearPlaylist()
{
temp = front;
if (front == NULL) {
printf("Playlist is Already Empty\n");
system("pause");
return;
}
else
{
printf("Clearing Playlist");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void Display() {
printf("PLAYLIST ");
temp = front;
if ((front == NULL) && (rear == NULL)) {
printf("Playlist is empty\n");
return;
}
printf("Next Song is : \n");
while (temp != NULL) {
int valDisplay = temp->data;
printf("%d",valDisplay);
ShowSong(valDisplay);
printf("\n");
temp = temp->next;
}
printf("\n");
}
int main() {
int ch;
do {
system("CLS");
printf("MUSICS\n");
printf("1)\n",song[0]);
printf("2)\n",song[1]);
printf("3)\n",song[2]);
printf("4)\n",song[3]);
printf("5)\n",song[4]);
printf("6)\n",song[5]);
printf("7)\n",song[6]);
printf("8)\n",song[7]);
printf("9)\n",song[8]);
printf("10)\n",song[9]);
printf("\n");
Display();
printf("\n");
printf("1) Add Song to playlist\n");
printf("2) Skip to next song\n");
printf("3) Clear playlist\n");
printf("4) Exit\n");
printf("Enter your choice : \n");
scanf("%d",&ch);
switch (ch) {
case 1: Insert();
break;
case 2: NextSong();
break;
case 3: ClearPlaylist();
break;
case 4: printf("exit\n");
break;
default: printf("Invalid Choice");
}
} while (ch != 4);
return 0;
}
#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;
So I'm having issue with my code with the structure I'm using. I would like my structure to be able add,retrieve or sort but I'm getting a lot of problem with the structure. It work if I use only number but I need to user 3 string. One for firstname, lastname and phonenumber but I can't figure.
This is the code I'm having right now:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
char first[15];
char last[15];
char phone[12];
struct node *next;
}*head;
void append(int num, char f[15], char l[15],char p[12])
{
struct node *temp, *right;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
right = (struct node *)head;
while (right->next != NULL)
right = right->next;
right->next = temp;
right = temp;
right->next = NULL;
}
void add(int num, char f[15], char l[15],char p[12])
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
if (head == NULL)
{
head = temp;
head->next = NULL;
}
else
{
temp->next = head;
head = temp;
}
}
void addafter(int num, char f[15], char l[15],char p[12],int loc)
{
int i;
struct node *temp, *left, *right;
right = head;
for (i = 1; i<loc; i++)
{
left = right;
right = right->next;
}
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
left->next = temp;
left = temp;
left->next = right;
return;
}
void insert(int num, char f[15], char l[15],char p[12])
{
int c = 0;
struct node *temp;
temp = head;
if (temp == NULL)
{
add(num,f,l,p);
}
else
{
while (temp != NULL)
{
if (temp->data<num)
c++;
temp = temp->next;
}
if (c == 0)
add(num,f,l,p);
else if (c<count())
addafter(num,f,l,p, ++c);
else
append(num,f,l,p);
}
}
int delete(int num)
{
struct node *temp, *prev;
temp = head;
while (temp != NULL)
{
if (temp->data == num)
{
if (temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r = head;
if (r == NULL)
{
return;
}
while (r != NULL)
{
printf("%d ", r->data);
r = r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c = 0;
n = head;
while (n != NULL)
{
n = n->next;
c++;
}
return c;
}
int main()
{
int i, num;
char fname[15], lname[15], phone[12];
struct node *n;
head = NULL;
while (1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Retrieve\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if (scanf("%d", &i) <= 0){
printf("Enter only an Integer\n");
exit(0);
}
else {
switch (i)
{
case 1:
printf("Enter the id, first, last and phone (Separte with space) : ");
scanf("%d %s %s %s", &num,fname,lname,phone);
insert(num,fname,lname,phone);
break;
case 2:
if (head == NULL){
printf("List is Empty\n");
}else{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3:
//To be made
//scanf("Retrieve this : %d\n", count());
break;
case 4:
if (head == NULL){
printf("List is Empty\n");
}else{
printf("Enter the number to delete : ");
scanf("%d", &num);
if (delete(num))
printf("%d deleted successfully\n", num);
else
printf("%d not found in the list\n", num);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
Thanks for anyone that could explain me the issue and or fix it.
Everywhere you have:
temp->data = num;
add the lines
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);