程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 調試-求大神指點這段程序為什麼一直有錯誤,真的不知道怎麼處理了。。。

調試-求大神指點這段程序為什麼一直有錯誤,真的不知道怎麼處理了。。。

編輯:編程解疑
求大神指點這段程序為什麼一直有錯誤,真的不知道怎麼處理了。。。

程序的要求是線性表的插入和刪除
程序如下:
#include
#include
struct node{
int data;
node *next;
};
node *create_sort(void)
{
node *p1,*head=0;
int a;
printf("建立一條有序鏈表,請輸入數據,以-1結束:");
scanf("%d,&a);
while (a!=-1){
p1=new node;
p1->data=a;
head=insert(head,p1);
scanf("%d,&a);
}return(head);
}
void print(const node *head)
{
const node *p;
p=head;
printf("鏈表上各個節點的數據為:\n");
while(p!=NULL){
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
}
node *delete_one_node(node *head,int num)
{
node *p1,*p2;
if(head==NULL){
printf("鏈表為空,無節點可刪!\n");
return(NULL);
}
if (head->data==num){
p1=head;
head=head->next;
delete p1;
printf("刪除了一個節點!\n");
}
else{
p2=p1=head;
while(p2->data!=num&&p2->next!=NULL){
p1=p2;
p2=p2->next;
}
if(p2->data==num){
p1->next=p2->next;
delete p2;
printf("刪除了一個節點!\n");
}
else printf("%d鏈表上沒找到要刪除的節點!\n",num);
}
return(head);
}
void deletechain(node *h)
{
node *p1;
while(h){
p1=h;
h=h->next;
delete p1;
}
printf("已釋放鏈表的節點空間!\n");
}

int count(node*head)
{
int n;
node *p;
p=head;
n=0;
while(p!=NULL){
n=n+1;
p=p->next;
}return(n);
}

node *delete_k_node(node *head,int k)
{
int j=1;
node *p,*p1;
if(head==NULL){
printf("鏈表為空,無節點可刪!\n");
return(NULL);
}
p=head;
if(k==1){
p=head;
head=head->next;
delete p;
printf("刪除了第一個節點!\n");
}
else{
p=find(head,k-1);
if(p->next!=NULL)
{
p1=p->next;
p->next=p1->next;
delete p1;
printf("刪除了第%d個節點!\n",k);
}
return(head);
}
}
void main(void)
{
node *head;
int num;
int k;
head=create_sort();
print(head);
printf("節點數:%d\n,count(head));
printf("輸入要刪除節點上的序號!\n");
scanf("%d,&num);
head=delete_k_node(head,k);
print(head);
printf("輸入要刪除節點上的整數!\n");
scanf("%d,&num);
head=delete_one_node(head,num);
print(head);
printf("輸入要插入的整數!\n");
scanf("%d",&num);
head=insert(head,num);
print(head);
}

調試錯誤顯示如下:
h:\mydocuments\csq\csq.cpp(12) : error C2001: newline in constant

h:\mydocuments\csq\csq.cpp(13) : error C2143: syntax error : missing ')' before 'while'
h:\mydocuments\csq\csq.cpp(13) : error C2143: syntax error : missing ';' before '{'
h:\mydocuments\csq\csq.cpp(16) : error C2065: 'insert' : undeclared identifier
h:\mydocuments\csq\csq.cpp(16) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\mydocuments\csq\csq.cpp(17) : error C2001: newline in constant
h:\mydocuments\csq\csq.cpp(18) : error C2143: syntax error : missing ')' before '}'
h:\mydocuments\csq\csq.cpp(18) : error C2143: syntax error : missing ';' before '}'
h:\mydocuments\csq\csq.cpp(98) : error C2065: 'find' : undeclared identifier
h:\mydocuments\csq\csq.cpp(98) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\mydocuments\csq\csq.cpp(116) : error C2001: newline in constant
h:\mydocuments\csq\csq.cpp(117) : error C2146: syntax error : missing ')' before identifier 'printf'
h:\mydocuments\csq\csq.cpp(118) : error C2001: newline in constant
h:\mydocuments\csq\csq.cpp(119) : error C2146: syntax error : missing ')' before identifier 'head'
h:\mydocuments\csq\csq.cpp(122) : error C2001: newline in constant
h:\mydocuments\csq\csq.cpp(123) : error C2146: syntax error : missing ')' before identifier 'head'
h:\mydocuments\csq\csq.cpp(127) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
執行 cl.exe 時出錯.

最佳回答:


 #include<iostream>
using namespace std;

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

node* find(node *head, int k)
{
    node *p = head;
    node *pre = head;
    while(p && k-- > 0)
    {
        pre = p;
        p = p->next;
    }
    if(p == NULL)
        printf("未找到該元素位置\n");
    return pre;
}

node *insert(node *head, int elem)
{
    node *p = head;
    node *pre = head;
    if(p == NULL)//鏈表為空,插入第一個元素
    {
        node *p1 = new node;
        p1->data = elem;
        p1->next = NULL;
        head = p1;
    }
    else
    {
        while(p)
        {
            if(p->data > elem || p->next == NULL)//如果找到了要找的位置或者已經到最後的節點了
            {
                node *p1 = new node;
                p1->data = elem;
                if(p == head)
                {
                    if( p->data > elem)
                    {
                        head = p1;
                        p1->next = p;
                    }
                    else
                    {
                        p1->next = p->next;
                        p->next = p1;
                    }
                }
                else
                {
                    if(p->data < elem)
                    {
                        p->next = p1;
                        p1->next = NULL;
                    }
                    else
                    {
                        p1->next = p;
                        pre->next =p1;
                    }
                }
                break;
            }
            pre = p;
            p = p->next;
        }
    }
    return head;
}

node *create_sort(void)
{
    node *p1,*head=0;
    int a;
    printf("建立一條有序鏈表,請輸入數據,以-1結束:");
    scanf("%d",&a);
    while (a!=-1){
        head=insert(head,a);
        scanf("%d",&a);
    }
    return(head);
}
void print(const node *head)
{
    const node *p;
    p=head;
    printf("鏈表上各個節點的數據為:\n");
    while(p!=NULL){
        printf("%d\t",p->data);
        p=p->next;
    }
    printf("\n");
}
node *delete_one_node(node *head,int num)
{
    node *p1,*p2;
    if(head==NULL){
        printf("鏈表為空,無節點可刪!\n");
        return(NULL);
    }
    if (head->data==num){
        p1=head;
        head=head->next;
        delete p1;
        printf("刪除了一個節點!\n");
    }
    else{
        p2=p1=head;
        while(p2->data!=num&&p2->next!=NULL){
            p1=p2;
            p2=p2->next;
        }
        if(p2->data==num){
            p1->next=p2->next;
            delete p2;
            printf("刪除了一個節點!\n");
        }
        else printf("%d鏈表上沒找到要刪除的節點!\n",num);
    }
    return(head);
}
void deletechain(node *h)
{
    node *p1;
    while(h){
        p1=h;
        h=h->next;
        delete p1;
    }
    printf("已釋放鏈表的節點空間!\n");
}
int count(node*head)
{
    int n;
    node *p;
    p=head;
    n=0;
    while(p!=NULL){
        n=n+1;
        p=p->next;
    }return(n);
}
node *delete_k_node(node *head,int k)
{
    int j=1;
    node *p,*p1;
    if(head==NULL){
        printf("鏈表為空,無節點可刪!\n");
        return(NULL);
    }
    p=head;
    if(k==1){
        p=head;
        head=head->next;
        delete p;
        printf("刪除了第一個節點!\n");
    }
    else{
        p=find(head,k-1);
        if(p->next!=NULL)
        {
            p1=p->next;
            p->next=p1->next;
            delete p1;
            printf("刪除了第%d個節點!\n",k);
        }

    }
    return(head);
}
void main(void)
{
    node *head;
    int num;
    int k;
    head=create_sort();
    print(head);
    printf("節點數:%d\n",count(head));
    printf("輸入要刪除節點上的序號!\n");
    scanf("%d",&k);
    head=delete_k_node(head,k);
    print(head);
    printf("輸入要刪除節點上的整數!\n");
    scanf("%d",&num);
    head=delete_one_node(head,num);
    print(head);
    printf("輸入要插入的整數!\n");
    scanf("%d",&num);
    head=insert(head,num);
    print(head);
}

圖片說明

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved