Hello,

I got working code of Linked List here. But I having a problem with adding an item.

I added four numbers in the Linked List. First, I added 2 then 8 then 4 and the last one 16. The result would be like this:
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/display-1.jpg[/IMG]

What I want to happen is, whatever the first item that I added in the list would be the first list in the list and so on.

So if I added "2,8,4,16" the result should be:
The linked list contains: 2
The linked list contains: 8
The linked list contains: 4
The linked list contains: 16

NOT like this:
The linked list contains: 16
The linked list contains: 4
The linked list contains: 8
The linked list contains: 2

Here's my code:

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

typedef struct listel
{
 int val;
 struct listel *next;
 struct listel *curr;
 struct listel *head;
 struct listel *temp;
 struct listel *q;
 struct listel *m;
 struct listel *search;
}item;

void display(item *curr,item *head);
struct listel *search(item *head);
void delete(item *curr,item *head,int x);

int main()
{
 int i,cho,x,pop=0;
 item *head,*curr;
 head=NULL;
 clrscr();
 do
 {
  printf("\n\n [1] Add item\n");
  printf(" [2] Display items\n");
  printf(" [3] Search an item\n");
  printf(" [4] Delete an item\n");
  printf(" [5] Exit\n\n");
  printf(" Enter your choice:");
  scanf("%3d",&cho);
  i = getchar();
  if(cho==1)
  {
   clrscr();
   printf("\n Enter the element you want to add: ");
   scanf("%3d",&x);
   curr=(item*)malloc(sizeof(item));
   curr->val = x;
   curr->next = head;
   head=curr;
   pop=1;
  }
  else if(cho==2 && pop)
  {
   display(curr,head);
  }
  else if(cho==3 && pop)
  {
   search(head);
  }
  else if(cho==4 && pop)
  {
   delete(head,curr,x);
  }
  else
  {
   clrscr();
   printf("  _____________________\n |  \t\t       |");
   printf("\n | The list is empty.  |\n");
   printf(" |_____________________|");
  }
 }
 while(cho != 5);
 {
  clrscr();
  printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t    ___________________\n\n\t\t\t    Press Enter to Exit");
  printf("\n\t\t\t    ___________________");
  i = getchar();
  ++i;
 }
 clrscr();
 return 0;
}

void display(item *curr,item *head)
{
 curr=head;
 clrscr();
 while(curr != NULL)
 {
  printf("\n The linked list contains:%3d",curr->val);
  curr=curr->next;
 }
}

struct listel *search(item *head)
{
 int x,found=0;
 item *temp;
 temp=head;
 clrscr();
 printf("\n Enter the data that you want to search: ");
 scanf("%d",&x);
 while(temp)
 {
  if(temp->val==x)
  {
   found=1;
  }
  temp=temp->next;
 }
 if(found == 1)
 {
  printf("\n %d is found in the list.",x);
 }
 else
 {
  printf("\n %d is not found in the list.",x);
 }
 return(temp);
}

void delete(item *curr,item *head,int x)
{
 item *temp,*m;
 temp=curr;
 clrscr();
 printf("\n Enter the element to delete: ");
 scanf("%d",&x);
 while(temp)
 {
  if(temp->val == x)
  {
   printf("\n %d has been deleted.",x);
   if (temp==head)
   {
    head=temp->next;
    free(temp);
    return;
   }
   else
   {
    m->next=temp->next;
    free(temp);
    return;
   }
  }
  else
  {
   m=temp;
   temp=temp->next;
  }
 }
 printf("\n No such entry to delete.");
}

It appears the problem is that new numbers are being added to the head of the list, and you need them added to the tail. Change lines 44 and 45 to add the new node to the end of the list.

Hello Ancient Dragon,

Can I reverse the display output instead of adding?

If you are adding nodes at the head every time and want to print it reverse you can have a double linked list for the purpose, since you will have to traverse backwards.

But its far more easy and convenient to add at the end of the list.

Though I didn't get your structure "listel". I mean what for have you taken that many structure pointers, where only *next is sufficient ???

@kings_mitra
You mean, we can't do that in Singly Linked List? Why?

Why? You have only one link -- next. How are you supposed to get from the current node to the previous node unless there is another link that points backwards in the list? To print your linked list backwards you have to start at the last node (tail) and work backwards towards the beginning.

Its a whole lot easier to just add the new nodes to the end of the existing list.

