華農oj數據結構——8579

8579 鏈式線性表的基本操作
時間限制:1000MS 代碼長度限制:10KB
提交次數:5567 通過次數:2176

題型: 編程題 語言: G++;GCC
Description 編寫算法,創建一個含有n個元素的帶頭結點的單鏈表L並實現插入、刪除、遍歷操作。本題目提供部分代碼,請補全內容。
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 創建含有n個元素的單鏈表
LinkList p,q;
int i;
ElemType e;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL; // 先建立一個帶頭結點的單鏈表
q = (LinkList)malloc(sizeof(LNode));
q = L;
for (i=0; i<n; i++) {
scanf("%d", &e);
p = (LinkList)malloc(sizeof(LNode)); // 生成新結點
// 請補全代碼

}
return OK;
}

int LoadLink_L(LinkList &L){
// 單鏈表遍歷
LinkList p = L->next;
if(___________________________)printf(“The List is empty!”); // 請填空
else
{
printf(“The LinkList is:”);
while(___________________________) // 請填空
{
printf("%d “,p->data);
___________________________ // 請填空
}
}
printf(”\n");
return OK;
}

int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在帶頭結點的單鏈線性表L中第i個位置之前插入元素e
// 請補全代碼

}

int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在帶頭結點的單鏈線性表L中,刪除第i個元素,並用e返回其值
// 請補全代碼

}

int main()
{
LinkList T;
int a,n,i;
ElemType x, e;
printf(“Please input the init size of the linklist:\n”);
scanf("%d",&n);
printf(“Please input the %d element of the linklist:\n”, n);
if(___________________________) // 判斷鏈表是否創建成功,請填空
{
printf(“A Link List Has Created.\n”);
LoadLink_L(T);
}
while(1)
{
printf(“1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n”);
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(___________________________) printf(“Insert Error!\n”); // 判斷i值是否合法,請填空
else printf(“The Element %d is Successfully Inserted!\n”, x);
break;
case 2: scanf("%d",&i);
if(___________________________) printf(“Delete Error!\n”); // 判斷i值是否合法,請填空
else printf(“The Element %d is Successfully Deleted!\n”, e);
break;
case 3: LoadLink_L(T);
break;
case 0: return 1;
}
}
}

輸入格式
測試樣例格式說明:
根據菜單操作:
1、輸入1,表示要實現插入操作,緊跟着要輸入插入的位置和元素,用空格分開
2、輸入2,表示要實現刪除操作,緊跟着要輸入刪除的位置
3、輸入3,表示要輸出順序表的所有元素
4、輸入0,表示程序結束

輸入樣例
3
3 6 9
3
1
4 12
2
1
3
0

輸出樣例
Please input the init size of the linklist:
Please input the 3 element of the linklist:
A Link List Has Created.
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 12 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:6 9 12
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 創建含有n個元素的單鏈表
  LinkList p,q;
  int i;
  ElemType e;
  L = (LinkList)malloc(sizeof(LNode));
  L->next = NULL;              // 先建立一個帶頭結點的單鏈表
  //q = (LinkList)malloc(sizeof(LNode));
  q = L;
  for (i=0; i<n; i++) {
	 scanf("%d", &e);
    p = (LinkList)malloc(sizeof(LNode));  // 生成新結點
    p->data=e;
    p->next=NULL;
    q->next=p;
    q=q->next;

  }
  return OK;
}

int LoadLink_L(LinkList &L){
// 單鏈表遍歷
 LinkList p = L->next;
 if(p==NULL)printf("The List is empty!"); // 請填空
 else
 {
	 printf("The LinkList is:");
	 while(p)    // 請填空
	 {
		printf("%d ",p->data);
		p=p->next;    // 請填空
	 }
 }
 printf("\n");
 return OK;
}

int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在帶頭結點的單鏈線性表L中第i個位置之前插入元素e
// 請補全代碼
    int cnt=0;
    LinkList p=L;
    while(p&&cnt<i-1){
        p=p->next;
        cnt++;
    }
    if(p&&cnt==(i-1)){
        LinkList N;
        N=(LinkList)malloc(sizeof(LNode));
        N->data=e;
        N->next=NULL;
        N->next=p->next;
        p->next=N;
        return OK;
    }else{
        return ERROR;
    }

}

int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在帶頭結點的單鏈線性表L中,刪除第i個元素,並用e返回其值
// 請補全代碼
    int cnt=0;
    LinkList p=L;
    while(p&&cnt<i-1){
        p=p->next;
        cnt++;
    }
    if(p&&cnt==(i-1)&&p->next){
        LinkList T;
        T=p->next;
        e=T->data;
        p->next=T->next;
        free(T);
        return OK;
    }else{
        return ERROR;
    }

}

int main()
{
 LinkList T;
 int a,n,i;
 ElemType x, e;
 printf("Please input the init size of the linklist:\n");
 scanf("%d",&n);
 printf("Please input the %d element of the linklist:\n", n);
 if(CreateLink_L(T,n))     // 判斷鏈表是否創建成功,請填空
 {
	 printf("A Link List Has Created.\n");
	 LoadLink_L(T);
 }
 while(1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: scanf("%d%d",&i,&x);
				  if(!LinkInsert_L(T,i,x)) printf("Insert Error!\n"); // 判斷i值是否合法,請填空
				  else printf("The Element %d is Successfully Inserted!\n", x);
				  break;
			case 2: scanf("%d",&i);
				  if(!LinkDelete_L(T,i,e)) printf("Delete Error!\n"); // 判斷i值是否合法,請填空
				  else printf("The Element %d is Successfully Deleted!\n", e);
				  break;
			case 3: LoadLink_L(T);
				  break;
			case 0: return 1;
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章