鏈表
#include "stdio.h"
typedef struct Home
{
int fridge;
int washMachine;
struct Home *address;
}Home;
int main()
{
Home zaiHome,jiangHome,worldHome;
zaiHome.fridge=0;
zaiHome.washMachine=0;
zaiHome.address=&jiangHome;
jiangHome.fridge=1;
jiangHome.washMachine=0;
jiangHome.address=&worldHome;
worldHome.fridge=1;
worldHome.washMachine=1;
worldHome.address=NULL;
Home *p;
for(p=&zaiHome;p!=NULL;p=p->address)
{
printf("fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
}
}
#include "stdio.h"
typedef struct Home
{
int fridge;
int washMachine;
struct Home *address;
}Home;
int main()
{
Home zaiHome,jiangHome,worldHome;
zaiHome.fridge=0;
zaiHome.washMachine=0;
zaiHome.address=&jiangHome;
jiangHome.fridge=1;
jiangHome.washMachine=0;
jiangHome.address=&worldHome;
worldHome.fridge=1;
worldHome.washMachine=1;
worldHome.address=NULL;
Home *p=&zaiHome;
printf("zaiHome.fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
p=p->address;
printf("jiangHome.fridge=%d,jiangMachine=%d\n",p->fridge,p->washMachine);
p=p->address;
printf("worldHome.fridge=%d,worldMachine=%d\n",p->fridge,p->washMachine);
}
#include "stdio.h"
typedef struct P
{
int fridge;
int washMachine;
struct Home *address;
}Home;
- 作業(yè)
111;xx1;xx1@qq.com
112;xx2;xx2@qq.com
113;xx3;xx3@qq.com
xx1;xx1.@qq.com;111
xx2;xx2.@qq.com;112
xx3;xx3.@qq.com;113
//1.先將文件中的內容讀取到內存中,fgets()存到字符串數(shù)組
//2.按照;分割字符串
//3.交換位置
//4.在文件里輸出
## C語言鏈表
### 鏈表
include "stdio.h"
typedef struct Home
{
int fridge;
int washMachine;
struct Home *address;
}Home;
int main()
{
Home zaiHome,jiangHome,worldHome;
zaiHome.fridge=0;
zaiHome.washMachine=0;
zaiHome.address=&jiangHome;
jiangHome.fridge=1;
jiangHome.washMachine=0;
jiangHome.address=&worldHome;
worldHome.fridge=1;
worldHome.washMachine=1;
worldHome.address=NULL;
Home *p;
for(p=&zaiHome;p!=NULL;p=p->address)
{
printf("fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
}
}
include "stdio.h"
typedef struct Home
{
int fridge;
int washMachine;
struct Home *address;
}Home;
int main()
{
Home zaiHome,jiangHome,worldHome;
zaiHome.fridge=0;
zaiHome.washMachine=0;
zaiHome.address=&jiangHome;
jiangHome.fridge=1;
jiangHome.washMachine=0;
jiangHome.address=&worldHome;
worldHome.fridge=1;
worldHome.washMachine=1;
worldHome.address=NULL;
Home *p=&zaiHome;
printf("zaiHome.fridge=%d,washMachine=%d\n",p->fridge,p->washMachine);
p=p->address;
printf("jiangHome.fridge=%d,jiangMachine=%d\n",p->fridge,p->washMachine);
p=p->address;
printf("worldHome.fridge=%d,worldMachine=%d\n",p->fridge,p->washMachine);
}
include "stdio.h"
typedef struct P
{
int fridge;
int washMachine;
struct Home *address;
}Home;
>- 作業(yè)
111;xx1;xx1@qq.com
112;xx2;xx2@qq.com
113;xx3;xx3@qq.com
xx1;xx1.@qq.com;111
xx2;xx2.@qq.com;112
xx3;xx3.@qq.com;113
//1.先將文件中的內容讀取到內存中,fgets()存到字符串數(shù)組
//2.按照;分割字符串
include "srdio.h"
int *xxx()
{
//我們在堆區(qū)分配出來一塊空間
int *p=(int *)malloc(sizeof(int));
return p;
}
int main()
{
int *p=xxx();
*p=6;
free(p);//釋放p所指向的那塊空間指向的堆區(qū)
return *p;
}
include "stdio.h"
include "stdib.h"
typedef struct Property
{
int fridge;
int washMachine;
}Property;
typedef struct Home
{
Property property;
struct Home *next;
}Home;
int main()
{
Home *pZai=(Home *)malloc(sizeof(Home));
Home *pJiang=(Home *)malloc(sizeof(Home));
Home *pWorld=(Home *)malloc(sizeof(Home));
pZai->property.fridge=0;
pZai->property.washMachine=0;
pZai->next=pJiang;
pJiang->property.fridge=0;
pJiang->property.washMachine=1;
pJiang->next=pWorld;
pWorld->property.fridge=1;
pWorld->property.washMachine=1;
pWorld->next=NULL;
Home *temp;
for(temp=pZai;temp!=NULL;temp=temp->next)
{
printf("fridge=%d.washMachine=%d\n",temp->property.fridge,temp->property.washMachine);
}
return 0;
}
include "stdio.h"
include "stdlib.h"
typedef struct Property
{
int fridge;
int washMachine;
}Property;
typedef struct Home
{
Property property;
struct Home *next;
}Home;
int main()
{
Home *head=(Home *)malloc(sizeof(Home));
Home *pZai=(Home *)malloc(sizeof(Home));
Home *pJiang=(Home *)malloc(sizeof(Home));
Home *pWorld=(Home *)malloc(sizeof(Home));
//head所對應的空間地址域部分只存儲鏈表當中第一個有實際數(shù)據(jù)的節(jié)點地址葡兑;head所對應的空間數(shù)據(jù)域部分不存儲信息,我們叫做有頭節(jié)點.
head->next=pZai;
pZai->property.fridge=0;
pZai->property.washMachine=0;
pZai->next=pJiang;
pJiang->property.fridge=0;
pJiang->property.washMachine=0;
pJiang->next=pWorld;
pWorld->property.fridge=1;
pWorld->property.washMachine=1;
pWorld->next=NULL;
Home *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("fridge=%d.washMachine=%d\n",temp->property.fridge,temp->property.washMachine);
}
return 0;
}
>- 利用頭插法給鏈表填數(shù)據(jù)
include "stdio.h"
include "stdlib.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
int main()
{
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
pLINK head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
//1,頭插法赞草,給鏈表填入數(shù)據(jù)讹堤。
pLINK p=(pLINK)malloc(sizeof(LINK));
p->next=head->next;
head->next=p;
printf("請輸入學生信息:\n");
Student stu;
scanf("%s%d",stu.name,&stu.num);
p->data=stu;
//2.打印函數(shù)
pLINK temp=head->next;
for(;temp!=NULL;temp=temp->next)
{
printf("name=%s,num=%d\n",temp->data.name,temp->data.num);
}
}
>- 如果存在內存泄露(開辟了兩個head,而只有一個head有用時),解決方法1
include "stdio.h"
include "stdlib.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
pLINK createList(pLINK head)//接收開辟的空間的地址
{
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
}
return head;//不為NULL時,直接返回地址,不創(chuàng)建.
}
int main()
{
pLINK head=NULL;
pLINK head=(pLINK *)malloc(sizeof(pLINK));
head->next=NULL;
head=createList(head);//將堆區(qū)建立的head空間的地址傳給子函數(shù)里的(pLINK head).
。厨疙。洲守。。
return 0;
}
>- 解決方法2
include "stdio.h"
include "stdlib.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
pLINK createList(pLINK head)
{
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
pLINK head=(pLINK)malloc(sizeof(LINK));
if(head==NULL)
{
head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
}
return head;
}
int main()
{
pLINK head=NULL;
pLINK head=(pLINK *)malloc(sizeof(pLINK));
head=createList(head);
沾凄。梗醇。。搭独。
return 0;
}
include "stdio.h"
include "stdlib.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
pLINK createList(pLINK head)
{
if(NULL==head)
{
head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
}
return head;
}
//打印函數(shù)
void printData(pLINK head)
{
if(NULL==head)
{
printf("無信息可打印:\n");
return;
}
pLINK temp=head->next;
for(;temp!=NULL;temp=temp->next)
{
printf("name=%s,num=%d\n",temp->data.name,temp->data.num);
}
}
Student getData();
void headInsertData(pLINK head)
{
if(NULL==head)
{
printf("鏈表沒有創(chuàng)建:\n");
return;
}
pLINK p=(pLINK)malloc(sizeof(LINK));
p->next=head->next;
head->next=p;
p->data=getData();
}
Student getData()
{
printf("請輸入學生信息:\n");
Student stu;
scanf("%s%d",stu.name,&stu.num);
return stu;
}
int main()
{
pLINK head=NULL;
head=(pLINK)malloc(sizeof(LINK));
head=createList(head);
headInsertData(head);
printData(head);
return 0;
}
include "stdio.h"
include "stdlib.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
pLINK createList(pLINK head)
{
if(NULL==head)
{
head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
}
printf("鏈表創(chuàng)建成功\n");
return head;
}
//打印函數(shù)
void printData(pLINK head)
{
if(NULL==head)
{
printf("無信息可打印:\n");
}
pLINK temp=head->next;
printf("head-->");
for(;temp!=NULL;temp=temp->next)
{
printf("[%s,%d]--->",temp->data.name,temp->data.num);
}
printf("NULL\n");
}
Student getData();
void headInsertData(pLINK head)
{
if(NULL==head)
{
printf("鏈表沒有創(chuàng)建:\n");
return;
}
pLINK p=(pLINK)malloc(sizeof(LINK));
p->next=head->next;
head->next=p;
p->data=getData();
printf("數(shù)據(jù)插入成功\n");
}
Student getData()
{
printf("請輸入學生信息[name,num]:\n");
Student stu;
scanf("%s%d",stu.name,&stu.num);
return stu;
}
int main()
{
int select;
pLINK head=NULL;
while(1)
{
printf("==========\n");
printf("1.創(chuàng)建鏈表\n");
printf("2.頭插數(shù)據(jù)\n");
printf("3.打印數(shù)據(jù)\n");
printf("==========\n");
scanf("%d",&select);
switch(select)
{
case 1:
head=createList(head);
break;
case 2:
headInsertData(head);
break;
case 3:
printData(head);
break;
default:
break;
}
}
return 0;
}
include "stdio.h"
include "stdlib.h"
include "string.h"
typedef struct Student
{
char name[10];
int num;
}Student;
typedef struct LINK
{
Student data;
struct LINK next;
}LINK,pLINK;
//創(chuàng)建鏈表(創(chuàng)建頭節(jié)點)
pLINK createList(pLINK head)
{
if(NULL==head)
{
head=(pLINK)malloc(sizeof(LINK));
head->next=NULL;
}
printf("鏈表創(chuàng)建成功\n");
return head;
}
//打印函數(shù)
void printData(pLINK head)
{
if(NULL==head)
{
printf("無信息可打印\n");
return;
}
pLINK temp=head->next;
printf("head-->");
for(;temp!=NULL;temp=temp->next)
{
printf("[%s,%d]--->",temp->data.name,temp->data.num);
}
printf("NULL\n");
}
Student getData();
void headInsertData(pLINK head)
{
if(NULL==head)
{
printf("鏈表沒有創(chuàng)建:\n");
return;
}
pLINK p=(pLINK)malloc(sizeof(LINK));
p->next=head->next;
head->next=p;
p->data=getData();
printf("頭部數(shù)據(jù)插入成功\n");
}
void myScanf(char *str,int size)
{
int i=0;
for(i=0;i<size-1;i++)
{
str[i]=getchar();
if(str[i]=='\n')
{
break;
}
}
//說明在中途碰到\n
if(str[i]=='\n')
{
str[i]='\0';
}
else
{
str[i]='\0';
while(getchar()!='\n');//吸收緩存里存留的字符
}
}
Student getData()
{
printf("請輸入學生信息[name,num]:\n");
Student stu;
myScanf(stu.name,20);
scanf("%d",&stu.num);
return stu;
}
void tailInsertData(pLINK head)
{
//找尾指針:最后一個節(jié)點的地址
pLINK temp;
for(temp=head;temp->next!=NULL;temp=temp->next)
{
;
}
//這個循環(huán)跳出的條件:temp->next==NULL;
pLINK p=(pLINK)malloc(sizeof(LINK));
p->data=getData();
temp->next=p;
p->next=NULL;
printf("尾部數(shù)據(jù)插入成功\n");
}
void headDeleteData(pLINK head)
{
if(NULL==head||head->next==NULL)
{
printf("無信息可刪\n");
return;
}
pLINK p=head->next;
head->next=p->next;
free(p);
p=NULL;
printf("頭刪成功\n");
}
void tailDeleteData(pLINK head)
{
pLINK temp;
if(head==NULL||head->next==NULL)
{
printf("無信息可刪\n");
return;
}
//相當于等待狀態(tài)婴削,等待temp->next->next==NULL;
for(temp=head;temp->next->next!=NULL;temp=temp->next)
{
;
}
pLINK p=temp->next;
temp->next=NULL;
free(p);
p=NULL;
printf("尾刪成功\n");
}
void randomInsertData(pLINK head)
{
int n,count=1,i;
printf("請輸入距離頭部多少個位置插入數(shù)據(jù)\n");
scanf("%d",&n);
getchar();
if(n==0)
{
printf("輸入錯誤\n");
}
pLINK temp;
pLINK p=(pLINK)malloc(sizeof(LINK));
for(temp=head;temp->next!=NULL;temp=temp->next)
{
count++;
}
printf("count=%d\n",count);
if(n>count)
{
printf("超出鏈表的長度了,輸入無效\n");
}
else if(n==count)
{
temp->next=NULL;
p->next=temp->next;
temp->next=p;
p->data=getData();
printf("數(shù)據(jù)插入成功\n");
}
else if(n<count)
{
temp=head;
for(i=1;i<n;i++)
{
temp=temp->next;
}
printf("目前num=%d\n",temp->data.num);
p->next=temp->next;
temp->next=p;
p->data=getData();
printf("數(shù)據(jù)插入成功\n");
}
}
void insertData(pLINK head)
{
if(NULL==head)
{
printf("鏈表沒有創(chuàng)建:\n");
return;
}
int select;
while(1)
{
printf("==========\n");
printf("1.頭插數(shù)據(jù)\n");
printf("2.尾插數(shù)據(jù)\n");
printf("3.任意位置插入數(shù)據(jù)\n");
printf("4.返回上一層\n");
printf("==========\n");
scanf("%d",&select);
getchar();//吸收存留的\n
switch(select)
{
case 1:
headInsertData(head);
break;
case 2:
tailInsertData(head);
break;
case 3:
randomInsertData(head);
break;
case 4:
return;
default:
break;
}
}
}
void randomDeleteData(pLINK head)
{
int n,count=1,i;
printf("請輸入距離頭部多少個位置刪除數(shù)據(jù)\n");
scanf("%d",&n);
getchar();
if(n==0)
{
printf("輸入錯誤\n");
}
pLINK temp;
pLINK p=(pLINK)malloc(sizeof(LINK));
for(temp=head;temp->next!=NULL;temp=temp->next)
{
count++;
}
printf("count=%d\n",count);
if(n>=count)
{
printf("刪除失敗\n");
}
else if(n==count-1)
{
temp=head;
for(i=1;i<n;i++)
{
temp=temp->next;
}
p=temp->next;
temp->next=NULL;
free(p);
printf("數(shù)據(jù)刪除成功\n");
}
else if(n<count-1)
{
temp=head;
for(i=1;i<n;i++)
{
temp=temp->next;
}
p=temp->next;
temp->next=p->next;
free(p);
printf("數(shù)據(jù)刪除成功\n");
}
}
void deleteData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("無信息可刪\n");
return;
}
int select;
while(1)
{
printf("==========\n");
printf("1.頭刪數(shù)據(jù)\n");
printf("2.尾刪數(shù)據(jù)\n");
printf("3.任意位置刪除數(shù)據(jù)\n");
printf("4.返回上一層\n");
printf("==========\n");
scanf("%d",&select);
switch(select)
{
case 1:
headDeleteData(head);
break;
case 2:
tailDeleteData(head);
break;
case 3:
randomDeleteData(head);
break;
case 4:
return;
default:
break;
}
}
}
void searchData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("無信息可查詢\n");
return;
}
int num;
printf("請輸入要查詢的學號\n");
scanf("%d",&num);
pLINK temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
if(temp->data.num==num)
{
printf("[%s,%d]\n",temp->data.name,temp->data.num);
return;
}
}
if(NULL==temp)
{
printf("查無此人\n");
}
}
void changeData(pLINK head)
{
if(head==NULL||head->next==NULL)
{
printf("無信息可修改\n");
return;
}
char name[20];
printf("請輸入名字\n");
myScanf(name,20);
pLINK temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
if(strcmp(temp->data.name,name)==0)
{
printf("該學生原來信息:[%s,%d]\n",temp->data.name,temp->data.num);
printf("請輸入要修改的信息[姓名牙肝,學號]:");
Student stu;
myScanf(stu.name,20);
scanf("%d",&stu.num);
getchar();
temp->data=stu;
printf("修改成功\n");
return;
}
}
if(temp==NULL)
{
printf("查無此人\n");
}
}
int main()
{
pLINK head=NULL;
int select;
while (1)
{
printf("=========\n");
printf("1.創(chuàng)建鏈表\n");
printf("2.插入數(shù)據(jù)\n");
printf("3.刪除數(shù)據(jù)\n");
printf("4.打印數(shù)據(jù)\n");
printf("5.查找數(shù)據(jù)\n");
printf("6.修改數(shù)據(jù)\n");
printf("7.退出\n");
printf("=========\n");
scanf("%d",&select);
getchar();
switch (select)
{
case 1:
head=createList(head);
break;
case 2:
insertData(head);
break;
case 3:
deleteData(head);
break;
case 4:
printData(head);
break;
case 5:
searchData(head);
break;
case 6:
changeData(head);
break;
case 7:
return 0;
default:
break;
}
}
return 0;
}