单链表创建和删除的C程序,大家帮忙改下
来源:百度知道 编辑:UC知道 时间:2024/06/01 08:25:24
#include <stdlib.h>
struct Node
{
int nID;
Node* pNext;
};
Node* g_pHead = 0;
void NewNode(int nID)
{
Node* pNode = (Node*)malloc(sizeof(Node));
pNode->nID = nID;
pNode->pNext = g_pHead;
g_pHead = 0;
}
bool DelNode(int nID)
{
Node* pLastNode = g_pHead;
if (g_pHead->nID == nID)
{
g_pHead = g_pHead->pNext;
free(pLastNode);
return true;
}
Node* pNode = pLastNode->pNext;
while (pNode != 0)
{
if (pNode->nID == nID)
{
pLastNode->pNext = pNode->pNext;
free(pNode);
return true;
}
pLastNode = pNode;
pNode = pNode->pNext;
}
return false;
}
int main(int argc, char* argv[])
{
return 0;
}
struct Node
{
int nID;
Node* pNext;
};
Node* g_pHead = 0;
void NewNode(int nID)
{
Node* pNode = (Node*)malloc(sizeof(Node));
pNode->nID = nID;
pNode->pNext = g_pHead;
g_pHead = 0;
}
bool DelNode(int nID)
{
Node* pLastNode = g_pHead;
if (g_pHead->nID == nID)
{
g_pHead = g_pHead->pNext;
free(pLastNode);
return true;
}
Node* pNode = pLastNode->pNext;
while (pNode != 0)
{
if (pNode->nID == nID)
{
pLastNode->pNext = pNode->pNext;
free(pNode);
return true;
}
pLastNode = pNode;
pNode = pNode->pNext;
}
return false;
}
int main(int argc, char* argv[])
{
return 0;
}
编译运行即可
#include<malloc.h>
#include<stdio.h>
//#include<string.h>
#define null 0
struct student
{
long Number;
char Name[20];
long Score;
struct student *Next;
};
int n=0;/*n为全局变量,用来计算链表的结点个数*/
/*-----------------------------------------*/
/*--------------创建结点函数Creat()--------*/
/*-----------------------------------------*/
struct student *Creat()
{
struct student *p1;
struct student *p2;
struct student *head=null;
p1=p2=(struct student *)malloc(sizeof(struct student));/*开辟一段可用内存单元*/
printf("please input the student's Number Name and the Score:\n");
scanf("%ld %s %ld",&(p2->Number),p2->Name,&p2->Score);
while(p2->Number!=0)
{
n++;
if(n==1) /*是否开辟的是第一个结点*/
head=p2;
else
p1->Next=p2;
p1=p2;
p2=(st