7 Replies - 1273 Views - Last Post: 19 August 2010 - 10:10 AM Rate Topic: -----

#1 SHENGTON  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Linked List Problem

Posted 17 August 2010 - 10:54 PM

Hello,

I'm still a novice with this C programming and I got some problems with my program. I got 4 errors and 2 warnings with my Linked List. I attached an image for you to see. Someone can help me how to solve this problem?

Here's my code:
#include<stdio.h>
#include<conio.h>

typedef struct listel
{
 int val;
 struct listel *next;
};

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

int main()
{
 int i,cho,x;
 struct listel *curr,*head,*temp,*q;
 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("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   clrscr();
   printf("\n Enter the element you want to add: ");
   scanf("%d",&x);
   add(&curr,&head);
   display(&curr,&head);
  }
  else if(cho==2)
  {
   clrscr();
   display(&curr,&head);
  }
  else if(cho==3)
  {
   clrscr();
   printf("\n Now demonstrating search: ");
   temp = search(&curr,&head);
   printf("\n Address of first occurence is  %u ",temp);
  }
  else if(cho==4)
  {
   clrscr();
   printf("\n Enter the element to delete: ");
   scanf("%d",&x);
   delete(&head,x);
  }
  else
  {
   clrscr();
   printf("  _____________________\n |  \t\t       |");
   printf("\n | The array 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 add(struct listel *curr,*head)
{
 int x;
 temp = q;
 if(curr==NULL)
 {
  curr=(listel*)malloc(sizeof(listel));
	temp = *q;
 }
 else
 {
  while((temp->next)!=NULL)
  {
   temp=temp->next;
  }
  temp->next=(listel*)malloc(sizeof(listel));
  temp=temp->next;
 }
 temp->val = x;
 temp->next = NULL;
}

void display(struct listel *curr,*head)
{
 curr=head;
 while(curr!=NULL)
 {
  printf(" Data : %d",curr->val);
  printf(" Link : %d",curr->next);
  curr=curr->next;
 }
}


struct listel *search(struct listel *curr,*head)
{
 struct listel *temp,*p;
 int num;
 temp = p;
 printf(" Enter the data that you want to search: ");
 scanf("%d",&num);
 printf("Link of temp %u", temp->link);
 while(temp->next!=NULL)
 {
  printf("In while ");
  if(temp->val==num)
  {
   return(temp);
   temp=temp->next;
  }
 }
 return(NULL);
}


void delete(struct listel *head,int x)
{
 struct listel *temp,*m;
 temp=curr;
 x= NULL;
 while(temp->next !=NULL)
 {
  if(temp->val == x)
  {
   if (temp==head)
   {
    head=temp->next;
    free(temp);
    return;
   }
   else
   {
    m->next = temp->next;
    free(temp);
    return;
   }
  }
  m=temp;
  temp=temp->next;
 }
 printf(" No such entry to delete.");
}

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Problem

#2 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Linked List Problem

Posted 17 August 2010 - 11:55 PM

You've got all kinds of problems here.
First of all, when you're declaring a function, you must specify the type of every single argument. Unlike when declaring variables, you can't just string multiple arguments of the same type together with only one type declaration they way you're trying to.

Second, you're declaring the variables head and curr as struct listel pointers, but then when you call your functions, you use & to take pointers to the pointers. They're already pointers, so there's no need for the ampersand.

In your function add, you don't declare temp or q anywhere.

In your function delete, you never declare curr.

There are more problems as well, but fix those first. Then look at the errors you're getting from your compiler and work on fixing the rest.
Was This Post Helpful? 0
  • +
  • -

#3 SHENGTON  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Re: Linked List Problem

Posted 18 August 2010 - 05:41 AM

Hello,

Please check my code again Sir. Kindly correct my codes if you see something wrong. I changed and ran these codes as what you have said but I got a lot of errors. Honestly, I really don't know if I changed the right thing.

Here's my code again Sir:
#include<stdio.h>
#include<conio.h>

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


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

int main()
{
 int i,cho,x;
 struct listel *curr,*head,*temp,*q;
 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("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   clrscr();
   printf("\n Enter the element you want to add: ");
   scanf("%d",&x);
   add(curr,head);
   display(curr,head);
  }
  else if(cho==2)
  {
   clrscr();
   display(curr,head);
  }
  else if(cho==3)
  {
   clrscr();
   printf("\n Now demonstrating search: ");
   temp = search(curr,head);
   printf("\n Address of first occurence is  %u ",temp);
  }
  else if(cho==4)
  {
   clrscr();
   printf("\n Enter the element to delete: ");
   scanf("%d",&x);
   delete(head,curr,x);
  }
  else
  {
   clrscr();
   printf("  _____________________\n |  \t\t       |");
   printf("\n | The array 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 add(item *curr,item *head)
{
 int x,;
 temp = q;
 if(curr==NULL)
 {
  curr=(item*)malloc(sizeof(item));
	temp = *q;
 }
 else
 {
  while((temp->next)!=NULL)
  {
   temp=temp->next;
  }
  temp->next=(item*)malloc(sizeof(item));
  temp=temp->next;
 }
 temp->val = x;
 temp->next = NULL;
}

void display(item *curr,item *head)
{
 curr=head;
 while(curr!=NULL)
 {
  printf(" Data : %d",curr->val);
  printf(" Link : %d",curr->next);
  curr=curr->next;
 }
}


struct listel *search(item *curr,item *head)
{
 struct listel *temp,*p;
 int x;
 temp = p;
 printf(" Enter the data that you want to search: ");
 scanf("%d",&x);
 printf("Link of temp %u", temp->link);
 while(temp->next!=NULL)
 {
  printf("In while ");
  if(temp->val==x)
  {
   return(temp);
   temp=temp->next;
  }
 }
 return(NULL);
}


void delete(item *head,item *curr,int x)
{
 struct listel *temp,*m;
 temp=curr;
 x= NULL;
 while(temp->next !=NULL)
 {
  if(temp->val == x)
  {
   if (temp==head)
   {
    head=temp->next;
    free(temp);
    return;
   }
   else
   {
    m->next = temp->next;
    free(temp);
    return;
   }
  }
  m=temp;
  temp=temp->next;
 }
 printf(" No such entry to delete.");
}

This post has been edited by SHENGTON: 18 August 2010 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Somyagupta  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 85
  • Joined: 05-August 10

Re: Linked List Problem

Posted 18 August 2010 - 06:35 AM

Can u Post the error and warning list
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Linked List Problem

Posted 18 August 2010 - 07:01 AM

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



to

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



You prototype delete as
void delete(item *head,int x);

then define it as
void delete(item *head,item *curr,int x)


Here are some more for you to fix:
llll.c: In function ‘main’:
llll.c:54: warning: format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘struct listel *’
llll.c:23: warning: unused variable ‘q’
llll.c: In function ‘add’:
llll.c:85: error: expected identifier or ‘(’ before ‘;’ token
llll.c:86: error: ‘temp’ undeclared (first use in this function)
llll.c:86: error: (Each undeclared identifier is reported only once
llll.c:86: error: for each function it appears in.)
llll.c:86: error: ‘q’ undeclared (first use in this function)
llll.c:89: warning: implicit declaration of function ‘malloc’
llll.c:89: warning: incompatible implicit declaration of built-in function ‘malloc’
llll.c:98: warning: incompatible implicit declaration of built-in function ‘malloc’
llll.c: In function ‘display’:
llll.c:111: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘struct listel *’
llll.c: In function ‘search’:
llll.c:124: error: ‘struct listel’ has no member named ‘link’
llll.c: In function ‘delete’:
llll.c:142: warning: assignment makes integer from pointer without a cast
llll.c:150: warning: implicit declaration of function ‘free’



Read the errors/warnings and fix them. Note that you need to include <stdlib.h> to use malloc/free.
Was This Post Helpful? 0
  • +
  • -

#6 SHENGTON  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Re: Linked List Problem

Posted 19 August 2010 - 05:43 AM

Hello

I found a tutorial about Linked List by bodom658. But I'm confused with this code below. What's the meaning of these lines below? I'm still a student and very newbie with this. Hope you can explain it to me line by line. Thanks! :)

int main()
{
	int x;
	
	struct list MyList;
	InitList(&MyList);
	
	for(x = 0; x < 100; x++) push(&MyList, x);
	print(&MyList);
	printf("\n");
	for(x = 0; x < 25; x++) pop(&MyList);
	print(&MyList);
	printf("\n");
	for(x = 0; x < 80; x++) pop(&MyList);
	print(&MyList);
	printf("\n");
	
	return 0;
}


and this code to:
void InitList(struct list *sList)
{
	sList->start = NULL;
}

Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Linked List Problem

Posted 19 August 2010 - 07:36 AM

First of all, in that tutorial, push function adds an element to the beginning of the list. Therefore:
for(x = 0; x < 100; x++) push(&MyList, x);


Adds 100 elements to the list, starting from 0 -> 99. When x is 0, then 0 is added to the front of the list, therefore, the list contains one element (0).
When x is 1, then 1 is added to front of the list, so the list size grows to two, and list contains following elements: (1, 0).
When x is 2, 2 is added, ..., and list contains three elements: (2, 1, 0).
And etc ..., until x becomes 99, and is added to the list.

print(&MyList); -> not hard to anticipate what this function does. It prints all the elements in the list.

for(x = 0; x < 25; x++) pop(&MyList);


pop function removes an element from the list. (from the beginning of the list). Here, pop is called 25 times, so, it removes the first 25 elements from the list.
Same story for:
for(x = 0; x < 80; x++) pop(&MyList);


but now, pop is called 80 times, therefore, 80 elements are removed. The list is empty now, remember that we've added 100 elements first.
Was This Post Helpful? 0
  • +
  • -

#8 SHENGTON  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Re: Linked List Problem

Posted 19 August 2010 - 10:10 AM

I just combined the example code of bodom658 and I added some codes so that there's a menu. But I got 3 errors and 3 warnings with my Linked List. I attached an image for you to see. Someone can help me how to solve this problem?

Here's the code:
#include<stdio.h>
#include<stdlib.h>

void push(struct list *sList, int data);
void pop(struct list *sList);
void print(struct list *sList);

struct node
{
 int data;
 struct node *next;
};

struct list
{
 struct node *start;	
};


int main()
{
 struct list *sList;
 int i,cho=0;
 do
 {
  printf("\n\n\n [1] Add Items\n");
  printf(" [2] Display Items\n");
  printf(" [3] Delete Items\n");
  printf(" [4] Exit\n\n");
  printf(" Enter your choice: ");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   push(sList,data);
  }
  else if(cho==2)
  {
   print(slist);
  }
  else if(cho==3)
  {
   pop(sList);
  }
 }
 while(cho != 4);
 {
  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 push(struct list *sList, int data);
{
 sList->start = NULL;
 struct node *p;
 int n;
 clrscr();
 printf("\n\n Enter integer for total numbers to be allocated: ");
 scanf("%d",&n);
 printf("\n\n");
 p = (struct node *)malloc(sizeof(struct node));
 p->data = n;
 p->next = sList->start;
 sList->start = p;
}

void print(struct list *sList)
{
 struct node *p = sList->start;
 while(p != NULL)
 {
  printf("%d",p->data);
  p=p->next;
 }
}

void pop(struct list *sList)
{
 if(sList->start != NULL)
 {
  struct node *p = sList->start;
  sList->start = sList->start->next;
  free(p);
 }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1