程序的要求是线性表的插入和删除
程序如下:
#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 时出错.