顺序表
顺序表程序摘自一书,书名:C语言从入门到精通
#include <stdio.h>
#include <stdlib.h>
#define MAXLISTSIZE 1024
typedef struct
{
int data[MAXLISTSIZE];
int last;
}linearlist;
linearlist* CreateList()
{
linearlist *list = (linearlist*)malloc(sizeof(linearlist));
list->last = 0;
return list;
}
void ListList(linearlist* list)
{
int i;
printf("当前线性表的状态:");
if(list->last==0)
{
printf("当前顺序表为空");
}else{
for(i=0;i<(list->last);i++)
{
printf("[%4d]",list->data[i]);
}
printf("\n");
}
}
void Output(linearlist* list)
{
system("cls");
printf("------------------------------\n");
printf("- 顺序表 -\n");
printf("-a:追加一个节点i:插入一个节点-\n");
printf("-d:删除一个节点e:退出 -\n");
printf("------------------------------\n");
ListList(list);
}
void AppendNode(linearlist* list,int n)
{
if(list->last < MAXLISTSIZE)//if检测顺序表的范围,防止数据溢出;
{
list->data[list->last]=n;
list->last+=1;
}
}
void InsertNode(linearlist* list,int n,int pos)
{
int j;
if(pos<0 || pos>list->last)printf("所插入位置超出顺序表范围\n");
else{
for(j=list->last;j>=pos;j--)list->data[j+1]=list->data[j];
list->data[pos]=n;
list->last++;
}
}
void DeleteNode(linearlist* list,int pos)
{
int j;
if((pos<0)||(pos>list->last))
{
printf("所要删除的位置超出顺序表的范围\n");
}else{
for(j=pos;j<list->last;j++)
list->data[j]=list->data[j+1];
list->last--;
}
}
int main(void)
{
int key,pos; //key用于存储要放进顺序表的数据,pos用于存储顺序表检索下标
char ch; //ch用于存储用户的输入
linearlist *list; //结构体指针变量用于存储顺序表
list = CreateList();//创建顺序表
while(1)
{
Output(list);//输出界面和提示信息
printf("\n请选择:");
ch=getchar();
fflush(stdin);//下面根据不同输入执行不同的功能函数
if(ch=='a')
{
printf("请输入要追加的数据:");
scanf("%d",&key);
AppendNode(list,key);//追加顺序表中的数据
}
else if(ch=='i')
{
printf("请输入要插入的数据的位置:");
scanf("%d",&pos);
printf("请输入要插入的数据:");
scanf("%d",&key);
InsertNode(list,key,pos);//根据下标在顺序表中插入数据
}
else if(ch=='d')
{
printf("请输入要删除的数据的位置:");
scanf("%d",&pos);
DeleteNode(list,pos);//根据下标删除顺序表中的数据
}
else if(ch=='e')
{
Output(list);
fflush(stdin);
free(list);
exit(0);
}
}
return 0;
}
手撕链表
静态链表设置
编译环境linux—gcc
#include <stdio.h>
struct link
{
int data;//数据域
struct link *next;//指针域
};
int main(void)
{
struct link node1={1,NULL};
struct link node2={2,NULL};
struct link node3={3,NULL};
node1.next=&node2;
node2.next=&node3;
printf("node1:%d\n",node1.data);
printf("node2:%d\n",node1.next->data);
printf("node3:%d\n",node1.next->next->data);
return 0;
}
链表相关操作
#include <stdio.h>
#include <stdlib.h>
//**********************节点模板
typedef struct node
{
int data; //数据域
struct node *next; //指针域 链表是无序数据,因为指针域而产生不同节点的联系
}NODE;
//*****************************链表相关操作函数
NODE* creatLinkFromHead(NODE *head);//创建链表,节点从头添加
NODE* creatLinkFromLast(NODE *head);//创建链表,节点从尾添加
void linkPRIN(NODE *head); //链表遍历输出
int countLinkNode(NODE *head); //链表节点计数
int pointLinkNode(NODE *head,int pointdata);//链表数据查询
int linkNodeDataChange(NODE *head,int pointdata,int newdata);//链表数据修改
NODE* linkNodeInsertBefore(NODE *head,int pointdata,NODE *newNode);//指定节点前插入新节点
NODE* linkNodeInsertBehind(NODE *head,int pointdata,NODE *newNode);//指定节点后插入新节点
NODE* foundLinkLastNode(NODE *head);//找到并返回单链表的尾节点
NODE* linkInsertLinkBefore(NODE *head,int pointdata,NODE *insertLinkHead,NODE *insertLinkFail);//指定节点前插入新链表
NODE* linkInsertLinkBehind(NODE *head,int pointdata,NODE *insertLinkHead,NODE *insertLinkFail);//指定节点后插入新链表
NODE* linkNodeDelete(NODE *head,int pointdata);//删除指定节点
NODE* linkChangeover(NODE *head);//链表逆转
void freeLink(NODE *head);//销毁链表
//*****************************下面是主函数
int main(void)
{
NODE *head=NULL;
NODE *fail=NULL;
head=creatLinkFromHead(head);
linkPRIN(head);
fail=foundLinkLastNode(head);
fail->next=head;
linkPRIN(head);//这里形成了循环链表
return 0;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* creatLinkFromHead(NODE *head)
{
NODE *new=NULL;
while(1)
{
new=(NODE*)malloc(sizeof(NODE));
printf("(quit:0)input node data:");
scanf("%d",&new->data);
getchar();
if(new->data==0)
{
free(new);
return head;
}
if(head==NULL)head=new;
else{
new->next=head;
head=new;
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* creatLinkFromLast(NODE *head)
{
NODE *new=NULL;
NODE *point=head;
while(1)
{
new=(NODE*)malloc(sizeof(NODE));
printf("(quit:0)input node data:");
scanf("%d",&new->data);
getchar();
if(new->data==0)
{
free(new);
return head;
}
if(head==NULL)
{
head=new;
point=head;
}else{
while(point->next!=NULL)
{
point=point->next;
}
point->next=new;
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void linkPRIN(NODE *head)
{
while(head!=NULL)
{
printf("%d ->",head->data);
head=head->next;
}
putchar('\n');
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int countLinkNode(NODE *head)
{
int i=0;
while(head!=NULL)
{
i++;
head=head->next;
}
return i;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int pointLinkNode(NODE *head,int pointdata)
{
while(head!=NULL)
{
if(head->data==pointdata)return 1;
}
head=head->next;
return 0;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int linkNodeDataChange(NODE *head,int pointdata,int newdata)
{
while(head!=NULL)
{
if(head->data==pointdata)
{
head->data=newdata;
return 1;
}
head=head->next;
}
return 0;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkNodeInsertBefore(NODE *head,int pointdata,NODE *newNode)
{
NODE *point=head;
if(point->data==pointdata)
{
newNode->next=point;
return newNode;
}else{
while(point->next!=NULL)
{
if(point->next->data==pointdata)
{
newNode->next=point->next;
point->next=newNode;
break;
}
point=point->next;
}
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkNodeInsertBehind(NODE *head,int pointdata,NODE *newNode)
{
NODE *point=head;
while(point!=NULL)
{
if(point->data==pointdata)
{
newNode->next=point->next;
point->next=newNode;
break;
}
point=point->next;
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* foundLinkLastNode(NODE *head)
{
while(head->next!=NULL)
{
head=head->next;
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkInsertLinkBefore(NODE *head,int pointdata,NODE *insertLinkHead,NODE *insertLinkFail)
{
NODE *point=head;
if(point->data==pointdata)
{
insertLinkFail->next=point;
return insertLinkHead;
}else{
while(point->next!=NULL)
{
if(point->next->data==pointdata)
{
insertLinkFail->next=point->next;
point->next=insertLinkHead;
break;
}
point=point->next;
}
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkInsertLinkBehind(NODE *head,int pointdata,NODE *insertLinkHead,NODE *insertLinkFail)
{
NODE *point=head;
while(point!=NULL)
{
if(point->data==pointdata)
{
insertLinkFail->next=point->next;
point->next=insertLinkHead;
break;
}
point=point->next;
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkNodeDelete(NODE *head,int pointdata)
{
NODE *freeNode=NULL;
NODE *point=head;
if(point->data==pointdata)
{
freeNode=point;
point=point->next;
free(freeNode);
return point;
}else{
while(point->next!=NULL)
{
if(point->next->data==pointdata)
{
freeNode=point->next;
point->next=point->next->next;
free(freeNode);
break;
}
point=point->next;
}
}
return head;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
NODE* linkChangeover(NODE *head)
{
NODE *before=NULL;
NODE *behind;
while(head!=NULL)
{
behind=before;
before=head;
head=head->next;
before->next=behind;
}
return before;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>
void freeLink(NODE *head)
{
NODE *freeNode=NULL;
while(head!=NULL)
{
freeNode=head;
head=head->next;
free(freeNode);
}
}
队列 (在链表尾部增加新节点,在头部删除旧节点)
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 10
typedef struct team
{
int data;//数据域
struct team *next;//指针域
}TEAM;
TEAM* creatTeam();//创建队列
void printTeam(TEAM *head);//输出队列
TEAM* changeTeam(TEAM *head);//队列改变
void freeTeam(TEAM *head);//销毁队列
int main(void)
{
int len=7;
TEAM *head;
head=creatTeam();
printTeam(head);
while(len--)
{
head=changeTeam(head);
printTeam(head);
}
freeTeam(head);
return 0;
}
TEAM* creatTeam()
{
int i=MAXLEN;
TEAM *head=NULL;
TEAM *new=NULL;
TEAM *point=head;
while(i--)
{
new=(TEAM*)malloc(sizeof(TEAM));
printf("(quit:0)input node data:");
scanf("%d",&new->data);
getchar();
if(new->data==0)
{
free(new);
return head;
}
if(head==NULL)
{
head=new;
point=head;
}else{
while(point->next!=NULL)
{
point=point->next;
}
point->next=new;
}
}
return head;
}
void printTeam(TEAM *head)
{
while(head!=NULL)
{
printf("[%d] ",head->data);
head=head->next;
}
putchar('\n');
}
TEAM* changeTeam(TEAM *head)
{
int i=MAXLEN;
TEAM *new=NULL;
TEAM *point=head;
TEAM *freeNode=head;
new=(TEAM*)malloc(sizeof(TEAM));
printf("input data:");
scanf("%d",&new->data);
while(point->next!=NULL && i>1)
{
point=point->next;
i--;
}if(i>1){
point->next=new;
}else{
point->next=new;
head=head->next;
free(freeNode);
}
return head;
}
void freeTeam(TEAM *head)
{
TEAM *freeNode=NULL;
while(head!=NULL)
{
freeNode=head;
head=head->next;
free(freeNode);
}
}
链表练手应用编程
成绩管理系统,以前写的,可优化的点很多。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/************************************************************ structrue model ***************************************************************/
typedef struct nianji
{
char nname[20];
struct banji *head;
struct nianji *next;
}NJ;
typedef struct banji
{
char bname[20];
struct banji *next;
struct student *head;
}BJ;
typedef struct student
{
char *name;
int yw;
int sx;
int yy;
struct student *next;
}SD;
/*************************************************************************print informetion************************************************************************************/
void printInformetion(NJ *nhead)
{
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("nianji is %s\n",nhead->nname);
BJ *bhead=nhead->head;
while(bhead!=NULL)
{
printf("Class name :%s\n",bhead->bname);
SD *shead=bhead->head;
while(shead!=NULL)
{
printf("Student %6s score(Y,S,W) %5d %5d %5d\n",shead->name,shead->yw,shead->sx,shead->yy);
shead=shead->next;
}
bhead=bhead->next;
}
putchar('\n');
}
/*******************************************input informetion************************************************/
NJ* scanInformetion()
{
int blen=0;
int slen=0;
NJ *njhead=(NJ*)malloc(sizeof(NJ));
NJ *npoint=njhead;
printf("please input nianji name:");
scanf("%s",npoint->nname);
printf("How many Class?--------------->:");
scanf("%d",&blen);
BJ *bhead=(BJ*)malloc(sizeof(BJ));
npoint->head=bhead;
printf("please input Class name------------------------>:");
scanf("%s",bhead->bname);
while(blen--)
{
SD *shead=(SD*)malloc(sizeof(SD));
bhead->head=shead;
printf("How many studen?-------------------->:");
scanf("%d",&slen);
printf("please input student name----->:");
shead->name=(char*)malloc(10);
scanf("%s",shead->name);
printf("please input student chinses score---->:");
scanf("%d",&shead->yw);
printf("please input student math score---->:");
scanf("%d",&shead->sx);
printf("please input student english score---->:");
scanf("%d",&shead->yy);
while(--slen)
{
SD *snew=(SD*)malloc(sizeof(SD));
printf("please input student name----->:");
snew->name=(char*)malloc(10);
scanf("%s",snew->name);
printf("please input student chinses score---->:");
scanf("%d",&snew->yw);
printf("please input student math score---->:");
scanf("%d",&snew->sx);
printf("please input student english score---->:");
scanf("%d",&snew->yy);
while(shead->next!=NULL)
{
shead=shead->next;
}
shead->next=snew;
}
if(blen==0)break;
BJ *bnew=(BJ*)malloc(sizeof(BJ));
printf("please input Class name------------------------>:");
scanf("%s",bnew->bname);
bhead->next=bnew;
bhead=bhead->next;
}
return njhead;
}
/****************************************display*********************************************/
void display()
{
int i;
for(i=0;i<5;i++)
{
if(i==2)
{
printf("@@@@@@@@@@@@@@@@@ Students score system @@@@@@@@@@@@@@@@@\n");
}
else{
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
}
putchar('\n');
}
/***************************************** scoreDispose *********************************************/
void scoreDispose(NJ *njhead)
{
int maxtotal1=0;
int mintotal1=0;
NJ *point=njhead;
BJ *bhead=point->head;
SD *shead=NULL;
SD *max=NULL;
SD *min=NULL;
SD *MAX=NULL;
SD *MIN=NULL;
MAX=MIN=bhead->head;
printf("*****************every class's every student'total score and maxscore and minscore*******************\n");
printf("NIAN JI %s\n",point->nname);
maxtotal1=mintotal1=bhead->head->yw+bhead->head->sx+bhead->head->yy;
while(bhead!=NULL)
{
int maxtotal2;
int mintotal2;
max=bhead->head;
min=bhead->head;
printf("Class is %s\n",bhead->bname);
shead=bhead->head;
maxtotal2=mintotal2=shead->yw+shead->sx+shead->yy;
while(shead!=NULL)
{
int total=0;
float ave=0;
total=shead->yw+shead->sx+shead->yy;
ave=(float)total/3;
printf("student (%5s) total score is (%5d) average score is (%.2f)\n",shead->name,total,ave);
if(maxtotal2<total)
{
maxtotal2=total;
max=shead;
}
if(mintotal2>total)
{
mintotal2=total;
min=shead;
}
shead=shead->next;
}
printf("Class MAX: %5s %5d\nClass MIN: %5s %5d\n",max->name,max->yw+max->sx+max->yy,min->name,min->yw+min->sx+min->yy);
if(maxtotal1<maxtotal2)
{
maxtotal1=maxtotal2;
MAX=max;
}
if(mintotal1>maxtotal2)
{
maxtotal2=maxtotal2;
MIN=min;
}
bhead=bhead->next;
}
printf("&&&&&&&&&&&&&&&&&&& Nian ji maxscore and minscore &&&&&&&&&&&&&&&&&&\n\n");
printf("Nianji MAX:%s %d\nNianji MIN:%s %d\n\n",MAX->name,MAX->yw+MAX->sx+MAX->yy,MIN->name,MIN->yw+MIN->sx+MIN->yy);
}
/*********************************************************************************************/
void informationXG(NJ *head)
{
char bn[20];
BJ *bhead=head->head;
printf("you want XG banji:");
scanf("%s",bn);
while(bhead!=NULL)
{
if(strcmp(bhead->bname,bn)==0)
{
int xgname;
char xgn[20];
printf("you need XG name?(yes is 1,no is anykey):");
scanf("%d",&xgname);
if(xgname==1)
{
printf("please input new class name:");
scanf("%s",xgn);
strcpy(bhead->bname,xgn);
}
char sn[20];
SD *shead=bhead->head;
printf("you want XG student:");
scanf("%s",sn);
while(shead!=NULL)
{
int score=0;
int xz=0;
if(strcmp(shead->name,sn)==0)
{
int xgname1;
char xgn1[20];
printf("you need XG student name?(yes is 1,no is anykey):");
scanf("%d",&xgname1);
if(xgname==1)
{
printf("please input new student name:");
scanf("%s",xgn1);
strcpy(shead->name,xgn1);
}
printf("success found the student ,XG(1.YW 2.SX 3.YY):");
scanf("%d",&xz);
printf("XG score to newscore:");
scanf("%d",&score);
switch(xz)
{
case 1:
shead->yw=score;
break;
case 2:
shead->sx=score;
break;
case 3:
shead->yy=score;
break;
//case :break;
default:printf("your input is error!\n");
}
}else{
printf("No found the student!\n");
}
shead=shead->next;
}
}else{
printf("Haven't the banji.\n");
}
bhead=bhead->next;
}
}
/**************************************** main *********************************************/
int main()
{
display();
NJ *njhead=scanInformetion();
printInformetion(njhead);
scoreDispose(njhead);
informationXG(njhead);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
informationXG(njhead);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
informationXG(njhead);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
printInformetion(njhead);
scoreDispose(njhead);
free(njhead);
return 0;
}
链表贪吃蛇,用了linux下的图形库和线程库,编译要链接库。
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
typedef struct Snake
{
int hang;
int lie;
struct Snake *next;
}snake;
int key;
int dir;
snake *head=NULL;
snake *tail=NULL;
snake food;
void initFood()
{
int x=(rand()%48)+1;
int y=(rand()%48)+1;
food.hang=x;
food.lie=y;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasSnakeNode(int hang,int lie)
{
snake *node;
node=head;
while(node != NULL)
{
if(node->hang==hang && node->lie==lie)
{
return 1;
}
node=node->next;
}
return 0;
}
int hasFood(int hang,int lie)
{
if(food.hang==hang && food.lie==lie)
{
return 1;
}
return 0;
}
void gamePic()
{
int hang;
int lie;
move(0,0); //<---------------------------------
for(lie=0;lie<50;lie++)
{
for(hang=0;hang<50;hang++)
{
if(lie==0||lie==49)
{
printw("<>");
}else{
if(hang==0||hang==49)
{
printw("||");
}else{
if(hasSnakeNode(hang,lie))
{
printw("[]");
}else if(hasFood(hang,lie))
{
printw("oo");
}else{
printw(" ");
}
}
}
}
printw("\n");
}
printw("By QianXi! <EATint python [%d,%d]> ",food.hang,food.lie);
}
void addnode()
{
snake *new=(snake*)malloc(sizeof(snake));
new->next=NULL;
switch(dir)
{
case UP:
new->hang=tail->hang;
new->lie =tail->lie-1;
break;
case DOWN:
new->hang=tail->hang;
new->lie =tail->lie+1;
break;
case LEFT:
new->hang=tail->hang-1;
new->lie =tail->lie;
break;
case RIGHT:
new->hang=tail->hang+1;
new->lie =tail->lie;
break;
}
tail->next=new;
tail=new;
}
void initsnake()
{
snake *p;
dir=DOWN;
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
initFood();
head=(snake*)malloc(sizeof(snake));
head->hang=24;
head->lie=24;
head->next=NULL;
tail=head;
addnode();
}
void deletnode()
{
snake *p;
p=head;
head=head->next;
free(p);
}
int ifsnakeDie()
{
snake *p;
p=head;
if(tail->hang==0 || tail->lie==0 || tail->hang==49 || tail->lie==49)
{
return 1;
}
while(p->next != NULL)
{
if(p->hang == tail->hang && p->lie == tail->lie)
{
return 1;
}
p=p->next;
}
return 0;
}
void movesnake()
{
addnode();
if(hasFood(tail->hang,tail->lie))
{
initFood();
}else{
deletnode();
}
if(ifsnakeDie())
{
initsnake();
}
}
void* refreshJieMian()
{
while(1)
{
movesnake();
gamePic();
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir)!=abs(direction))
{
dir=direction;
}
}
void* changDir()
{
while(1)
{
key=getch();
switch(key)
{
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initsnake();
gamePic();
pthread_create(&t1,NULL,refreshJieMian,NULL);
pthread_create(&t2,NULL,changDir,NULL);
while(1);
getch();
endwin();
return 0;
}