星星博客 »  > 

江西师范大学数据结构线性表下编程题(c语言版)

作为一名非常非常菜的大一学生,写这种填空类似的题目简直就是要我的命,与其填空还不如让我自己写呜呜呜,特别是输出语句让你觉得很莫名其妙,第一道就被卡住了,我甚至觉得是题目的问题,为什么会判断true???还输出一个回答错误。于是我把他改成了false,把语句也改了,结果给我wa了,当时我人傻了半个小时也不明白,后面发现tmd居然是要在函数中输出false的语句…
总之,我裂开了。至于后面的四道还算简单,很快就过了。

话不多说,直接上代码

1.实现单链表的初始化,插入、删除、访问等基本操作。 单链表为带头结点的单链表结构。

#include <stdio.h>
#include <stdlib.h>
typedef enum {false, true} bool;
#define ERROR NULL
typedef int datatype;
typedef struct ListNode {  //定义单链表结点类型
    datatype data;
    struct ListNode *next;
}node;
typedef node* List;

List InitList(){
	List head;
	head = (List)malloc(sizeof(node));
	head->next = NULL;
	return head;
}
node* Find( List L, datatype X ){
    List p;
    p = (List)malloc(sizeof(node));
    p = L;
    while(p){
        if(p->data == X){
            return p;
        }
        p = p->next;
    }
    return p;
}
bool Insert( List L, datatype X,  node* P ){
    List q,r;
    datatype flag;
    r = (List)malloc(sizeof(node));
    r->data = X;
    q = (List)malloc(sizeof(node));
    q = L;
    while(q->next!=P){
        q = q->next;
        if(q == NULL){
        	printf("Wrong Position for Insertion\n");
        return false;
        break;
        }
    }
    if(P == NULL){
        q->next = r;
        r->next = NULL;
    }
    else
    {
        r->next = q->next;
        q->next = r;

    }
    return true;
}
bool Delete( List L, node* P )
{
    List p;
    p = (List)malloc(sizeof(node));
    p = L;
    while(p){
        if(p->next == P)
        {
        p->next = P->next;
        free(P);
        
        break;
        }
    p = p->next;
}
if(p == NULL){
	 printf("Wrong Position for Deletion\n");
    return false;
}
else
    return true;
}
void DispList(List L){
    List p;
    p = L->next;
    while(p){
        printf("%d ",p->data);
        p = p->next;
    }

}
int main()
{
    List L;
    datatype X;
    node* P;
    int N;
    bool flag,flog;

    L = InitList();
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        flag = Insert(L, X, NULL);
        if ( flag==false ) printf("Insert Error\n");
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        P = Find(L, X);
        if ( P == ERROR )
            printf("Finding Error: %d is not in.\n", X);
        else {
            flag=Delete(L,P);
            if ( flag == true )
            printf("%d is found and deleted.\n", X);
        }
    }
    scanf("%d", &X);
    flag = Insert(L, X, L->next);
    if ( flag==false ) printf("Insert Error.\n");
    else
        printf("%d is inserted as the first element.\n", X);

    P = (node*)malloc(sizeof(node));
    flog = Insert(L, X, P);
    if ( flog==true ) printf("Wrong Position for Insertion\n");
    flog = Delete(L, P);
    if ( flog==true ) printf("Wrong Position for Insertion\n");

    DispList(L);
    return 0;
}

2.
假设带头结点的单链表L是升序排列的,将值为x的结点插入到链表L中,并保持链表有序性。

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;
typedef struct ListNode {  //定义单链表结点类型
    datatype data;
    struct ListNode *next;
}node;
typedef node* List;

List CreatbyQueue();//创建一个非空的带头结点单链表
void Insert( List L, datatype X){
	List q,p;
	p = L->next;
		q = L;
	while (p != NULL && p->data < X)
		{
		q = p;
		p= p->next;
		}
		p = (List)malloc(sizeof(List));
		p->data = X;
		p->next = q->next;
			q->next = p;
}

int main()
{
    List L;
    datatype X;
    node* p;
    int N;
    L=CreatbyQueue();
    scanf("%d",&N);
    while(N--){
        scanf("%d",&X);
        Insert(L,X);
    }
    for ( p=L->next; p; p = p->next ) printf("%d ", p->data);
    return 0;
}
List CreatbyQueue()
{
    List L,R,S;
    datatype X;
    int N;
    L=R=(List)malloc(sizeof(node));
    L->next=NULL;
    scanf("%d",&N);
    while (N--)
    {
         scanf("%d",&X);
         S=(List)malloc(sizeof(node));
         S->data=X;
         R->next=S;
         R=S;
   }
    R->next=NULL;
    return L;
}

