https://blog.csdn.net/cai18381306175/article/details/78853562
利用链表实现对字母的插入、删除,错误是读取访问权限冲突,求问大佬这是指针的问题吗,请各位大佬帮忙修改(问题在delete函数星号内部)
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef struct node
{
char data;
struct node* next;
}node, * pnode;
pnode create_list()
{
int i;
pnode head = (pnode)malloc(sizeof(node));
if (!head)
{
cout << "未成功分配内存" << endl;
exit(-1);
}
pnode p = head;
for (i = 0; i < 25; i++)
{
p->data = 'A' + i;
p->next = (pnode)malloc(sizeof(node));
if (!p->next)
{
cout << "未成功分配内存" << endl;
exit(-1);
}
p = p->next;
}
p->data = 'A' + 25;
p->next = NULL;
return head;
}
int show_list(pnode p)
{
while (p!= NULL)
{
cout << p->data << '-' << '>';
p = p->next;
}
if (p== NULL)
{
cout << "NULL" << endl;
}
return 0;
}
int insert_list(pnode p)
{
char a;
cin >> a;
pnode pnew = (pnode)malloc(sizeof(node));
if (!pnew)
{
cout << "内存未分配成功" << endl;
exit(-1);
}
pnew->data = a;
pnew->next = NULL;
while (p != NULL)
{
if (p->data != a)
{
cout << "wrong" <<endl;
p = p->next;
}
else
{
pnew->next = p->next;
p->next = pnew;
break;
}
}
return 0;
}
int delete_list(pnode p)
{
char a;
cin >> a;
pnode q;
q = NULL;
while (p != NULL)
{
if (p->data != a)
{
cout << "wrong" << endl;
q = p;
p = p->next;
}
****if (p->data == a)****
{
q->next= p->next;
free(p);
}
}
return 0;
}
int main()
{
pnode phead;
phead=create_list();
show_list(phead);
insert_list(phead);
show_list(phead);
delete_list(phead);
show_list(phead);
return 0;
}
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答