生活随笔
收集整理的這篇文章主要介紹了
第六天2017/04/11(1:结构体链表基础和相关经典操作)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、結構體基礎
【知識回顧】自定義數(shù)據(jù)結構的本質
#include "stdio.h"
#include "stdlib.h"
#include "string.h"struct student
{
char name[
64];
int age;
char *p;
};
struct teacher
{
char name[
64];
int age;
char *p;
struct student s1;
struct student *pstu;
struct teacher *ptea;
};
int main()
{
struct teacher t1;
struct student s2;t1.age =
40;t1.s1.age =
24;
t1.pstu = &s2; t1.pstu->age =
20; strcpy(t1.pstu->name,
"guojiawei");printf(
"%d,%s\n",t1.pstu->age,t1.pstu->name);getchar();
}
二、鏈表
鏈表的學習目錄
鏈表圖
#include "stdio.h"
#include "stdlib.h"
#include "string.h"typedef struct Node
{int
data;struct Node
*next;
}SLIST;
int create_SList(SLIST
** Head) ;
int SList_print(SLIST
* pHead);
int SList_Insert(SLIST
* pHead,int x,int y);
int SList_Delete(SLIST
* pHead,int y);
int SList_Destroy(SLIST
* pHead);
int SList_Reverse(SLIST
* pHead); int main()
{SLIST
*pHead
= NULL;
if(create_SList(
&pHead)
!= 0)
return -1;printf(
"打印鏈表:");SList_print(pHead);int x ;int y ;printf(
"輸入要查找結點為 x 的值 ");scanf(
"%d",
&x);printf(
"輸入要插入的值 y = ");scanf(
"%d",
&y);
if(SList_Insert(pHead,x,y)
!= 0){
return -1;}printf(
"在值為x的結點后面插入值為y的結點后,打印鏈表:\n");SList_print(pHead);
int rv;printf(
"輸入要刪除的值 y = ");scanf(
"%d",
&y);rv
= SList_Delete(pHead,y);
if(rv
== 0){printf(
"找到值為y的點,刪除的值為y的結點后,打印鏈表:\n");SList_print(pHead);}
else{printf(
"沒找到值為y的點,直接打印鏈表:\n");SList_print(pHead);}
SList_Reverse(pHead);printf(
"鏈表逆置后,打印鏈表:");SList_print(pHead);SList_Destroy(pHead);getchar();
}
int create_SList(SLIST
** Head)
{int ret
= 0;int
data = 0;SLIST
*pHead
= (SLIST
*)malloc(sizeof(SLIST));
if(pHead
==NULL){ret
= -1;printf(
"func create_SList() err:%d",ret);
return ret;}pHead
->data = 0;pHead
->next
= NULL;SLIST
*pCur
= NULL; pCur
= pHead; printf(
"請輸入數(shù)據(jù):(-1:輸入完畢)\n");scanf(
"%d",
&data);
while(
data !=-1){SLIST
*pNew
= (SLIST
*)malloc(sizeof(SLIST));
if(pNew
== NULL){ret
= -2;SList_Destroy(pHead); printf(
"func:create_SList() err malloc:%d",ret);
return ret;}pNew
->data = data;pCur
->next
= pNew; pNew
->next
= NULL;pCur
= pNew;printf(
"請輸入數(shù)據(jù):(-1:輸入完畢)\n");scanf(
"%d",
&data);}
*Head
= pHead;
return ret;
}
int SList_print(SLIST
* pHead)
{
if(pHead
== NULL)
return -1;SLIST
* pCur
= NULL;pCur
= pHead
->next;
while(pCur){printf(
"%7d",pCur
->data);pCur
= pCur
->next;}printf(
"\n");
return 0;
}
int SList_Insert(SLIST
* pHead,int x,int y)
{int ret
= 0;
if(pHead
== NULL) {ret
= -1;printf(
"func:SList_Insert error %d",ret);
return ret;}
SLIST
* pPre
= pHead;SLIST
* pCur
= pHead
->next;SLIST
* pTmp
= NULL;
while(pCur
!=NULL){
if(pCur
->data == x) {break;}pPre
= pCur;pCur
= pCur
->next;}
if(pCur
== NULL){pCur
= (SLIST
*)malloc(sizeof(SLIST));
if(pCur
== NULL){ret
= -2;printf(
"func:SList_Insert error: malloc %d",ret);SList_Destroy(pHead);
return ret;}pCur
->data = y;pPre
->next
= pCur;pCur
->next
= NULL;}
else {SLIST
* pNew
= (SLIST
*)malloc(sizeof(SLIST));
if(pNew
== NULL){ret
= -2;printf(
"func:SList_Insert error: malloc %d",ret);SList_Destroy(pHead);
return ret;}pNew
->data = y;pNew
->next
= pCur;pPre
->next
= pNew;}
return ret;
}
int SList_Delete(SLIST
* pHead,int y)
{
SLIST
*pPre
= pHead; SLIST
*pCur
= pHead
->next;
while(pCur
!= NULL){
if(pCur
->data == y){break;}pPre
= pCur;pCur
= pCur
->next;}
if(pCur
== NULL) {
return -1; }
else {pPre
->next
= pCur
->next;free(pCur);pCur
= NULL;
return 0;}
}
int SList_Destroy(SLIST
* pHead)
{SLIST
*pTmp
= NULL; SLIST
*pCur
= pHead;
if(pHead
== NULL)
return -1;
while(pCur){pTmp
= pCur
->next; free(pCur);pCur
= pTmp;}
return 0;
}int SList_Reverse(SLIST
* pHead)
{
if(
NULL==pHead) {
return -1; }
if( (
NULL==pHead
->next)
||(
NULL == pHead
->next
->next) )
return 0;
SLIST
*pCur
= pHead
->next
->next; SLIST
*pPre
= pHead
->next; SLIST
*pTmp
= NULL;
while(pCur
!= NULL){pTmp
= pCur
->next; pCur
->next
= pPre;pPre
= pCur;pCur
= pTmp;}
pHead
->next
->next
= NULL; pHead
->next
= pPre;
return 0;
}
鏈表逆置的解析圖
總結
以上是生活随笔為你收集整理的第六天2017/04/11(1:结构体链表基础和相关经典操作)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。