簡體   English   中英

C中鏈表中的錯誤

[英]error in linked list in C

程序在請求之前接受輸入。 在輸入第一個節點的值后問題開始。

這是一個簡單的程序,它從用戶處獲取輸入並將其存儲在鏈接列表中,然后顯示存儲的數據。

在此處輸入圖片說明

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

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

void main()
{      
    struct NODE  *first,*old,*new_node;
    int n,i;
    printf ("Enter number of elements: \n");
    scanf ("%d",&n);
    first=(struct NODE*)malloc(sizeof(struct NODE));
    first->next= NULL;

    printf ("Enter value of node 1: \n");
    scanf ("%d\n",&first->data);
    old = first;
    for(i=2;i<=n;i++)
    {
        new_node=(struct NODE*)malloc(sizeof(struct NODE));
        new_node->next= NULL;

        printf("Enter value of node %d: \n",i);
        scanf("%d\n",&new_node->data);
        old->next=new_node;
        old = new_node;
    }

    old=first;
    while(old!= NULL)
    {
        printf("%d \t",old->data);
        old=old->next;
    }
}

問題是您的scanf格式規范中\\n包含(我強調):

格式字符串中很少有常量(即,未格式化占位符的字符),主要是因為程序通常不旨在讀取已知數據。 一個或多個空格字符是一個例外,它會丟棄輸入中的所有空格字符。

因此, \\n表示將忽略在每個數字之后輸入的換行符,並且直到鍵入另一個換行符為止(在第一個節點計數輸入中正確省略了它scanf() ,下一個scanf()才會完成。

您所需要做的就是從格式字符串中刪除\\n ,您的代碼將按預期工作:

    ...
    scanf ("%d\n",&first->data);
    ...
    scanf("%d\n",&new_node->data);
    ...

請注意,可以通過在printf()格式字符串中省略\\n並在調用scanf()之前調用fflush(stdout)在提示符下輸入相同的行,例如:

    printf ("Enter number of elements: "); 
    fflush(stdout);
    scanf ("%d",&n);

這將使您進行更自然的對話。

您可以像這樣執行所有鏈表操作

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

struct Node{
    struct Node *next;
    int data;

};

void printList(struct Node *n)
{
  while (n != NULL)
  {
     printf(" %d ", n->data);
     n = n->next;
  }
}

void insertFirst( struct Node *start,int data1)
 {
        struct Node *n=(struct Node*)malloc(sizeof(struct Node));
        n->data=data1;
        n->next=start->next;
        start->next=n;
 }

void insertLast( struct Node *start,int data1)
 {
    struct Node *last,*previous;
    last=start->next;
    while(last!=NULL)
    {
        previous=last;
        last=last->next;    
     }
        struct Node *n=(struct Node*)malloc(sizeof(struct Node));
        n->data=data1;
        n->next=NULL;
        previous->next=n;
 }

void insertPosition(struct Node *start, int pos, int data1)
{
    int count=0;
    struct Node *node;
    node=start->next;
    while(node!=NULL)
    {
        count++;
        node=node->next;
    }
    printf("total elements before insertion is %d\n ",count);

    if(count+1<pos)
    printf("cannot insert at desired position ");
    else
    {
     int i=1;
     node=start;
        while(i<pos)
        {
            node=node->next;
            i++;
        }

      struct Node *n=(struct Node*)malloc(sizeof(struct Node));
      n->data=data1;
      n->next=node->next;
      node->next=n;
}
}

void deleteFirst(struct Node *start)
{
    struct Node *firstNode;
    firstNode= start->next;
    start->next=firstNode->next;
    free(firstNode);
    printf("first node is removed\n");
}

void deleteLast(struct Node *start)
{
    struct Node *last,*previous;
    last=start;
    while(last->next!=NULL)
    {
        previous=last;
        last=last->next;    

     }
        previous->next=NULL;

    free(last);
    printf("last node is removed\n");
}     

void deletePosition(struct Node *start, int pos)
{
    int count=0;
    struct Node *node;
    struct Node *previous;
    node=start->next;
    while(node!=NULL)
    {
        count++;
        node=node->next;
    }
    printf("total elements before deletion is %d\n ",count);

    if(count<pos)
    printf("cannot delete the desired position ");
    else
    {
     int i=1;
     node=start->next;
        while(i<pos)
        {
            previous=node;
            node=node->next;
            i++;
        }

      previous->next=node->next;
      free(node);
        printf("node is removed\n");

}
}

int main()
{
    struct Node *start=NULL;
    struct Node *node1=NULL;
    struct Node *node2=NULL;
    struct Node *node3=NULL;
    struct Node *node=NULL;

    start=(struct Node*)malloc(sizeof(struct Node));    
    node1=(struct Node*)malloc(sizeof(struct Node));
    node2=(struct Node*)malloc(sizeof(struct Node));
    node3=(struct Node*)malloc(sizeof(struct Node));


    start->next=node1;
    node1->data=1;
    node1->next=node2;

     node2->data=2;
    node2->next=node3;

     node3->data=3;
    node3->next=NULL;
    printf("\nsize %d\n",sizeof(struct Node));


    //insertFirst(start,10);
    //insertLast(start,200);
   // insertPosition(start,2,300);

    // deleteFirst(start);
  //  deleteLast(start);
  //deletePosition(start,2);
     printList(start->next);
}

可以刪除注釋以執行該操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM