Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89888
  • 博文数量: 99
  • 博客积分: 55
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 21:29
文章分类

全部博文(99)

文章存档

2013年(5)

2012年(94)

我的朋友

分类:

2012-12-28 19:03:07

原文地址:C语言 链表 作者:下雨夜

单向链表
#include
#include
#include
#define len sizeof(struct student)
struct student
{
char name[10];
int age;
struct student *next;
};
struct student *creat(struct student *head)
{
int n,m;
struct student *p1,*p2;
printf("Enter number of note:");
scanf("%d",&n);
m=n;
do
{
p1=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",p1->name,&p1->age);
if(m==n){
head->next=p1;
p2=p1;
}
else{
p2->next=p1;
p2=p1;
}
}while(--n);
p2->next=NULL;
return head;
}
struct student *print_list(struct student *head)
{
struct student *p1;
if(head->next==NULL){
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p1=head->next;
while(p1!=NULL){
printf("%s %d\n",p1->name,p1->age);
p1=p1->next;
}
}
return head;
}
struct student *add_list(struct student *head)
{
struct student *new_note,*p1;
int x,n=0;
new_note=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",new_note->name,&new_note->age);
p1=head->next;
while(p1!=NULL){
n++;
p1=p1->next;
}
printf("You have %d place to add a new note(1 to %d):",n+1,n+1);
scanf("%d",&x);
if(x<=0||x>(n+1)){
printf("enter error\n");
return head;
}
p1=head;
while((--x) && (p1=p1->next));
new_note->next=p1->next;
p1->next=new_note;
return head;
}
struct student *delete_list(struct student *head)
{
char name[20];
struct student *p1,*p2;
int flag;
if(head->next==NULL){
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p1=head;
p2=head->next;
printf("Who you want to delete:\n");
scanf("%s",name);
while((p2!=NULL) && strcmp(name,p2->name)){
p2=p2->next;
p1=p1->next;
}
if(p2!=NULL){
p1->next=p2->next;
free(p2);
}
}
return head;
}