3.

#include<stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct ListNode {  //定义单链表结点类型
    datatype data;
    struct ListNode *next;
}node;
typedef node* List;
List CreatbyQueue();
void  DelAllX(List L,datatype X){
    List p = L->next, pre = L, q; 
    while(p!=NULL){
        if(p->data==X){
            q = p; 
            p=p->next;
            pre->next=p;  
            free(q);  
        }else{
           pre=p;
           p=p->next;
        } 
    }
}
int main()
{
    List L;
    datatype X;
    node* p;
    L=CreatbyQueue();
    scanf("%d",&X);
    DelAllX(L,X);

    for ( p=L->next; p; p = p->next ) printf("%d ", p->data);
    return 0;
}

List CreatbyQueue()
{
    List L,R,S;
    datatype X;
    int N;
    L=R=(List)malloc(sizeof(node));
    L->next=NULL;
    scanf("%d",&N);
    while (N--)
    {
         scanf("%d",&X);
         S=(List)malloc(sizeof(node));
         S->data=X;
         R->next=S;
         R=S;
   }
    R->next=NULL;
    return L;
}

4.已知两个带头结点的单链表L1和L2中的结点值均已按升序排序,设计一个算法,将L1和L2合并成一个升序的带头结单链表,并用L1记录新的带头结点单链表

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;
typedef struct ListNode {  //定义单链表结点类型
    datatype data;
    struct ListNode *next;
}node;
typedef node* List;

List CreatbyQueue();
List MergeAscend(List L1,List L2);
int main()
{
    List L1,L2,p,m;
    p = (List)malloc(sizeof(node));
    L1=CreatbyQueue();
    L2=CreatbyQueue();
    m = MergeAscend( L1, L2);
    p = m->next;
    while(p){
        printf("%d ", p->data);
        p = p->next;
    }
    return 0;
}

List MergeAscend(List L1,List L2)
{
    List head,x,y,p;
    head=(List)malloc(sizeof(node));
    head->next=NULL;
    p=head;
    x=L1->next;
    y=L2->next;
    while(x&&y)
    {
        if(x->data<=y->data)
        {
            p->next=x;
            p=x;
            x=x->next;
        }
        else
        {

            p->next=y;
            p=y;
            y=y->next;
        }
    }
    p->next=x>=y?x:y;
    return head;
}
List CreatbyQueue()
{
    List L,R,S;
    datatype X;
    int N;
    L=R=(List)malloc(sizeof(node));
    L->next=NULL;
    scanf("%d",&N);
    while (N--)
    {
         scanf("%d",&X);
         S=(List)malloc(sizeof(node));
         S->data=X;
         R->next=S;
         R=S;
   }
    R->next=NULL;
    return L;
}

5.编写一个程序,用尽可能快的方法返回带头结点单链表中倒数第k个结点的地址,如果不存在,则返回ERROR

#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL

typedef int datatype;
typedef struct ListNode {  //定义单链表结点类型
    datatype data;
    struct ListNode *next;
}node;
typedef node* List;

List CreatbyQueue();
List Search(List L,int k);
int main()
{
    List L;
    int k,N;
    node *p;
    L=CreatbyQueue();

    scanf("%d",&N);
    while(N--){
         scanf("%d",&k);
         p=Search( L, k);
         if (p==ERROR) printf("Finding Error.the %d position is not in.\n",k);
         else printf("the %d position value is %d.\n",k,p->data);
    }
    return 0;
}
List Search(List L,int k){
    List x1,x2,x3;
    x1 = x2 = x3 = L;
    int sum = k;
    if(k<0) {
		return NULL;
	}
	if(L==NULL)
	{
		return NULL;
	}
	while(sum>0){
        x3 = x3->next;
        if(x3 == NULL){
            return NULL;
        }
        sum--;
	}
    int ans=0;
    while(x1->next)
    {
        if(ans<k-1)
        {
            x1=x1->next;
            ans++;
        }
        else
        {
            x1=x1->next;
            x2=x2->next;
        }
    }
    return x2;
}
List CreatbyQueue()
{
    List L,R,S;
    datatype X;
    int N;
    L=R=(List)malloc(sizeof(node));
    L->next=NULL;
    scanf("%d",&N);
    while (N--)
    {
         scanf("%d",&X);
         S=(List)malloc(sizeof(node));
         S->data=X;
         R->next=S;
         R=S;
   }
    R->next=NULL;
    return L;
}

相关文章