Ahh ok, thanks for the explanation Ancient Dragon.

Here's the code again, I modified something in the adding part:

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

typedef struct listel
{
 int val;
 struct listel *next;
 struct listel *curr;
 struct listel *head;
 struct listel *temp;
 struct listel *tempo;
 struct listel *tempe;
 struct listel *q;
 struct listel *m;
 struct listel *search;
}item;

void display(item *curr,item *head);
struct listel *search(item *head);
void delete(item *curr,item *head,int x);

int main()
{
 int i,cho,x,pop=0;
 item *head,*curr,*tempo,*tempe;
 head=NULL;
 clrscr();
 do
 {
  printf("\n\n [1] Add item\n");
  printf(" [2] Display items\n");
  printf(" [3] Search an item\n");
  printf(" [4] Delete an item\n");
  printf(" [5] Exit\n\n");
  printf(" Enter your choice:");
  scanf("%3d",&cho);
  i = getchar();
  if(cho==1)
  {
   clrscr();
   printf("\n Enter the element you want to add: ");
   scanf("%3d",&x);
   curr=(item*)malloc(sizeof(item));
   curr->val = x;
   if(head == NULL)
   {
    head=tempo;
    curr=head;
   }
   else
   {
    tempe = head;
    while(tempe->next != NULL)
    {
     tempe = tempe->next;
    }
    tempe->next = tempo;
   }
   pop=1;
  }
  else if(cho==2 && pop)
  {
   display(curr,head);
  }
  else if(cho==3 && pop)
  {
   search(head);
  }
  else if(cho==4 && pop)
  {
   delete(curr,head,x);
  }
  else
  {
   clrscr();
   printf("  _____________________\n |  \t\t       |");
   printf("\n | The list is empty.  |\n");
   printf(" |_____________________|");
  }
 }
 while(cho != 5);
 {
  clrscr();
  printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t    ___________________\n\n\t\t\t    Press Enter to Exit");
  printf("\n\t\t\t    ___________________");
  i = getchar();
  ++i;
 }
 clrscr();
 return 0;
}

void display(item *curr,item *head)
{
 curr=head;
 clrscr();
 printf("\n The linked list contains: ");
 printf("Head");
 while(curr != NULL)
 {
  printf("->%d",curr->val);
  curr=curr->next;
 }
 printf("->Null");
}

struct listel *search(item *head)
{
 int x,found=0;
 item *temp;
 temp=head;
 clrscr();
 printf("\n Enter the data that you want to search: ");
 scanf("%d",&x);
 while(temp)
 {
  if(temp->val==x)
  {
   found=1;
  }
  temp=temp->next;
 }
 if(found == 1)
 {
  printf("\n %d is found in the list.",x);
 }
 else
 {
  printf("\n %d is not found in the list.",x);
 }
 return(temp);
}

void delete(item *curr,item *head,int x)
{
 item *temp,*m;
 temp=curr;
 clrscr();
 printf("\n Enter the element to delete: ");
 scanf("%3d",&x);
 while(temp)
 {
  if(temp->val == x)
  {
   printf("\n %d has been deleted.",x);
   if (temp==head)
   {
    head=temp->next;
    free(temp);
    return;
   }
   else
   {
    m->next=temp->next;
    free(temp);
    return;
   }
  }
  else
  {
   m=temp;
   temp=temp->next;
  }
 }
 printf("\n No such entry to delete.");
}

Edited 6 Years Ago by SHENGTON: n/a

The problem is, when I added the second items the program will stop at this part:

Enter the element you want to add: 4

You mean, we can't do that in Singly Linked List? Why?

I think AD has given the answer for you.
Now, if you have got the explanation correctly the next code post was supposed to have that change.... which I didn't get :(
So, update your code to add the node at the end of the list.
Believe me, that is the best option to generalize the concept of linked list you are using.

Moreover, you have used flag to check for head entry, whereas you are updating the flag value only once. Check what your program responds when you delete the head element or when you delete all the elements and try to display.

Then, construct a separate function for adding elements and use switch case construct in main for the input selection.

When you are adding elements at the end you only need to have a check for the first element, so as to maintain the head pointer.
While deleting the head element or the last element(only one remaining) only you need to adjust the head pointer.

Try it out and post the updated code...;)

This article has been dead for over six months. Start a new discussion instead.