User Tools

Site Tools


user:kpryslop:portfolio:project1

Linked List Program

A project for DATA by Kyle Pryslopski Fall Semester 2011.

This project was begun on ? and is anticipated to take till 12/2. (Upon completion you can correct this with the actual length).

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • Matt
  • Joe
  • Karl
  • linked lists
  • character fuctions

Background

Scope

This project is to make a program that makes a list and edits it.

Attributes

Procedure

I made the separate programs then put them together to make the final program.

Code

#include <stdio.h>
#include <stdlib.h>
 
struct node {
        int value;
        struct node *next;
        struct node *prev;
};
typedef struct node Node;
 
int main()
{
        int input, input2, i = 0;
        Node *start, *tmp, *tmp2, *end;
 
        start = tmp = NULL;
 
        printf("Enter a value (-1 to quit): ");
        scanf("%d", &input);
 
        do {
 
                if (input == -1)
                        break;
 
                if (start == NULL)
                {
                        start = (Node *) malloc (sizeof(Node));
                        start -> value = input;
                        start -> next = NULL;
                        tmp = start;
                        end = start;
                }
                else
                {
                        tmp -> next = (Node *) malloc (sizeof(Node));
                        tmp -> next -> value = input;
                        tmp -> next = (Node *) malloc (sizeof(Node));
                        tmp -> next -> value = input;
                        tmp -> next -> next = NULL;
                        tmp = tmp -> next;
                        end = end -> next;
                }
 
                printf("Enter a value (-1 to quit): ");
                scanf("%d", &input);
 
        } while (input != -1);
 
        tmp = start;
 
        while(tmp != NULL)
        {
                printf("[%d] value = %d\n", i, tmp -> value);
                tmp = tmp -> next;
                i++;
        }
 
        printf("Select a node to delete: ");
        scanf("%d", &input2);
 
        tmp = start;
        for(i=0; i<(input2-1); i++)
        {
                tmp = tmp -> next;
        }
 
        if ((tmp == start) && (input != 1))
        {
                start = start -> next;
                tmp -> next = NULL;
                free(tmp);
                tmp = NULL;
        }
        else
        {
                tmp2 = tmp -> next;
                tmp -> next = tmp2 -> next;
                tmp2 -> next = NULL;
                free(tmp2);
                tmp2 = NULL;
        }
 
        i = 0;
        tmp = start;
 
        while(tmp != NULL)
        {
                printf("[%d] value = %d\n", i, tmp -> value);
                tmp = tmp -> next;
                i++;
        }
 
        printf("Enter node to insert before: ");
        scanf("%d", &input);
 
        tmp = start;
 
        for(i=0; i<(input-1); i++)
        {
                tmp = tmp -> next;
        }
 
        printf("Enter a value: ");
        scanf("%d", &input2);
 
        tmp2 = (Node *) malloc (sizeof(Node));
        tmp2 -> value = input2;
 
        if((tmp == start) && (input != 1))
        {
                tmp2 -> next = start;
                start = tmp2;
        }
        else
        {
                tmp2 -> next = tmp -> next;
                tmp -> next = tmp2;
        }
 
        i = 0;
        tmp = start;
 
        while(tmp != NULL)
        {
                printf("[%d] value = %d\n", i, tmp -> value);
                tmp = tmp -> next;
                i++;
        }
 
        printf("Enter node to insert after: ");
        scanf("%d", &input);
 
        tmp = start;
        for(i=0; i<input; i++)
        {
                tmp = tmp -> next;
        }
 
        printf("Enter a value: ");
        scanf("%d", &input2);
 
        tmp2 = (Node *) malloc (sizeof(Node));
        tmp2 -> value = input2;
        if((tmp == end) && (input != 1))
        {
                tmp2 -> prev = end;
                end -> next = tmp2;
                end = end -> next;
        }
        else
        {
                tmp2 -> prev = tmp;
                tmp2 -> next = tmp -> next;
                tmp -> next = tmp2;
                tmp2 -> next -> prev = tmp2;
        }
 
        i = 0;
        tmp = start;
 
        while(tmp != NULL)
        {
                printf("[%d] value = %d\n", i, tmp -> value);
                tmp = tmp -> next;
                i++;
        }
 
        return(0);
}

Execution

Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:

lab46:~/src/cprog/linkedA$ ./projecta
Enter a value (-1 to quit): 1
Enter a value (-1 to quit): 2
Enter a value (-1 to quit): 3
Enter a value (-1 to quit): 4
Enter a value (-1 to quit): 5
Enter a value (-1 to quit): -1
[0] value = 1
[1] value = 2
[2] value = 3
[3] value = 4
[4] value = 5
Select a node to delete: 2
[0] value = 1
[1] value = 2
[2] value = 4
[3] value = 5
Enter node to insert before: 2
Enter a value: 3
[0] value = 1
[1] value = 2
[2] value = 3
[3] value = 4
[4] value = 5
Enter node to insert after: 4
Enter a value: 6
[0] value = 1
[1] value = 2
[2] value = 3
[3] value = 4
[4] value = 5
[5] value = 6
lab46:~/src/cprog/linkedA$ 

Reflection

This was a confusing program to make because the append function was not working.

References

user/kpryslop/portfolio/project1.txt · Last modified: 2011/12/02 18:51 by kpryslop