求助!!! 链表问题
自己写的一个程序 无法正确插入节点 帮忙看看什么问题呢#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 4
typedef struct student
{
char name[10];
int num;
struct student *prev;
struct student *next;
}NODE, *PNODE;
struct student *creatInode(struct student *pHead);
void removeInode(struct student *pHead);
void InsertInode(PNODE pHead, NODE *Inode);
void traverse_list(PNODE pHead);
struct student *pHead = NULL;
int main(void)
{
//PNODE insert = NULL;
int i = NUM-1;
NODE a[NUM] = {{"zhang", 1}, {"li", 3}, {"wu", 2}, {"xu", 4}};
PNODE studentInfo =(PNODE)malloc(sizeof(NODE));
pHead = creatInode(studentInfo);
for (; i>=0; i--)
{
InsertInode(pHead, &a[i]);
}
traverse_list(pHead);
removeInode(pHead);
traverse_list(pHead);
return 0;
}
PNODE creatInode(PNODE pHead)
{
PNODE pTail;
pHead = (PNODE)malloc(sizeof(NODE));
if (pHead == NULL)
{
printf("动态内存分配失败,程序中止!\n");
exit(-1);
}
pTail = pHead;
pTail->next = NULL;
return pHead;
}
void InsertInode(PNODE pHead, PNODE Inode)
{
if (!(Inode = (PNODE)malloc(sizeof(NODE))))
{
printf("malloc error");
exit -1;
}
if(pHead->next != NULL)
{
pHead->next->prev = Inode;
Inode->next = pHead->next;
pHead->next = Inode;
Inode->prev = pHead;
}
else
{
Inode->next = pHead->next;
pHead->next = Inode;
Inode->prev = pHead;
}
}
void removeInode(NODE *pHead)
{
int i = 0;
int j = i+1;
pHead = (PNODE)malloc(sizeof(NODE));
for(; i<NUM; i++)
{
for(; j<NUM; j++)
{
if (pHead[i].num > pHead[j].num)
{
pHead = &pHead[i];
pHead[i].next = pHead[j].next;
pHead[i].prev = &pHead[j];
pHead[j].prev = pHead->prev;
pHead[j].next = &pHead[i];
}
}
}
}
void traverse_list(PNODE pHead)
{
PNODE p;
p = pHead->next;
if (NULL == p)
{
printf("链表为空!\n");
}
while(NULL != p)
{
printf("name :%s num :%d \n",p->name,p->num);
p = p->next;
}
}