开发者

Printing value of a link in linked list

I have written a Linked list program in C. But my code to print the linked list is showing an error. I can't understand what is going wrong. Can anyone help me to find out what has gone wrong in my code

#include<stdio.h>
#include<stdlib.h>
struct node{
  int link_data;
  struct node *next;
};
void add_node(struct node *,int);
int delete_node(struct node *);
struct node *front = NULL,*rear = NULL;
int main(){
  开发者_StackOverflow社区struct node *l = (struct node *)malloc(sizeof(struct node *));
  struct node *inc = (struct node *)malloc(sizeof(struct node *));
  int number,i,option,del_node;
  while(1){
    printf("Enter the option:\n1. Add node\n2. Delete node\n");
    scanf("%d",&option);
    switch(option){
      case 1:
        printf("Enter the number to be add\n");
        scanf("%d",&number);
        add_node(l,number);
        break;
      case 2:
        del_node = delete_node(l);
        printf("%d has been removed\n",del_node);
        break;
      case 3:
        for(inc = front ; inc < rear ; inc = inc->next){
          print("%d",inc->link_data);   <<<<<<<<<<<<<<<<< Error part of the program
        }
        break;
      case 4: 
        exit(0);
        break;
    }
  }
}
void add_node(struct node *l,int number){
  struct node *newnode = (struct node *)malloc(sizeof(struct node *));
  if(front == NULL){
    newnode->link_data = number;
    newnode->next = NULL;
    front = newnode;
    l = newnode;    
  }
  else{
    newnode->link_data = number;
    l -> next = newnode;
    l = newnode;
  }
}
int delete_node(struct node *l){
  int node_del;
  struct node *inc;
  for(inc = front ; inc < l ;inc = inc -> next );
  node_del = inc->link_data;
  l = inc;
  l -> next = NULL;
  return node_del; 
}


$ gcc -o ll -Wall -g -O0 ll.c
ll.c: In function ‘main’:
ll.c:29:11: warning: implicit declaration of function ‘print’ [-Wimplicit-function-declaration]
ll.c:13:14: warning: unused variable ‘i’ [-Wunused-variable]

You want to use printf, not print. There's also a variable named i that you don't use.

On the actual printing bit, you never assign rear. You probably also don't want to use the test inc < rear; usually linked lists end when their next pointer is NULL.

In add_node, you point front to the new node. You probably didn't mean to do this.


Well, inc and read are pointers. So inc < read doesn't make much sense (it's not useful to compare addresses like that). You could try:

for (inc = front; inc != rear; inc = inc->next)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