繁体   English   中英

程序收到信号 SIGSEGV,请求用户输入双向链表时出现分段错误

[英]Program received signal SIGSEGV, Segmentation fault when asking user input for doubly linked list

我是 C 编程的新手,当我出于某种原因要求用户输入数字时,我的整个程序都会出现此错误

Program received signal SIGSEGV, Segmentation fault.
0x0000555555555753 in addData (word=0x7fffffffeb70 "", mode=2) at main.c:202
202                 if (temp->next == NULL) {
(gdb) 

这是我的代码


#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MEMORY 6
#define MAX_word_LENGTH 11 
int numberOfwords;
char words[MEMORY][MAX_word_LENGTH];
struct Node{
    int data;

    char *word;

    struct Node *next, *prev;
};

struct Node *head, *tail;
struct Node* DlListDeleteLastNode(struct Node *head);
void addData(char *word, int mode);
void DisplayForward();
struct Node* deletePos(struct Node *head, int pos);
int getPostion(char *targetWord);
bool search(char *targetWord);
//struct Node* deletePos(struct Node *head, int pos);
int countWord(char *wording);
void insert_specified(char *wording, int pos);
int countNodes();

//void init(){
//   head = NULL;
//   tail = NULL;
//}
int main()
{
    
    
   char name[100];
   //char searchName[] = "barr";
   printf("Enter number of words to be stored in the word stream: ");
   
    int input;
    scanf("%d", &input);
    
   
   int i;
   int count = 0; 
   for (i=0; i<input;i++){
      count++;
      printf("\nEnter name %d : ",count);

      scanf ("%[^\n]%*c", name);
      //scanf("%s[^\n]",words[i]);
      addData(name, 2);
   }
   
   
   //for (int i = 0; i <4; i++) {
   //    strcpy(words[i], 
   //}
  //insert_specified(searchName, 0);
  //printf("%d\n", numberOfwords);
  //DlListDeleteLastNode();
  DisplayForward();
  //int num = countNodes();
  //printf("count %d", num);
  //deletePos(head, 3);
  //bool x = search(searchName);
  //printf("%d\n", x);
  //printf("%d\n", numberOfwords);
  //DisplayForward();
  //fflush(stdin);
  printf("\nEntered names are:\n");
     for(i=0;i<6;i++)
     puts(words[i]);
}
int isEmpty(struct Node *h){
    if(h==NULL)
        return 1;
    else
        return 0;
}

void addData(char *word, int mode){

if (mode == 1) {
    for (int i=0; i<5; i++) {
        if (strcmp(words[i],word) == 0) {
            //printf("found\n");
            //printf("FOUND%s\n", word);
            int x = getPostion(word);
            //printf("index found %d/n", x);
            //deletePos(head, x);
            struct Node* temp = head;
            struct Node* temp2 = NULL;
            while (x > 1) {
                temp = temp->next;
                x--;
            }
            if (temp->next == NULL) {
                head = DlListDeleteLastNode(head);
            }
            else {
                temp2 = temp->prev;
                temp2->next = temp->next;
                temp->next->prev = temp2;
                free(temp);
                temp = NULL;
                
            }
            break;
        }
        
        //x index = getPostion()
        
    }
    if (numberOfwords >= 5) {
        DlListDeleteLastNode(head);
        struct Node *ptr;   
       ptr= malloc(sizeof *ptr);
       ptr->word = malloc(sizeof(char) * 12);
        //char nimI[20];
        
        //baru->nim = malloc(12 * sizeof(char));
       //strcpy(ptr->word, word);
     
       if(head==NULL)  
       {  
           ptr->next = NULL;  
           ptr->prev=NULL; 
           strcpy(ptr->word, word);
           //ptr->word= word;  
           head=ptr;  
           //head = ptr;
           //tail = ptr;
       }  
       else   
       {  
           //ptr->word=word;  
           strcpy(ptr->word, word);
           ptr->prev=NULL;  
           ptr->next = head;  
           head->prev=ptr;  
           head=ptr;  
       }
       numberOfwords++;
    }
    else {
        struct Node *ptr;   
       ptr= malloc(sizeof *ptr);
       ptr->word = malloc(sizeof(char) * 12);
        //char nimI[20];
        
        //baru->nim = malloc(12 * sizeof(char));
       //strcpy(ptr->word, word);
     
       if(head==NULL)  
       {  
           ptr->next = NULL;  
           ptr->prev=NULL; 
           strcpy(ptr->word, word);
           //ptr->word= word;  
           head=ptr;  
           //head = ptr;
           //tail = ptr;
       }  
       else   
       {  
           //ptr->word=word;  
           strcpy(ptr->word, word);
           ptr->prev=NULL;  
           ptr->next = head;  
           head->prev=ptr;  
           head=ptr;  
       }  
       numberOfwords++;
       strcpy(words[numberOfwords], word);
        
    }
        
    
       
        
}
else {
    int flag = 0;
    //int numWords = countNodes();
    for (int i=0; i<8; i++) {
        if (strcmp(words[i],word) == 0) {
            //printf("found\n");
            //printf("FOUND%s\n", word);
            int x = getPostion(word);
            //printf("index found %d/n", x);
            //deletePos(head, x); //
            struct Node* temp = head;
            struct Node* temp2 = NULL;
            while (x > 1) {
                temp = temp->next;
                x--;
            }
            if (temp->next == NULL) {
                head = DlListDeleteLastNode(head);
            }
            else {
                temp2 = temp->prev;
                temp2->next = temp->next;
                temp->next->prev = temp2;
                free(temp);
                temp = NULL;
                
            }
            
            flag = 1;
            
            //DisplayForward();
            //numberOfwords--;
            break;
        }
        
        //x index = getPostion()
        
    }
    printf("\nflag num-> %d\n", flag);
    //numberOfwords++;
    int numWords = countNodes();
    if (numWords >= 5 && flag==0) {
       //printf("activated");
       int max = 0;
       char maxWord[50];
       for (int i = 0; i < 8; i++) {
           int num = countWord(words[i]);
           if (num > max) {
               max = num;
               //printf("%d", max);
               strcpy(maxWord, words[i]);
               //printf("word %s\n", maxWord);
           }
           
           
       }
       printf("most occured word: %s\n", maxWord);
       int mostOccuredWordIndex = getPostion(maxWord);
       printf("index: %d\n", mostOccuredWordIndex);
       mostOccuredWordIndex--;
       insert_specified(word, mostOccuredWordIndex);
       DlListDeleteLastNode(head);
       
        
        
    }
    
    else if (numWords >= 5) {
        //DlListDeleteLastNode(head);
        //struct Node *ptr;   
       //ptr= malloc(sizeof *ptr);
       //ptr->word = malloc(sizeof(char) * 12);
        //char nimI[20];
        
        //baru->nim = malloc(12 * sizeof(char));
       //strcpy(ptr->word, word);
       
       //Get most occured word in words array
       
       //find the position of the most occured word
       //int mostOccuredWordIndex = getPostion(maxWord);
       //remove last element and insert new word into that position
       
       //if position is in the last index then remove word and replace with the word 
       //if (mostOccuredWordIndex == 5) {
        //   printf("occurred");
           
       //}
       // 
       DlListDeleteLastNode(head);
       numberOfwords = numberOfwords + 1;
       struct Node *ptr;   
       ptr= malloc(sizeof *ptr);
       ptr->word = malloc(sizeof(char) * 12);
       if(head==NULL)  
       {  
           ptr->next = NULL;  
           ptr->prev=NULL; 
           strcpy(ptr->word, word);
           //ptr->word= word;  
           head=ptr;  
           //head = ptr;
           //tail = ptr;
       }  
       else   
       {  
           //ptr->word=word;  
           strcpy(ptr->word, word);
           ptr->prev=NULL;  
           ptr->next = head;  
           head->prev=ptr;  
           head=ptr;  
       }
       
    }
    
    
    
    else {
        struct Node *ptr;   
       ptr= malloc(sizeof *ptr);
       ptr->word = malloc(sizeof(char) * 12);
        //char nimI[20];
        
        //baru->nim = malloc(12 * sizeof(char));
       //strcpy(ptr->word, word);
     
       if(head==NULL)  
       {  
           ptr->next = NULL;  
           ptr->prev=NULL; 
           strcpy(ptr->word, word);
           //ptr->word= word;  
           head=ptr;  
           //head = ptr;
           //tail = ptr;
       }  
       else   
       {  
           //ptr->word=word;  
           strcpy(ptr->word, word);
           ptr->prev=NULL;  
           ptr->next = head;  
           head->prev=ptr;  
           head=ptr;  
       }  
       numberOfwords = numberOfwords + 1;
       strcpy(words[numberOfwords], word);
        
    }
    DisplayForward();
    numWords = countNodes();
    printf("\nnumber of words%d \n", numWords);
}
}
    




struct Node* DlListDeleteLastNode(struct Node *head) {
    struct Node *temp = head;
    struct Node* temp2; 
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp2 = temp->prev;
    temp2->next = NULL;
    free(temp);
    numberOfwords--;
    return head;
}
void DisplayForward() {  
    //Node current will point to head  
    struct Node *current = head;  
    if(head == NULL) {  
        printf("List is empty\n");  
        return;  
    }  
    printf("["); 
    int count = numberOfwords; 
    // while current->next != Null
    while(current!=NULL) {  
        //Prints each node by incrementing pointer.  
        printf("%s, ", current->word);  
        current = current->next;  
        count--;
    }  
    //current = current->next;  
    //printf("%s]\n", current->word);  
    //printf("%d", numberOfwords);
}  

bool search(char *targetWord) {
   int pos = 0;
   
   if(head==NULL) {
      printf("Linked List not initialized");
      return;
   } 

   struct Node *current = head;
   //current = head;
   
   while(current!=NULL) {
      pos++;
      if(strcmp(current->word, targetWord) == 0) {
         printf("%s found at position %d\n", targetWord, pos);
         return true;
      }

      if(current->next != NULL)
         current = current->next;
      else
         break;
      
   }
   return false;
   //printf("%s does not exist in the list\n", targetWord);
}
int getPostion(char *targetWord) {
    int pos = 0;
   
   if(head==NULL) {
      printf("Linked List not initialized");
      return -1;
   } 

   struct Node *current = head;
   //current = head;
   while(current!=NULL) {
      pos++;
      if(strcmp(current->word, targetWord) == 0) {
         //printf("%s found at position %d\n", targetWord, pos);
         return pos;
      }

      if(current->next != NULL)
         current = current->next;
      else
         break;
      
   }
   return -1;
   //printf("%s does not exist in the list\n", targetWord);
}


int countWord(char *wording) {
    int count = 0;
    for(int i=0;i<6;i++) {
        if (strcmp(words[i], wording)==0) {
            //printf("found word");
            count++;
        }
    }
    return count;
}

void insert_specified(char * wording, int pos)  
{  
   struct Node *ptr;
   ptr= malloc(sizeof *ptr);
   ptr->word = malloc(sizeof(char) * 12);
   struct Node *temp;   
   int i;   
   if(ptr == NULL)  
   {  
       printf("\n OVERFLOW");  
   }  
   else  
   {  
       //printf("\nEnter the location\n");  
       //scanf("%d",&loc);  
       temp=head;  
       for(i=0;i<pos;i++)  
       {  
           temp = temp->next;  
           if(temp == NULL)  
           {  
               printf("\ncan't insert\n");  
               return;  
           }  
       }  
       strcpy(ptr->word, wording); 
       ptr->next = temp->next;  
       ptr -> prev = temp;  
       temp->next = ptr;  
       temp->next->prev=ptr;  
       printf("Node Inserted\n");  
   }  
}
int countNodes() {  
    int counter = 0;  
    //Node current will point to head  
    struct Node *current = head;  
      
    while(current != NULL) {  
        //Increment the counter by 1 for each node  
        counter++;  
        current = current->next;  
    }  
    return counter;  
} 

我试图向用户询问它想要为我的程序输入的一些单词,以将项目存储在双向链表中,但却给出了分段错误

struct Node *head是一个零初始化的全局变量。 addData()中设置struct Node* temp = head; 然后你尊重temp就像你的堆栈跟踪显示的那样会出现段错误。

无论如何,在你到达那里之前,第二个scanf()失败了,因为它的输入缓冲区包含读取前一个数字的\n 格式字符串的语法在我看来也是错误的。 在任何情况下,因为您不检查您正在对未初始化数据进行操作的返回值。 这是最小的修复:

        if(scanf(" %99[^\n]*", name) != 1) {
            printf("scanf failed\n");
            return 1;
        }

我还将硬编码的< 8替换为< MEMORY以避免缓冲区溢出。 这是一个示例运行:

Enter number of words to be stored in the word stream: 2

Enter name 1 : test

flag num-> 0
[test, 
number of words1 

Enter name 2 : test2

flag num-> 0
[test2, test, 
number of words2 
[test2, test, 
Entered names are:

test
test2



暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM