leetcode707 设计链表
几乎都是这个报错,看不懂什么意思
指针这样赋值有什么问题,求解答
typedef struct {
int val;
struct MyLinkedList* next;
}MyLinkedList;
MyLinkedList* myLinkedListCreate() {
MyLinkedList* head = (MyLinkedList*)malloc(sizeof(MyLinkedList));
if (head == NULL)
exit(-1);
head->next = NULL;
return head;
}
int myLinkedListGet(MyLinkedList* obj, int index) {
MyLinkedList* cur = obj->next;
for (int i = 0; cur != NULL; i++)
{
if (i == index)
return cur->val;
else
cur = cur->next;
}
return -1;
}
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyLinkedList* shead = (MyLinkedList*)malloc(sizeof(MyLinkedList));
if (!shead)
exit(-1);
shead->val = val;
shead->next = obj->next;
obj->next = shead;
}
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList* cur = obj;
MyLinkedList* NewTail = (MyLinkedList*)malloc(sizeof(MyLinkedList));
if (!NewTail || !cur)
exit(-1);
NewTail->val = val;
NewTail->next = NULL;
while (cur && cur->next) //
cur = cur->next;
cur->next = NewTail;
}
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if (index == 0)
{
myLinkedListAddAtHead(obj, val);
return;
}
MyLinkedList* cur = obj->next;
for (int i = 1; cur != NULL; i++)
{
if (index == i)
{
MyLinkedList* nNode = (MyLinkedList*)malloc(sizeof(MyLinkedList));
if (nNode == NULL)
eixt(-1);
nNode->val = val;
nNode->next = cur->next;
cur->next = nNode;
return;
}
else
cur = cur->next;
}
}
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
MyLinkedList* cur = obj;
for (int i = 0; cur->next != NULL; i++)
{
if (index == i)
{
MyLinkedList* tmp = cur->next;
if (!tmp)
{
tmp = NULL;
free(tmp);
}
else
{
cur->next = tmp->next;
free(tmp);
}
return;
}
else
cur = cur->next;
}
} //
void myLinkedListFree(MyLinkedList* obj) {
MyLinkedList* shead = (MyLinkedList*)malloc(sizeof(MyLinkedList));
if (!shead)
exit(-1);
shead->next = obj;
MyLinkedList* cur = shead;
MyLinkedList* tmp;
while ((tmp = cur->next))
{
cur->next = tmp->next;
free(tmp);
}
free(shead);
return;
}