struct student *invert(struct student *head)
{
struct student *p1,*p2,*p3,*temp;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
temp=p1=head->next;
p2=p3=p1->next;
while(p3!=NULL){
p3=p3->next;
p2->next=p1;
p1=p2;
p2=p3;
}
temp->next=NULL;
head->next=p1;
}
return head;
}
struct student *clear(struct student *head)
{
struct student *p1,*p2;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p1=p2=head->next;
while(p1!=NULL){
p1=p1->next;
free(p2);
p2=p1;
}
}
head->next=NULL;
return head;
}
int main()
{
struct student *head;
int choose;
head=(struct student *)malloc(len);
head->next=NULL;
while(1){
printf("1:Creat a new list\n");
printf("2:Print the list\n");
printf("3:Add a note to the list\n");
printf("4:Delete a note from the list\n");
printf("5:Invert the list\n");
printf("6:Clear the list\n");
printf("7:Exit\n");
printf("\nPlease choose one choice:");
scanf("%d",&choose);
switch(choose){
case 1:
printf("\n---------------creat a new list:-------------\n");
head=creat(head);
printf("\n");
break;
case 2:
printf("\n---------------print list:-------------------\n");
head=print_list(head);
printf("\n");
break;
case 3:
printf("\n---------------add a new note:-----------------\n");
head=add_list(head);
printf("\n");
break;
case 4:
printf("\n---------------delete a note:------------------\n");
head=delete_list(head);
printf("\n");
break;
case 5:
printf("\n---------------invert the list-----------------\n");
head=invert(head);
printf("\n");
break;
case 6:
printf("\n--------------clear the list-----------------\n");
head=clear(head);
printf("\n");
break;
case 7:
exit(0);
default:
printf("Enter error\n");
}
}
return 0;
}
 
 
双向链表
#include
#include
#include
#define len sizeof(struct student)
struct student
{
char name[10];
int age;
struct student *next;
struct student *pre;
};
struct student *creat(struct student *head)
{
int n,m;
struct student *p1,*p2;
printf("Enter number of note:");
scanf("%d",&n);
m=n;
do
{
p1=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",p1->name,&p1->age);
if(m==n){
head->next=p1;
p1->pre=head;
p2=p1;
}
else{
p2->next=p1;
p1->pre=p2;
p2=p1;
}
}while(--n);
p2->next=NULL;
return head;
}
struct student *print_list(struct student *head)
{
struct student *p1,*p2;
if(head->next==NULL){
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p2=head;
p1=head->next;
printf("Positive print the list\n");
while(p1!=NULL){
printf("%s %d\n",p1->name,p1->age);
p1=p1->next;
p2=p2->next;
}
printf("Reverse print the list\n");
while(p2!=head){
printf("%s %d\n",p2->name,p2->age);
p2=p2->pre;
}
}
return head;
}
struct student *add_list(struct student *head)
{
struct student *new_note,*p1;
int x,n=0;
new_note=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",new_note->name,&new_note->age);
p1=head->next;
while(p1!=NULL){
n++;
p1=p1->next;
}
printf("You have %d place to add a new note(1 to %d):",n+1,n+1);
scanf("%d",&x);
p1=head;
if(x==(n+1)){
while((p1->next!=NULL) && (p1=p1->next));
p1->next=new_note;
new_note->pre=p1;
new_note->next=NULL;
}
else{
while((--x) && (p1!=NULL) && (p1=p1->next));
new_note->next=p1->next;
p1->next->pre=new_note;
p1->next=new_note;
new_note->pre=p1;
}
return head;
}
struct student *delete_list(struct student *head)
{
char name[20];
struct student *p1,*p2;
int flag;
if(head->next==NULL){
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p1=head;
p2=head->next;
printf("Who you want to delete:\n");
scanf("%s",name);
while((p2!=NULL) && strcmp(name,p2->name)){
p2=p2->next;
p1=p1->next;
}
if(p2!=NULL && p2->next!=NULL){
p2->next->pre=p1;
p1->next=p2->next;
}
if(p2!=NULL && p2->next==NULL){
p1->next=NULL;
}
free(p2);
}
return head;
}

struct student *invert(struct student *head)
{
struct student *p1,*p2,*p3,*temp;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
temp=p1=head->next;
p2=p3=p1->next;
while(p3!=NULL){
p3=p3->next;
p2->next=p1;
p1->pre=p2;
p1=p2;
p2=p3;
}
temp->next=NULL;
head->next=p1;
p1->pre=head;
}
return head;
}
struct student *clear(struct student *head)
{
struct student *p1,*p2;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else{
p1=p2=head->next;
while(p1!=NULL){
p1=p1->next;
free(p2);
p2=p1;
}
}
head->next=NULL;
return head;
}
int main()
{
struct student *head;
int choose;
head=(struct student *)malloc(len);
head->next=NULL;
head->pre=NULL;
while(1){
printf("1:Creat a new list\n");
printf("2:Print the list\n");
printf("3:Add a note to the list\n");
printf("4:Delete a note from the list\n");
printf("5:Invert the list\n");
printf("6:Clear the list\n");
printf("7:Exit\n");
printf("\nPlease choose one choice:");
scanf("%d",&choose);
switch(choose){
case 1:
printf("\n---------------creat a new list:-------------\n");
head=creat(head);
printf("\n");
break;
case 2:
printf("\n---------------print list:-------------------\n");
head=print_list(head);
printf("\n");
break;
case 3:
printf("\n---------------add a new note:-----------------\n");
head=add_list(head);
printf("\n");
break;
case 4:
printf("\n---------------delete a note:------------------\n");
head=delete_list(head);
printf("\n");
break;
case 5:
printf("\n---------------invert the list-----------------\n");
head=invert(head);
printf("\n");
break;
case 6:
printf("\n--------------clear the list-----------------\n");
head=clear(head);
printf("\n");
break;
case 7:
exit(0);
default:
printf("Enter error\n");
}
}
return 0;
}
阅读(276) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~