#ifndef _DYNAMICLIST_H_
#define _DYNAMICLIST_H_
#include<stdio.h>
#include<stdlib.h>
//鏈表結點
typedef struct LINKNODE{
void* data;
struct LINKNODE* next;
}LinkNode;
//鏈表結構體
typedef struct LINKLIST{
LinkNode* head;
int size;
}LinkList;
//列印函式指標
typedef void(*PRINTLINKNODE)(void*);
//初始化鏈表
LinkList* Init_LinkList();
//指定位置插入
void Insert_LinkList(LinkList* List,int Pos,void* data);
//洗掉
void Remove_LinkList(LinkList* List,int Pos);
//獲得鏈表的長度
int Size_LinkList(LinkList* List);
//查找
int Find_LinkList(LinkList* List,void* data);
//回傳第一個結點
void* Front_LinkList(LinkList* List);;
//列印鏈表結點
void Print_LinkList(LinkList* List,PRINTLINKNODE print);
//釋放
void Free_LinkList(LinkList* List);
#endif
#include"DynamicList.h"
//初始化鏈表
LinkList* Init_LinkList()
{
LinkList* List=(LinkList*)malloc(sizeof(LinkList));
List->size=0;
List->head=(LinkNode*)malloc(sizeof(LinkNode));
List->head->data=https://bbs.csdn.net/topics/NULL;
List->head->next=NULL;
return List;
}
//指定位置插入
void Insert_LinkList(LinkList* List,int Pos,void* data)
{
if(List==NULL||data=https://bbs.csdn.net/topics/=NULL)
{
return;
}
if(Pos<0||Pos>List->size)
{
Pos=List->size;
}
//創建新的結點
LinkNode* Pnew=(LinkNode*)malloc(sizeof(LinkNode));
Pnew->data=https://bbs.csdn.net/topics/data;
Pnew->next=NULL;
LinkNode* Pcur=List->head;
for(int i=0;i<Pos;i++)
{
Pcur=Pcur->next;
}
Pnew->next=Pcur->next;
Pnew->next=Pnew;
List->size++;
}
//洗掉
void Remove_LinkList(LinkList* List,int Pos)
{
if(List==NULL||Pos<0||Pos>=List->size)
{
return;
}
LinkNode* Pcur=List->head;
for(int i=0;i<Pos;i++)
{
Pcur=Pcur->next;
}
//快取洗掉的結點
LinkNode* Pdel=Pcur->next;
Pcur->next=Pdel->next;
free(Pdel);
List->size--;
}
//獲得鏈表的長度
int Size_LinkList(LinkList* List)
{
return List->size;
}
//查找
int Find_LinkList(LinkList* List,void* data)
{
if(List==NULL||data=https://bbs.csdn.net/topics/=NULL)
{
return -1;
}
LinkNode* Pcur=List->head->next;
int i=0;
while(Pcur!=NULL)
{
if(Pcur->data=https://bbs.csdn.net/topics/data)break;
Pcur=Pcur->next;
i++;
}
return i;
}
//回傳第一個結點
void* Front_LinkList(LinkList* List)
{
return List->head->next;
}
//列印鏈表結點
void Print_LinkList(LinkList* List,PRINTLINKNODE print)
{
if(List==NULL)
{
return;
}
LinkNode* Pcur=List->head->next;
while(Pcur!=NULL)
{
print(Pcur->data);
Pcur=Pcur->next;
}
}
//釋放
void Free_LinkList(LinkList* List)
{
if(List==NULL)
{
return;
}
LinkNode* Pcur=List->head;
while(Pcur!=NULL)
{
LinkNode* Pnext=Pcur->next;
free(Pcur);
Pcur=Pnext;
}
List->size=0;
free(List);
}
#define _CRT_SECURE_NO_WARNINGS
#include"DynamicList.h"
typedef struct PERSON{
char name[64];
int age;
int score;
}Person;
//列印函式
void Myprint(void* data)
{
Person* p=(Person*)data;
printf("NAME:%S,AGE:%d,SCORE:%d\n",p->name,p->age,p->score);
}
int main()
{
LinkList* List=Init_LinkList();
Person p1={"A",18,100};
Person p2={"B",19,90};
Person p3={"C",20,80};
Insert_LinkList(List,0,&p1);
Insert_LinkList(List,0,&p2);
Insert_LinkList(List,0,&p3);
Print_LinkList(List,Myprint);
Free_LinkList(List);
system("pause");
return 0;
}
報錯:DynamicList.c
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(28): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(28): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(29): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(29): error C2223: “->data”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(30): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(30): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(32): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(32): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2143: 語法錯誤 : 缺少“)”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): warning C4552: “<”: 運算子不起任何作用;應輸入帶副作用的運算子
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(33): error C2059: 語法錯誤:“)”
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(34): error C2143: 語法錯誤 : 缺少“;”(在“{”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(35): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(35): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(35): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(37): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(37): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(37): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(37): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(38): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(38): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(38): error C2065: “Pnew”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(50): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(50): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2143: 語法錯誤 : 缺少“)”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): warning C4552: “<”: 運算子不起任何作用;應輸入帶副作用的運算子
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(51): error C2059: 語法錯誤:“)”
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(52): error C2143: 語法錯誤 : 缺少“;”(在“{”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(53): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(53): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(53): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(57): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(57): error C2065: “Pdel”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(57): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(57): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(59): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(59): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(59): error C2065: “Pdel”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(59): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(60): error C2065: “Pdel”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(60): warning C4022: “free”: 指標與實參 1 不匹配
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(77): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(77): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(78): error C2143: 語法錯誤 : 缺少“;”(在“型別”的前面)
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(79): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(79): warning C4047: “!=”:“int”與“void *”的間接級別不同
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(81): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(81): error C2223: “->data”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(82): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(82): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(82): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(83): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(85): error C2065: “i”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(99): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(99): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(100): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(100): warning C4047: “!=”:“int”與“void *”的間接級別不同
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(102): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(102): error C2223: “->data”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(102): error C2198: “print”: 用于呼叫的引數太少
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(103): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(103): error C2065: “Pcur”: 未宣告的識別符號
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(103): error C2223: “->next”的左側必須指向結構/聯合
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(113): error C2275: “LinkNode”: 將此型別用作運算式非法
1> c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.h(11) : 參見“LinkNode”的宣告
1>c:\users\71461\desktop\動態鏈表\動態鏈表\dynamiclist.c(113): er
uj5u.com熱心網友回復:
#include"DynamicList.h"
//初始化鏈表
LinkList* Init_LinkList()
{
LinkList* List=(LinkList*)malloc(sizeof(LinkList));
List->size=0;
List->head=(LinkNode*)malloc(sizeof(LinkNode));
List->head->data=https://bbs.csdn.net/topics/NULL;
List->head->next=NULL;
return List;
}
//指定位置插入
void Insert_LinkList(LinkList* List,int Pos,void* data)
{
if(List==NULL||data=https://bbs.csdn.net/topics/=NULL)
{
return;
}
if(Pos<0||Pos>List->size)
{
Pos=List->size;
}
//創建新的結點
LinkNode* Pnew=(LinkNode*)malloc(sizeof(LinkNode));
Pnew->data=https://bbs.csdn.net/topics/data;
Pnew->next=NULL;
LinkNode* Pcur=List->head;
for(int i=0;i<Pos;i++)
{
Pcur=Pcur->next;
}
Pnew->next=Pcur->next;
Pnew->next=Pnew;
List->size++;
}
//洗掉
void Remove_LinkList(LinkList* List,int Pos)
{
if(List==NULL||Pos<0||Pos>=List->size)
{
return;
}
LinkNode* Pcur=List->head;
for(int i=0;i<Pos;i++)
{
Pcur=Pcur->next;
}
//快取洗掉的結點
LinkNode* Pdel=Pcur->next;
Pcur->next=Pdel->next;
free(Pdel);
List->size--;
}
//獲得鏈表的長度
int Size_LinkList(LinkList* List)
{
return List->size;
}
//查找
int Find_LinkList(LinkList* List,void* data)
{
if(List==NULL||data=https://bbs.csdn.net/topics/=NULL)
{
return -1;
}
LinkNode* Pcur=List->head->next;
int i=0;
while(Pcur!=NULL)
{
//if(Pcur->data=https://bbs.csdn.net/topics/data)break;
if(Pcur->data=https://bbs.csdn.net/topics/=data)break; //判斷陳述句不要寫成賦值陳述句
Pcur=Pcur->next;
i++;
}
return i;
}
//回傳第一個結點
void* Front_LinkList(LinkList* List)
{
return List->head->next;
}
//列印鏈表結點
void Print_LinkList(LinkList* List,PRINTLINKNODE print)
{
if(List==NULL)
{
return;
}
LinkNode* Pcur=List->head->next;
while(Pcur!=NULL)
{
print(Pcur->data);
Pcur=Pcur->next;
}
}
//釋放
void Free_LinkList(LinkList* List)
{
if(List==NULL)
{
return;
}
LinkNode* Pcur=List->head;
while(Pcur!=NULL)
{
LinkNode* Pnext=Pcur->next;
free(Pcur);
Pcur=Pnext;
}
List->size=0;
free(List);
}
#define _CRT_SECURE_NO_WARNINGS
#include"DynamicList.h"
typedef struct PERSON{
char name[64];
int age;
int score;
}Person;
//列印函式
void Myprint(void* data)
{
Person* p=(Person*)data;
//printf("NAME:%S,AGE:%d,SCORE:%d\n",p->name,p->age,p->score);
printf("NAME:%s,AGE:%d,SCORE:%d\n",p->name,p->age,p->score); //%S改成%s
}
int main()
{
LinkList* List=Init_LinkList();
Person p1={"A",18,100};
Person p2={"B",19,90};
Person p3={"C",20,80};
Insert_LinkList(List,0,&p1);
Insert_LinkList(List,0,&p2);
Insert_LinkList(List,0,&p3);
Print_LinkList(List,Myprint);
Free_LinkList(List);
system("pause");
return 0;
}
供參考~
測驗沒有你貼出的這些問題,原始碼里有兩處錯誤,對比一下找出自己的問題。
檢查一下你的頭檔案名是否正確
uj5u.com熱心網友回復:
換成.cpp編譯沒有問題,.c一直出錯轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/87376.html
標籤:C語言
上一篇:qt自動生成的moc檔案編譯出錯
下一篇:考勤資料采集程式
