当前位置:编程学习 > C/C++ >>

C语言问题,看详细提问

程序运行要求如上。。是链表的操作

以下是我编的程序设计、貌似很多问题。这个问问有点恶心啊,最多发10000个字。后面的函数定义发不上来了。  各位高手帮我补充吧。 还有 哪里错误了帮我改下。  如果觉得麻烦的话你们自己编一个让我参考下?解决了再加50分= =、

//符号常量申明;
#include <stdio.h>
#include <stdlib.h>
#define NULLELEM '\0'
typedef char elemtype;
struct node            //结点数据类型
{
    elemtype data;        //数据域
    struct node *next;//指针域
};                    //链表结点结构体类型说明;
//函数声明;
int IsEmpty(struct node *head);  //判别是否空链
int Length(struct node *head);//用于计算单链表的长度,即统计结点个数
int Locate(struct node *head,elemtype x);//用于定位数据元素,即从头指针开始查找,依次访问每一个结点,查看结点数据域值是否等于给定的x,
elemtype Find(struct node *head,int i);
elemtype Next(struct node *head ,elemtype x);
elemtype Previous(struct node *head ,elemtype x);
void Traverse(struct node *head);
void CreateFromHead(struct node **head);
int Insert(struct node **Head,elemtype x,int i);
int Delete(struct node **head,int i);
main()
    {
 struct node *head=NULL, *p,*q;//声明变量
 int n=0;int no;elemtype x; int choice;CreateFromHead(&head);
//输出屏幕菜单;
printf("                   Linklist Operation Menu                           \n");
printf("---------------------------------------------------------------------\n");
printf("|           1:Create a linklist with head node from head            |\n");//头插法
printf("|           2:Create a linglist with head node from tail            |\n"); //尾插法
printf("|           3:Traverse a linklist                                   |\n"); //判别是否空链
printf("|           4:Calculate the length of the linglist                  |\n"); // 计算单链表长度
printf("|           5:Find a node of the linklist1                          |\n");  // 查找节点
printf("|           6:Locate the position of a node in linklist1            |\n");  // 定位节点
printf("|           7:Search the next node of a node in linklist1           |\n");//求取后继结点
printf("|           8:Search the previous node of a node in lingklist1      |\n"); //求取前驱结点
printf("|           9:Insert a node into the linklist1                      |\n");//插入一个结点
printf("|           10:Delete a node from the linglist1                     |\n"); //删除一个结点
printf("|           0:Exit                                                  |\n");
printf("---------------------------------------------------------------------\n");

//输入选择;
printf("Please input your choice:<0-10>\n");
scanf("%d",&choice);

 while(choice)//不退出
{switch(choice)      //利用switch/case结构调用不同的函数执行不同的选择;
{

case 1:
while(n<4)
    {
//申请结点空间
    p=(struct node*)malloc(sizeof(struct node));
//初始化结点数据域
    p->next=NULL;
    printf("input data:");
    p->data=getchar();
    getchar();
//挂链
    p->next=head;
    head=p;
    n++;}
//遍历
    p=head;
while(p)
    {printf("%4c",p->data);p=p->next;}
    putchar('\n');
//释放资源
 while (head)
{p=head;head=head->next;
free(p); }
break;

 

case 2:
while(n<4)
{p=(struct node*)malloc(sizeof(struct node));

p->next=NULL;
printf("input data:");
p->data=getchar();
getchar();

if(head==NULL)
{head=p;
}
else
{q=head;
while(q->next!=NULL)
q=q->next=p;}
n++;}

p=head;
while(p)
{printf("%4c",p->data);
p=p->next;}
putchar('\n');

while(head)
{p=head;
head=head->next;
free(p);}
break;

 

case 3:
printf("\nTraverse:\nthey are : ");
Traverse(head);
break;

 

case 4:
printf("\nLength:\nthere are %d nodes in this linklist.\n", Length(head));
break;

 

case 5:
x=Find(head, 3);
if(x) printf("\nFind:\nnode at position 3 is %c.\n", x);
break;

case 6:
no = Locate(head, 'D');
printf("\nLocate:\nD node is at position %d.\n", no);
break;

case 7:
x=Next(head, x);
 if(x) printf("\nNext:\nthe next node is %c.\n", x);
break;

 

case 8:
x=Previous(head, x);
 if(x) printf("\nPrevious:\nthe previous node is %c.\n", x);
break;

 

case 9:
printf("\nInsert:\n");
Insert(&head, 'D', 1); 
Traverse(head);

printf("\nInsert:\n");
Insert(&head, 'D', Length(head)+1); 
Traverse(head);
break;

 

case 10:
printf("\nDelete:\n");
Delete(&head, 1); 
Traverse(head);

printf("\nDelete:\n");
Delete(&head, Length(head)); 
Traverse(head);
break;

 

default://选择<0或>10,则提示输入错误
printf("Error choice!\n");
}
while(head)
 {p = head;
  head = head ->next;
  free(p);}
printf("Please input your choice:"); //提示选择屏幕菜单
scanf("%d",&choice); //读入屏幕选择}
    }

//各函数定义

补充://各函数定义
int Length(struct node * head)
{
 //遍历
 struct node  *p=head->next ;
 int n=0;
 while(p)
 {
  n++;
  p = p->next;
 }
 return(n);
}


int IsEmpty(struct node * head)
{
 if(head->next == NULL) return 1;
 return 0;
}

int Locate (struct node * head, elemtype x)
{
 struct node  *p=head->next ;
 int i, n = 0, size;
 size = Length(head);
 for(i=0; i<size; i++)
 {
  n++;
  if(p->data == x) break;
  p = p->next;
 }

 if(i<size ) return (n);
 else return (0);
}

elemtype  Find(struct node * head, int i)
{
 struct node  *p=head->next;
 int n=0;
 while(p)
 {
  n++;
  if (n == i )
   return (p->data);
  p=p->next ;
 }
 return NULLELEM;
}

elemtype Next(struct node * head, elemtype x)
{
 struct node  *p=head->next ;
 
 while(p)
 {
  if(p->data == x && p->next != NULL)
   return (p->next->data);
  p=p->next;
 }
 return NULLELEM;
}

elemtype Previous(struct node * head, elemtype x)
{
 struct node  *p=head->next, *q;
 if(IsEmpty(head)) return NULLELEM;
 q=p->next;
 while(q)
 {
  
  if(q->data == x)
   return (p->data);
  p=q;
  q=q->next;
 }
 return NULLELEM;
}

void Traverse(struct node * head)
{
 //遍历
 struct node  *p;
 p= head->next;
 while(p)
 {
  printf("%2c ->", p->data);
  p = p->next;
 }
 printf("\b\b  \n");
}


void CreateFromHead(struct node *head)
{
 struct node  *p;
 elemtype c;
 //采用相同的方式创建若干个结点
    while(1)
 {
  printf("input data:");
  c=getchar();
  getchar();
  if(c =='0') break;
  
  //申请结点空间
  p = (struct node *) malloc (sizeof(struct node));
  
  //初始化结点数据域 
  p->data = c;
  p->next = NULL;
    
  //挂链
  p->next = head->next;
  head->next = p;
 }
}


int Insert(struct node *head, elemtype x, int i)
{
 //将值为x的新结点插入到不带头结点的单链表head的第i个结点的位置上
 struct node *p=head , *s;
 int n=0;

 if(i!=1)
 {
  //寻找第i-1个结点
  while(p->next)//出口1
  {
   n++;
   if (n == i )   //找到则结束循环,出口2
    break;
   p=p->next ;
  }
  if (p==NULL)//出口1结束,则i<1或i>n+1时插入位置i有错
  {
   printf("position error\n");
   return 0;
  }
 } 
 //准备新结点
 s=(struct node *)malloc(sizeof(struct node));
 s->data=x; //初始化数据域为x
 //挂链:
 s->next=p->next;//新结点指向原来第i个结点(新链中第i+1个结点)
 p->next=s; //第i-1个结点指向新结点
 return 1;
}

int Delete(struct node * head, int i)
{
 //删除带头结点的单链表head上的第i个结点
 struct node *p=head ,*s;
 int n=0;
 
 if(i!=1)
 { //寻找第i-1个结点
  while(p->next) //出口1
  {
   n++;
   if (n == i )   //找到则结束循环,出口2
    break;
   p=p->next ;
  }
  //若出口1结束,则i错,结束时p->next ==NULL
  //若节点i-1存在,但若节点i不存在,也错,结束时条件仍为p->next ==NULL
  //因此若出口2结束,则p指向节点i-1,节点i也存在
  if (p->next==NULL)//i<1或i>n+1时插入位置i有错
  {
   printf("position error\n");
   return 0;
  }
 }
 s=p->next;//使s指向将被删除的第i个结点
 p->next=s->next;//将其从链上摘下
 free(s);//释放第i个结点的空间资源
 return 1;
}

追问:谢啦,补充好拉

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,