(c基础)上课笔记 12.21
2016-12-22 本文已影响10人
霸王小
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct student
{
int ID;
char name[10];
struct student *next;
}STU,*pSTU;
pSTU add_STU(pSTU head)
{
pSTU temp=(pSTU)malloc(sizeof(STU)),p=head;
temp->next=NULL;
printf("in put(ID,name) :");
scanf("%d,%s",&temp->ID,temp->name);
while(p->next!= NULL)
{
p=p->next;
}
p->next=temp;
temp=NULL;
return head;
}
void show_del_STU(pSTU head)
{
pSTU p=head,p1=head->next->next;
if(head->next!=NULL)
{
while(p1!=NULL)
{
p=p->next;
p1=p1->next;
}
free(p->next);
p->next=NULL;
}
}
void show_add_STU(pSTU head)
{
pSTU p=head->next;
while(1)
{
if(p!=NULL)
{
printf("%5d,%s\n",p->ID,p->name);
p=p->next;
}
else break;
}
}
pSTU find_noed(pSTU head)
{
pSTU p=head->next;
char name[10];
scanf("%s",name);
for(;NULL!=p;p=p->next)
{
if(strcmp(name,p->name)==0)
{
printf("已查找到%s,该ID为%d.",name,p->ID);
break;
}
}
if(NULL==p)
printf("查找不到.\n");
return head;
}
void menu(pSTU head)
{
int n=0;
while(1)
{
system("clear");
printf("1.增加\n");
printf("2.显示\n");
printf("3.删除\n");
printf("4.查找\n");
printf("0.\n");
scanf("%d",&n);
switch(n)
{
case 1:head=add_STU(head);break;
case 2:{show_add_STU(head);getchar();getchar();break;}
case 3:show_del_STU(head);break;
case 4:find_noed(head);getchar();getchar();break;
case 0:exit(0);
default:break;
}
}
}
void main()
{
pSTU head=(pSTU)malloc(sizeof(STU));
head->next=NULL;
menu(head);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define S sizeof(struct student)
typedef struct student
{
int ID;
char name[10];
struct student *next;
}STU;
STU *will_add_STU(STU *head,int n)
{
int i;
STU *temp=(STU *)malloc(S);
STU *p=head;
printf("请输入name:");
scanf("%s",temp->name);
temp->ID=6-n;
for(i=0;i<n;p=p->next,i++);
temp->next=p->next;
p->next=temp;
temp=NULL;
return head;
}
void show_link(STU *head)
{
STU *p=head;
if(NULL==p->next)
printf("此链表为空。\n");
for(;NULL!=p->next;p=p->next)
{
printf("ID为%d,name为%s.\n",p->next->ID,p->next->name);
}
}
STU *add_STU(STU *head,int n)
{
STU *temp=(STU *)malloc(S);
temp->ID=n;
strcpy(temp->name,"wang");
temp->next=head->next;
head->next=temp;
temp=NULL;
return head;
}
void main()
{
int i,n;
STU *head=(STU *)malloc(S);
head->next=NULL;
for(i=1;i<=5;i++)
head=add_STU(head,i);
show_link(head);
printf("请输入要增加的ID:");
scanf("%d",&n);
head=will_add_STU(head,n);
show_link(head);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define S sizeof(struct student)
typedef struct student
{
int ID;
char name[10];
struct student *next;
}STU;
STU *new_link()
{
STU *head=(STU *)malloc(S);
head->next=NULL;
return head;
}
STU *add_STU(STU *head)
{
int i;
for(i=1;i<=10;i++)
{
STU *temp=(STU *)malloc(S);
STU *p=head;
strcpy(temp->name,"wang");
temp->ID=i;
for(;NULL!=p->next;p=p->next);
if(NULL==p->next)
temp->next=NULL;
p->next=temp;
temp=NULL;
}
return head;
}
STU *del_STU(STU *head)
{
STU *p=head;
if(NULL==p->next)
return head;
for(;NULL!=p->next->next;p=p->next);
if(NULL==p->next->next)
{
free(p->next);
p->next=NULL;
}
return head;
}
void show_link(STU *head)
{
STU *p=head;
if(NULL==p->next)
printf("该链表为空!\n");
printf("\n=================================\n");
for(;NULL!=p->next;p=p->next)
printf("=\tID为%2d,name为%s.\t=\n",p->next->ID,p->next->name);
printf("=================================\n");
sleep(3);
}
void menu()
{
STU *head;
while(1)
{
system("clear");
printf("\t1.创建链表。\n");
printf("\t2.尾插。\n");
printf("\t3.尾删。\n");
printf("\t4.显示链表。\n");
printf("\t0.退出。\n\t");
int n;
scanf("%d",&n);
switch(n)
{
case 1:head=new_link();break;
case 2:head=add_STU(head);break;
case 3:head=del_STU(head);break;
case 4:show_link(head);break;
case 0:exit(0);break;
default:printf("请重新输入!");break;
}
}
}
void main()
{
menu();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define S sizeof(struct student)
typedef struct student
{
int ID;
char name[10];
struct student *next;
}STU;
STU *plist=NULL;
void create()
{
STU *temp=(STU *)malloc(S);
scanf("%d,%s",&temp->ID,temp->name);
temp->next=plist;
plist=temp;
temp=NULL;
return ;
}
void tail()
{
STU *temp=(STU *)malloc(S);
printf("input ID,name:");
scanf("%d,%s",&temp->ID,temp->name);
if(plist==NULL)
{
plist=temp;
temp->next=NULL;
}
STU *p=plist;
for(;NULL!=p->next;p=p->next);
p->next=temp;
temp->next=NULL;
return ;
}
void show()
{
STU *p=plist;
while(p!=NULL)
{
printf("\t%d\t%s\n",p->ID,p->name);
p=p->next;
}
return ;
}
void main()
{
int i;
for(i=0;i<5;i++)
create();
show();
}
-
作业
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZEOF sizeof(struct student)
typedef struct student
{
int ID;
char name[10];
struct student *next;
struct student *previous;
}STU;
STU *add_STU(STU *head)
{
STU *temp=(STU *)malloc(SIZEOF);
printf("请输入ID和name:");
scanf("%d,%s",&temp->ID,temp->name);
if(head->next==NULL)
{
temp->next=head->next;
head->next=temp;
temp->previous=head;
temp=NULL;
}
else
{
temp->next=head->next;
head->next->previous=temp;
temp->previous=head;
head->next=temp;
temp=NULL;
}
return head;
}
STU *del_STU(STU *head)
{
STU *p=head->next;
if(head->next==NULL)
printf("该指针为空,无法删除!\n");
else
{
//p=p->next;
head->next = p->next;
p->next->previous=head;
free(p);
//p->next=NULL;
}
return head;
}
void show_link(STU *head)
{
STU *p=head->next;
if(p==NULL)
printf("该链表为空!\n");
for(;NULL!=p;p=p->next)
printf("\tID为%2d,name为%s.\n",p->ID,p->name);
}
void show_link1(STU *head)
{
STU *p=head;
if(p==NULL)
printf("该链表为空!\n");
for(;NULL!=p->next;p=p->next);
for(;NULL!=p->previous;p=p->previous)
printf("\tID为%2d,name为%s.\n",p->ID,p->name);
}
void main()
{
STU *head=(STU *)malloc(SIZEOF);
head->next=NULL;
head->previous=NULL;
int i;
for(i=0;i<3;i++)
head=add_STU(head);
show_link(head);
printf("========================\n");
show_link1(head);
printf("========================\n");
head = del_STU(head);
show_link(head);
printf("========================\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZEOF sizeof(struct student)
typedef struct student
{
int ID;
char name[10];
struct student *next;
struct student *previous;
}STU;
STU *add_STU(STU *head)
{
STU *temp=(STU *)malloc(SIZEOF);
STU *p=head;
printf("请输入ID和name:");
scanf("%d,%s",&temp->ID,temp->name);
for(;NULL!=p->next;p=p->next);
temp->next=p->next;
p->next=temp;
temp->previous=p;
temp=NULL;
return head;
}
STU *del_STU(STU *head)
{
STU *p=head;
for(;NULL!=p->next;p=p->next);
p->previous->next=NULL;
free(p->previous->next);
}
void show_link(STU *head)
{
STU *p=head;
if(p->next==NULL)
printf("该链表为空!\n");
for(;NULL!=p->next;p=p->next)
printf("\tID为%2d,name为%s.\n",p->next->ID,p->next->name);
}
void show_link1(STU *head)
{
STU *p=head;
if(head->next==NULL)
printf("该链表为空!\n");
for(;NULL!=p->next;p=p->next);
for(;NULL!=p->previous;p=p->previous)
printf("\tID为%2d,name为%s.\n",p->ID,p->name);
}
void main()
{
STU *head=(STU *)malloc(SIZEOF);
head->next=NULL;
head->previous=NULL;
int i;
for(i=0;i<3;i++)
head=add_STU(head);
printf("====================\n");
show_link(head);
printf("====================\n");
show_link1(head);
printf("====================\n");
del_STU(head);
show_link(head);
printf("====================\n");
}