Here is my code:
#include<stdio.h>
#include<stdlib.h>
typedef struct list_node
{
int data;
struct list_node *next;
} node;
node *head,*temp;
int counter;
void display_menu();
char get_choice();
void process(char option);
void Insert_first();
void Print_first();
void Delete_first();
void Delete_end(); // Problem
void Insert_end(); // Problem
void Search();
int main()
{
char choice;
display_menu();
do
{
choice=get_choice();
process(choice);
} while(choice!='J');
return 0;
}
void display_menu()
{
printf("What do you want do do?\n");
printf("A. Insert a node at the beginning of the list\n");;
printf("B. Delete the first node of the list\n");
printf("C. Print list\n");
printf("D. Insert end\n");
printf("E. Delete the last node of the list\n");
printf("F. Search\n");
}
char get_choice()
{
char select;
printf("Enter your choice:\t");
scanf("%c",&select);
getchar();
printf("\n");
return select;
}
void process(char option)
{
switch(option)
{
case 'A': Insert_first();break;
case 'B': Delete_first();break;;
case 'C': Print_first();break;
case 'D': Insert_end();break;
case 'E': Delete_end();break;
case 'F': Search(); break;
default:
printf("Invalid input!\n\n");
}
}
void Search()
{
node *a;
node *b;
int i, j;
a=head;
b=head;
for(i = 1; i < counter - 1; ++i){
a = a -> next;
for(j = i + 1; j < counter; ++j){
b = b -> next;
if((a -> data) == (b -> data)){
printf("0\n");
}
else{
printf("1\n");
}
}
}
counter++;
}
void Insert_first()
{
temp=(node *)malloc(sizeof(node));
printf("Enter a number:\t");
scanf("%d",&(temp->data));
getchar();
printf("\n");
temp->next=head;
head=temp;
counter++;
}
void Insert_end()
{
temp=(node *)malloc(sizeof(node));
node *x;
int y;
printf("Enter a number:\t");
scanf("%d",&(temp->data));
getchar();
printf("\n");
x=head;
for(y = 1; y < counter; ++y){
x=x->next;
}
temp->next=x->next;
x->next = temp;
counter++;
}
void Print_first()
{
if(head==NULL)
{
printf("The list is empty\n");
}
else
{
temp=head;
printf("-------------------------------------------------\n");
do
{
printf("\t%d\t", temp->data);
temp=temp->next;
} while(temp!=NULL);
printf("\n");
printf("-------------------------------------------------\n\n");
}
}
void Delete_first()
{
if(head==NULL)
{
printf("\nThe list is empty\n");
}
else
{
temp=head;
head=temp->next;
free(temp);
counter--;
}
}
void Delete_end()
{
node *x, *z;
int y;
if(head == NULL)
printf("The list is empty");
else{
x=head;
for(y = 1; y < counter; ++y){
x=x->next;
}
z=x;
x=x->next;
free(z);
counter--;
}
}
I have two problems to solve:
- The
Delete_end()
function; - The
Search()
function.
I want the Delete_end()
function to act as Delete_first()
function, but instead of deleting the FIRST node and deallocates its space, it does these to the END node. I tried to code these functions, but it didnt return what I want it to be.
If we run the codes, and Enter the Following:
As you notice when I delete the first node (Switch Choice: B), the list from
--------------------------------
4 3 2
--------------------------------
becomes
--------------------------------
3 2
--------------------------------
Thus, it deletes the first node and deallocates the space. Now when I delete the end node (Switch Choice: E) and print the list (Switch Choice: C), the list becomes
--------------------------------
3 135704608
--------------------------------
I think the number 135704608 is the location in the memory. So what happen here is it didnt delete the last node and deallocates it but instead returns 135704608. I hope you can help me with that.
Second function is the Search()
, this function should return 0 if the number is already in the list, else 1 is returned. Here is the Search()
function from the above codes:
void Search()
{
node *a;
node *b;
int i, j;
a=head;
b=head;
for(i = 1; i < counter - 1; ++i){
a = a -> next;
for(j = i + 1; j < counter; ++j){
b = b -> next;
if((a -> data) == (b -> data)){
printf("0\n");
}
else{
printf("1\n");
}
}
}
counter++;
}
Any help is greatly appreciated!