鍊錶基本操作的實現

2021-06-19 14:45:29 字數 1825 閱讀 8433

#include #include #define len sizeof(struct student)

/*----------------資料定義----------------------*/

//定義乙個學生資訊的結構體,包括學號,姓名和結構體型別的指標

struct student

;typedef struct student * stunode;

int n=0; //全域性變數,記錄鍊錶的長度

/*---------------函式宣告---------------------*/

stunode create(); //建立乙個新的鍊錶

void print(stunode head); //通過傳入的煉表頭指標列印整個鍊錶

stunode delete(stunode head,int num); //通過傳入的煉表頭指標和學生學號刪除節點

stunode insert(stunode head,stunode newstu); //依照學生學號的順序向鍊錶中插入新元素

/*---------------函式定義----------------------*/

struct student *create()

else

p2=p1;

p1=(struct student *)malloc(len);

printf("請輸入學生的學號和姓名:");

scanf("%ld %s",&p1->num,p1->name);

}//將尾節點的指標置為null

p2->next=null;

return head;

}void print(struct student *head)

else

}}struct student *delete(struct student * head,int num)

//遍歷節點,判斷當前節點是不是需要刪除的節點及是否為尾節點

//如果找到相應節點,或者已經遍歷到尾節點就跳出迴圈

while(p1->num!=num&&p1->next!=null)

//判斷是否找到相應節點

if(p1->num==num)

else

n=n-1;

printf("%ld 節點已刪除.\n",num);

}else

return head;

}struct student *insert(struct student * head,struct student * newstu)

else

//找到乙個比新學號大的節點

if(p0->num <= p1->num)

else

p0->next=p1;

}else

}//鍊錶長度加1

n=n+1;

printf("%ld 插入成功!\n",newstu->num);

return head;

}void main()

printf("請輸入要插入的節點:");

stu=(struct student *)malloc(len);

scanf("%ld %s",&stu->num,stu->name);

while(stu->num!=0)

print(head);

}

mingw5編譯通過,鍊錶結構是資料結構中的基礎,掌握鍊錶的邏輯,儲存結構和基本操作,並能自己用**實現,將有助於對後續複雜資料結構和演算法的學習!

鍊錶的基本操作實現

任務描述 前幾個的實現是線性表的基本操作 現在實現的是鍊錶基本操作的實現。基本上是建立新結點 結點的長度 刪除結點 插入結點 合併結點 顯示結點的功能。includeusing namespace std typedef int status 儲存結構的型別定義 返回函式的狀態結果 typedef ...

鍊錶基本操作的實現

include include define len sizeof struct student 資料定義 定義乙個學生資訊的結構體,包括學號,姓名和結構體型別的指標 struct student typedef struct student stunode int n 0 全域性變數,記錄鍊錶的長...

鍊錶基本操作的實現

include include include define len sizeof struct student typedef struct studentstu 定義節點 int n 0 全域性變數,用來記錄鍊錶的長度 int item 定義選單選項 int number 要刪除學生的學號 st...