标签:ice ima str logs cmd insert windows lib head
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
int cmd;
typedef struct Lnode
{
int data;
struct Lnode *next;
}LNode, *Linklist;
void creatList(Linklist &L)
{
LNode *p;
int n,m;
printf(" 先输入一个n,在输入n个数: ");
scanf("%d",&n);
//p = (Linklist)malloc(sizeof(LNode));
L = p;
while(n--)
{
LNode *q;
q = (LNode*)malloc(sizeof(LNode));//这一步一定要重新申请内存,不然 q的指向永远不会变,
//导致一个单节点q=p,且自身的next指向自身;
scanf("%d",&m);
q->data = m;
p->next = q;
p = q;
}
p->next = NULL;
}
void Print(Linklist &L)
{
LNode *p;
p=L->next;
printf(" 链表输出为");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
printf(" 是否继续 yes 0r no? :");
char str[4];
scanf("%s",str);
if(strcmp(str,"yes"))
return ;
}
void Listinert(Linklist &L, int i, int e)
{
Linklist head = L;
int j = 0;
Linklist p = head;
while (p != NULL&&j < i - 1)
{
j++; p = p->next;
}
if (!p || j > i - 1)
printf("worry\n");
else
{
Linklist s=new LNode; //= (Linklist)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
}
}
void Listdelete(Linklist &L, int i)
{
int j=0;
Linklist p = L;
Linklist q;
while(p->next&&j<i-1)
{
p=p->next;j++;
}
if(!p->next||(j>i-1))
j=j;
else
{
q=p->next;p->next=q->next;
free(q);
}
}
void length(Linklist &L)
{
LNode *p;
p=L->next;
int l=0;
while(p!=NULL)
{
l++;
p=p->next;
}
printf("%d\n",l);
printf(" 是否继续 yes 0r no? :");
char str[4];
scanf("%s",str);
if(strcmp(str,"yes")||strcmp(str,"YES")||strcmp(str,"Yes"))
int w;
}
void menu()
{ //打印图形界面
printf(" -------MENU------ ");
printf("\n");
printf(" ================= ");
printf("\n");
printf(" Linklist operation \n");
printf(" 1. creat \n");
printf(" 2. insert \n");
printf(" 3. delete \n");
printf(" 4. print \n");
printf(" 5. length \n");
printf(" 6. reverse \n");
printf(" 0. exit \n");
printf(" ================= ");
printf("\n");
printf(" Choice(0,1,2,3,4,5,6): ");
scanf("%d",&cmd);
}
int main()
{
int flag=1,pos,num;
Linklist L1;
do
{ system("cls");
menu();
switch(cmd)
{
case 0:
flag=0;break;
case 1:
creatList(L1); break;
case 2:
printf(" 输入插入的位置和对象: ");
scanf("%d %d",&pos,&num);
Listinert(L1,pos,num);
break;
case 3:
printf(" 输入删除的位置: ");
scanf("%d",&pos);
Listdelete(L1,pos);break;
case 4:
Print(L1);break;
case 5:
printf(" 链表的长度为: ");
length(L1);
}
}while(flag==1);
}
标签:ice ima str logs cmd insert windows lib head
原文地址:http://www.cnblogs.com/Left-Behind/p/7520520.html