双向链表(案例)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Data{
char name[20];
int age;
int score;
}data_t;
typedef struct Stu{
data_t data;
struct Stu* next;
struct Stu* prev;
}stu_t;
stu_t* initStu(){
stu_t* head = malloc(sizeof(stu_t));
memset(head,0,sizeof(stu_t));
head->next = NULL;
head->prev = NULL;
}
void printStu(stu_t* head){
stu_t* p = head->next;
while(p!=NULL){
printf("*************\n");
printf("姓名:%s\n",p->data.name);
printf("年龄:%d\n",p->data.age);
printf("分数:%d\n",p->data.score);
p = p->next;
}
printf("*************\n");
}
stu_t* findStu(stu_t* head){
char name[20] = {0};
stu_t* p = head->next;
printf("请输入学生姓名:");
scanf("%s",&name);
while(getchar()!=10);
while(p!=NULL){
if(strcmp(p->data.name,name)==0){
return p;
}
p = p->next;
}
return NULL;
}
void removeStu(stu_t* head){
stu_t* tar = findStu(head);
if(tar==NULL){return;}
tar->prev->next = tar->next;
if(tar->next != NULL){
tar->next->prev = tar->prev;
}
free(tar);
}
void freeStu(stu_t* head){
stu_t* p = head;
while(p->next!=NULL){
p = p->next;
free(p->prev);
}
free(p);
}
void insertStu(stu_t* head){
data_t st = {0};
char name[20] = {0};
printf("请输入姓名:");
scanf("%s",name);
while(getchar()!=10);
static int age = 20;
static int score = 80;
strcpy(st.name,name);
st.age = age;
st.score = score;
age++;
score++;
stu_t* newNode = calloc(1,sizeof(stu_t));
newNode->data = st;
stu_t* p = head;
while(p->next != NULL){
p = p->next;
}
p->next = newNode;
newNode->next = NULL;
newNode->prev = p;
}
int main(){
stu_t* head = malloc(sizeof(stu_t));
memset(head,0,sizeof(stu_t));
insertStu(head);
insertStu(head);
insertStu(head);
printStu(head);
printf("\n");
removeStu(head);
printStu(head);
freeStu(head);
return 0;
}
双向循环链表(案例)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Data{
char name[20];
int age;
int score;
}data_t;
typedef struct Stu{
data_t data;
struct Stu* next;
struct Stu* prev;
}stu_t;
stu_t* initStu(){
stu_t* head = malloc(sizeof(stu_t));
memset(head,0,sizeof(stu_t));
head->next = head;
head->prev = head;
}
void printStu(stu_t* head){
stu_t* p = head->next;
while(p != head){
printf("*************\n");
printf("姓名:%s\n",p->data.name);
printf("年龄:%d\n",p->data.age);
printf("分数:%d\n",p->data.score);
p = p->next;
}
printf("*************\n");
}
stu_t* findStu(stu_t* head){
char name[20] = {0};
stu_t* p = head->next;
printf("请输入学生姓名:");
scanf("%s",name);
while(getchar()!=10);
while(p!=head){
if(strcmp(p->data.name,name)==0){
return p;
}
p = p->next;
}
return head;
}
void removeStu(stu_t* head){
stu_t* tar = findStu(head);
tar->prev->next = tar->next;
tar->next->prev = tar->prev;
free(tar);
}
void freeStu(stu_t* head){
stu_t* p = head;
while(p->next!=head){
p = p->next;
free(p->prev);
}
free(p);
}
void insertStu(stu_t* head){
data_t data = {0};
char name[20] = {0};
int age = 0,score = 0;
printf("请输入学生姓名:");
scanf("%s",name);
while(getchar()!=10);
printf("请输入学生年龄:");
scanf("%d",&age);
while(getchar()!=10);
printf("请输入学生成绩:");
scanf("%d",&score);
while(getchar()!=10);
strcpy(data.name,name);
data.age = age;
data.score = score;
stu_t* newNode = calloc(1,sizeof(stu_t));
newNode->data = data;
stu_t* p = head->prev;
p->next = newNode;
newNode->next = head;
head->prev = newNode;
newNode->prev = p;
}
int compare_by_score(stu_t* a,stu_t* b){
return a->data.score - b->data.score;
}
void lqsort(stu_t* head,stu_t* tail,int (*comp)(stu_t*,stu_t*)){
stu_t* left = head;
stu_t* right = tail;
if(left==right || right->next == left){return;}
stu_t* l = left;
stu_t* r = right;
stu_t* key = left;
data_t temp = {0};
while(l!=r){
while(l!=r){
if(comp(key,r)<0){
break;
}else{
r = r->prev;
}
}
while(l!=r){
if(comp(key,l)>0){
break;
}else{
l = l->next;
}
}
temp = l->data;
l->data = r->data;
r->data = temp;
}
temp = l->data;
l->data = left->data;
left->data = temp;
lqsort(head,l->prev,comp);
lqsort(l->next,tail,comp);
}
int main(){
stu_t* head = initStu();
int i = 0;
for(i=0;i<3;i++){
insertStu(head);
}
printf("初始化完成\n");
lqsort(head->next,head->prev,compare_by_score);
printStu(head);
printf("\n");
removeStu(head);
printStu(head);
freeStu(head);
return 0;
}
双向链表完成注册登录功能
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Data{
char name[20];
char pswd[20];
}data_t;
typedef struct Stu{
data_t data;
struct Stu* next;
struct Stu* prev;
}stu_t;
stu_t* initStu(){
stu_t* head = calloc(1,sizeof(stu_t));
head->next = NULL;
head->prev = NULL;
return head;
}
void printStu(stu_t* head){
stu_t* p = head->next;
while(p!=NULL){
printf("*************\n");
printf("账号:%s\n",p->data.name);
printf("密码:%s\n",p->data.pswd);
p = p->next;
}
printf("*************\n");
}
stu_t* findStu(stu_t* head,char* name){
stu_t* p = head->next;
while(p!=NULL){
if(strcmp(p->data.name,name)==0){
return p;
}
p = p->next;
}
return NULL;
}
void regist(stu_t* head){
char name[20] = {0};
char pswd[20] = {0};
printf("请输入账号:");
scanf("%s",name);
while(getchar()!=10);
stu_t* res = findStu(head,name);
if(res != NULL){
printf("账号以存在\n");
return ;
}
printf("请输入密码:");
scanf("%s",pswd);
stu_t* newSt = calloc(1,sizeof(stu_t));
strcpy(newSt->data.name,name);
strcpy(newSt->data.pswd,pswd);
stu_t* p = head;
while(p->next != NULL){
p = p->next;
}
p->next = newSt;
newSt->next = NULL;
newSt->prev = p;
}
stu_t* login(stu_t* head){
char name[20] = {0};
char pswd[20] = {0};
while(1){
printf("请输入账号:");
scanf("%s",name);
while(getchar()!=10);
printf("请输入密码:");
scanf("%s",pswd);
while(getchar()!=10);
stu_t* res = findStu(head,name);
if(res==NULL||strcmp(pswd,res->data.pswd)!=0){
printf("账号或密码错误\n");
printf("输入q+回车退出登录,输入其他则继续:");
if(getchar()=='q'){
while(getchar()!='\n');
break;
}
}else{
printf("登录成功\n");
return res;
}
}
return 0;
}
void updatePswd(stu_t* self){
char pswd[20] = {0};
printf("请输入新的密码:");
scanf("%s",pswd);
while(getchar()!=10);
strcpy(self->data.pswd,pswd);
}
void removeStu(stu_t* self){
self->prev->next = self->next;
if(self->next != NULL){
self->next->prev = self->prev;
}
}
void userInterface(stu_t* head,stu_t* self){
int res = 0;
int ch = 0;
while(1){
printf("1:查看个人信息\n");
printf("2:修改密码\n");
printf("3:注销\n");
printf("0:退出登录\n");
scanf("%d",&ch);
while(getchar()!='\n');
switch(ch){
case 1:
printf("账号:%s\n",self->data.name);
printf("密码:%s\n",self->data.pswd);
break;
case 2:
updatePswd(self);
return;
case 3:
removeStu(self);
return;
case 0:
return ;
default:
break;
}
}
}
int main(){
int ch = 0;
stu_t* head = initStu();
stu_t* res = 0;
while(1){
printf("1:注册\n");
printf("2:登录\n");
printf("0:退出\n");
scanf("%d",&ch);
while(getchar()!='\n');
switch(ch){
case 1:
regist(head);
break;
case 2:
res = login(head);
if(res!=NULL){
userInterface(head,res);
}
break;
case 0:
break;
default:
break;
}
}
return 0;
}