33,311
社区成员
发帖
与我相关
我的任务
分享
// lianbiao.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//#include <vld.h>
#include <malloc.h>
#include <windows.h>
struct lb
{
int num;
lb *next;
};
lb *head;
lb *init_list()
{
head = (lb *)malloc(sizeof(lb));
head->num = 0;
head->next = NULL;
return head;
}
lb *add(lb *head,int num)
{
lb *c = head; //当前节点
lb *p = NULL; //新节点
while (c->next != NULL)
{
c = c->next;
}
p = (lb *)malloc(sizeof(lb));
p->num = num;
p->next = NULL;
c->next = p;
return head;
}
lb *showlist(lb *head)
{
lb *c;
for (c = head->next;c->next != NULL;c = c->next)
{
printf("%d\n",c->num);
}
printf("%d\n",c->num);
return head;
}
void deletelist(lb *head)
{
lb *p;
lb *c;
for (c = head;c->next != NULL;c = p)
{
p = c->next;
free(c);
}
free(c);
}
lb *insertlist(lb *head,int num,int data)
{
lb *c;
lb *p;
int count = 1;
for (c = head->next;c->next != NULL;c = c->next)
{
count++;
if (count == num)
{
p = (lb *)malloc(sizeof(lb));
p->num = data;
p->next = c->next;
c->next = p;
}
}
if(count < num)
printf("链表中没有指定的位置,或者不允许从最后插入。");
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
init_list();
for (int i = 1;i < 6; ++i)
{
add(head,i);
}
insertlist(head,3,10);
showlist(head);
deletelist(head);
system("pause");
return 0;
}
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define mStu (Stu *)malloc(sizeof(Stu))
typedef struct student
{
int age;
struct student *next;
}Stu;
int main()
{
int count=0;
Stu *head=NULL;//声明头结点
Stu *ListCreat(int *p,Stu *head);//建立链表
Stu *ListOrder(int count,Stu *head);//进行排序
void Print(Stu *head);//打印链表
Stu *ListAdd(Stu *head);//插入链表
Stu *ListDelete(Stu *head);//删除链表
head=ListCreat(&count,head);
Print(head);printf("\n");
head=ListOrder(count,head);
Print(head);printf("\n");
head=ListAdd(head);
Print(head);printf("\n");
head=ListDelete(head);
Print(head);
getch();
}
Stu *ListCreat(int *p,Stu *head)
{
char answer=0;
Stu *New=mStu, *Old=NULL;
do
{
(*p)++;
printf("Please input a age:");
fflush(stdin);
scanf("%d",&New->age);
if(head==NULL)
head=New;
else
{
Old->next=New; //这里不给Old分配内存行吗??答:可以的,因为第一次不会执行这个else 语句 然后Old就会指向New的空间,第二次Old就会有指向的空间了,不会出现内存错误
}
Old=New;
New->next=NULL;
New=mStu;
printf("Do you want to input again?(y or n)");
fflush(stdin);
scanf(" %c",&answer);
}while(tolower(answer)=='y');
return head;
}
void Print(Stu *head)
{
Stu *p1=head;
while(p1!=NULL)
{
printf("%d ",p1->age);
p1=p1->next;
}
}
Stu *ListOrder(int count,Stu *head)
{
int temp=0;
Stu *p1=NULL,*p2=NULL;
for(int i=0;i<count;i++)
{
for(p1=head,p2=head->next;p2!=NULL;p1=p1->next,p2=p2->next)
{
if(p1->age > p2->age)
{
temp=p1->age;
p1->age=p2->age;
p2->age=temp;
}
}
}
return head;
}
Stu *ListAdd(Stu *head)
{
char answer1=0;
Stu *add=NULL;
Stu *p1=NULL,*p2=NULL;
do
{
add=mStu;
p1=head;
p2=NULL;//注意,这里对add,p1,p2做再次初始化非常重要
printf("\nPlease add a list:");
fflush(stdin);
scanf("%d",&add->age);
while(p1!=NULL && p1->age < add->age)
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)
{
p2->next=add;
add->next=NULL;
}
else if(p2==NULL)
{
add->next=head;
head=add;
}
else
{
p2->next=add;
add->next=p1;
}
printf("Do you want to add again??");
fflush(stdin);
scanf("%c",&answer1);
}while(tolower(answer1)=='y');
return head;
}
Stu *ListDelete(Stu *head)
{
char answer2=0;
Stu *Del=NULL,*p1=NULL,*p2=NULL;
do
{
Del=mStu;
p1=head;
p2=NULL;//注意,这里也很重要
printf("Which list do you want to delete??");
scanf("%d",&Del->age);
fflush(stdin);
while(p1!=NULL && p1->age!=Del->age)
{
p2=p1;
p1=p1->next;
}
if(p2==NULL)
head=head->next;
else if(p1==NULL)
printf("\nThere is no this data exit!!! Input Error!!!");
else
{
p2->next=p1->next;//删除p1结点
free(p1);
}
printf("\nDo you want to delete again???(y or n)");
scanf("%c",&answer2);
}while(tolower(answer2)=='y');
return head;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct student
{
int id; //学号
char name[20]; //姓名
struct student *next;
}student;
student *head = NULL;
int length; //链表的长度
void create()
{
student *p1,*p2;
length=0;
p1=(student *)malloc(sizeof(student));
p1->id=-1;
if(head==NULL)
head=p1;
printf("请输入学生的学号、姓名:\n");
while(1) //学号为0的时候退出
{
p2=(student *)malloc(sizeof(student));
scanf("%d %s",&p2->id,p2->name); //输入学生信息
if(p2->id==0)
{
printf("链表创建完成!\n");
break;
}
length++; //链表的长度
p1->next=p2;
p2->next=NULL;
p1=p1->next;
}
return ;
}
void display()
{
student *p=head->next;
printf("链表中所有的学生信息如下:\n");
while(p!=NULL)
{
printf("%d %s\n",p->id,p->name);
p=p->next;
}
return ;
}
void search()
{
int num;
student *p=head->next;
printf("需要查找的学生学号为:");
scanf("%d",&num);
while(p!=NULL)
{
if(p->id==num)
{
printf("学生学号为%d的学生的信息如下:\n",num);
printf("%d %s/n",p->id,p->name);
return;
}
p=p->next;
}
if(p==NULL)
printf("无此记录!\n");
return ;
}
void insert()
{
int num,i;
student *p,*q;
p=head;
printf("请输入你要插入位置: ");
scanf("%d",&num);
if(num>length)
{
printf("找不到要插入的位置\n");
return ;
}
else
{
printf("请输入你要插入的学生的信息:\n");
q=(student *)malloc(sizeof(student));
scanf("%d %s",&q->id,q->name);
while(p!=NULL)
{
if(p->id==q->id)
{
printf("该学号已经存在,无法插入!\n");
return ;
}
p=p->next;
}
p=head;
for(i=0;i <num;i++)
p=p->next;
q->next=p->next;
p->next=q;
length++;
printf("插入成功!\n");
return ;
}
}
void Delete()
{
int num;
student *p,*q;
q=head,p = head->next;
printf("请输入要删除的学生的学号:\n");
scanf("%d",&num);
while(p!=NULL)
{
if(p->id==num)
{
q->next=p->next;
free(p);
length--;
printf("删除成功!\n");
return ;
}
p=p->next;
q=q->next;
}
if(p==NULL)
{
printf("找不到要删除的编号!\n");
return ;
}
}
void menu()
{
printf("________________________________________________________________\n");
printf("| 0、 退出系统 |\n");
printf("| 1、 建立链表 |\n");
printf("| 2、 显示链表 |\n");
printf("| 3、 查找链表中的某个元素 |\n");
printf("| 4、 删除链表中指定学号的结点 |\n");
printf("| 5、 指定的位置上插入一个新结点 |\n");
printf("________________________________________________________________\n");
return ;
}
int main(void)
{
int a;
menu();
while(1)
{
printf("请选择相应的功能:");
cin>>a;
switch(a)
{
case 0:
return 0;
case 1:
create(); //从键盘输入学生信息
menu();
break;
case 2:
if(head)
{
display();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 3:
if(head)
{
search();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 4:
if(head)
{
Delete();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
case 5:
if(head)
{
insert();
menu();
}
else
{
printf("链表为空,请先建立链表!\n");
menu();
}
break;
default:
break;
}
}
return 0;
}